package com.elephant.framework.galaxy.aroad.module.roadcondition.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.roadcondition.dto.*;
import com.elephant.framework.galaxy.aroad.module.roadcondition.entity.RoadConditionEvent;
import com.elephant.framework.galaxy.aroad.module.roadcondition.mapper.RoadConditionEventMapper;
import com.elephant.framework.galaxy.aroad.module.roadcondition.service.RoadConditionEventService;
import com.elephant.framework.galaxy.aroad.module.szgl.entity.SzglGsdGcz;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 路况事件统计
 *
 * @author Xingyuyang
 * @date 2019-04-23 16:41:06
 */
@Service
@AllArgsConstructor
public class RoadConditionEventServiceImpl
    extends ServiceImpl<RoadConditionEventMapper, RoadConditionEvent>
    implements RoadConditionEventService {

  private final RoadConditionEventMapper roadConditionEventMapper;

  /**
   * 根据事件ID查询该路况事件是否已经存在（大于0表示已存在）
   *
   * @param eventID
   * @return
   */
  public Integer getRoadMaintainEventByEventID(String eventID) {
    return roadConditionEventMapper.selectCount(
        Wrappers.<RoadConditionEvent>query().lambda().eq(RoadConditionEvent::getEventId, eventID));
  }

  /**
   * 查询数据库是否存在该实体
   *
   * @param event
   * @return
   */
  public RoadConditionEvent selectRoadMaintainEvent(RoadConditionEvent event) {
    return baseMapper.selectRoadMaintainEvent(event);
  }

  /** 撤销事件,根据事件ID更新flag标识 */
  public Integer updateBatchByEventId(List<Object> eventIdList) {
    return baseMapper.updateBatchByEventId(eventIdList);
  }

  /** 删除事件（假删除），更新flag标识为1 */
  public Integer deleteBatchByEvent(RoadConditionEvent event) {
    return baseMapper.deleteBatchByEvent(event);
  }
  /**
   * 普通国省道养护施工事件信息是否已存在
   *
   * @param
   * @return
   */
  public Integer getRoadMaintainEventByRoadPlatform(
      String inftypename,
      String inftitle,
      String infcontent,
      String infwrinter,
      Date writerdate,
      String infchecker,
      Date checkdate,
      Date removedate,
      //      String remark,
      String glno,
      String writerno,
      String loctx,
      String locty,
      String zhuanghao,
      String daolu,
      // String removereson,
      //      String departname,
      //      String removedepart,
      //      Date expdate,
      //      String impact,
      //      String zhfrom,
      //      String zhto,
      String yccd) {
    return roadConditionEventMapper.selectCount(
        Wrappers.<RoadConditionEvent>query()
            .lambda()
            //            .eq(RoadConditionEvent::getEventId, infid)
            .eq(RoadConditionEvent::getEventType, inftypename)
            .eq(RoadConditionEvent::getEventTitle, inftitle)
            .eq(RoadConditionEvent::getEventContent, infcontent)
            .eq(RoadConditionEvent::getStartPerson, infwrinter)
            .eq(RoadConditionEvent::getStartTime, writerdate)
            .eq(RoadConditionEvent::getCheckPerson, infchecker)
            .eq(RoadConditionEvent::getCheckTime, checkdate)
            .eq(RoadConditionEvent::getEndTime, removedate)
            //            .eq(RoadConditionEvent::getStartBz, remark)
            .eq(RoadConditionEvent::getRoadCode, glno)
            .eq(RoadConditionEvent::getStartUnitNo, writerno)
            .eq(RoadConditionEvent::getLongitude, loctx)
            .eq(RoadConditionEvent::getLatitude, locty)
            .eq(RoadConditionEvent::getStoem, zhuanghao)
            .eq(RoadConditionEvent::getRoadName, daolu)
            // .eq(RoadConditionEvent::getEndReason, removereson)
            //            .eq(RoadConditionEvent::getMaintainUnit, departname)
            //            .eq(RoadConditionEvent::getEndUnit, removedepart)
            //            .eq(RoadConditionEvent::getPreEndTime, expdate)
            //            .eq(RoadConditionEvent::getInfluence, impact)
            //            .eq(RoadConditionEvent::getStartm, zhfrom)
            //            .eq(RoadConditionEvent::getEndm, zhto)
            .eq(RoadConditionEvent::getCarsLength, yccd));
  }

  public RoadConditionEvent getById(@Param("eventId") String eventId) {
    return baseMapper.getById(eventId);
  }

  public boolean deleteEmergencyEvent(@Param("eventId")String eventId){
    return baseMapper.deleteEmergencyEvent(eventId);
  }

  public boolean deleteEmergencyUnit(@Param("eventId")String eventId){
    boolean eFlag = baseMapper.deleteEmergencyEvent(eventId);
    boolean uFlag = baseMapper.deleteEmergencyUnit(eventId);
    return eFlag&&uFlag;
//    try{
//
//    }catch (RuntimeException r){
//      throw r;
//    }
  }

  public boolean saveEmergencyUnit(@Param("params") String params){
    String[] arr = params.split(",");
    String eventId = arr[0];
    String emergencyEventId = arr[1];
    return baseMapper.saveEmergencyUnit(eventId,emergencyEventId);
  }

  public boolean saveUnitWithDept(@Param("params") String params) {
    String[] paramStringList = params.split(",");
    String deptId = paramStringList[0];
    List<String> unitList = new ArrayList<>();
    for (int i = 1; i < paramStringList.length; i++) {
      unitList.add(paramStringList[i]);
    }
    baseMapper.deleteUnitByDept(deptId);
    if (unitList.size() > 1) {
      for (int i = 0; i < unitList.size(); i++) {
        baseMapper.saveUnitWithDept(deptId, unitList.get(i));
      }
    } else {
      return baseMapper.saveUnitWithDept(deptId, unitList.get(0));
    }
    return false;
  }

  public IPage<RoadConditionEvent> getRoadConditionEventPage(
      Page<RoadConditionEvent> page, RoadConditionEvent roadConditionEvent) {
    List<DeptUnitDto> unitList = baseMapper.getUnitByDept(roadConditionEvent.getDeptId());
    List<String> units = new ArrayList<>();
    if (unitList.size() > 0) {
      for (int i = 0; i < unitList.size(); i++) {
        units.add(unitList.get(i).getUnitId());
      }
    }
    return baseMapper.getRoadConditionEventPage(page, roadConditionEvent,units);
  }

  public List<SaveUnitDto> getUnitTree() {
    return baseMapper.getUnitTree();
  }

  public List<RoadConditionEvent> getRoadConditionEventMobilePage() {
    return baseMapper.getRoadConditionEventMobilePage();
  }

  /**
   * @description : 根据事件类型查询
   * @author : Xingyuyang
   * @date : 2019/4/23 0023 17:54
   * @param :
   * @return : null
   */
  //  @Cacheable(value = "staticByEventType")
  public List<staticByEventTypeDto> staticByEventType() {
    return baseMapper.staticByEventType();
  }

  /**
   * @description : 根据影响程度查询
   * @author : Xingyuyang
   * @date : 2019/4/23 0023 17:54
   * @param :
   * @return : null
   */
  //  @Cacheable(value = "staticByInfluence")
  public List<staticByInfluenceDto> staticByInfluence() {
    return baseMapper.staticByInfluence();
  }

  /**
   * @description : 根据月份查询
   * @author : Xingyuyang
   * @date : 2019-4-24 10:27:35
   * @param :
   * @return : null
   */
  //  @Cacheable(value = "staticByMonth")
  public List<staticByMonthDto> staticByMonth() {
    return baseMapper.staticByMonth();
  }

  /**
   * @description : 根据月份查询
   * @author : Xingyuyang
   * @date : 2019-4-24 10:27:35
   * @param :
   * @return : null
   */
  //  @Cacheable(value = "staticByRoadCode")
  public List<staticByRoadCodeDto> staticByRoadCode() {
    return baseMapper.staticByRoadCode();
  }

  /**
   * @description : 根据城市查询
   * @author : Xingyuyang
   * @date : 2019/4/24 0024 14:27
   * @return :
   *     java.util.List<com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByCityDto>
   */
  public List<staticByCityDto> staticByCity() {
    return baseMapper.staticByCity();
  }

  public List<DeptUnitDto> getUnitByDept(@Param("deptId") String deptId){
    return baseMapper.getUnitByDept(deptId);
  }

  //  @Cacheable(value = "roadConditionCoordinates")
  public List<CoordinateDto> getCoordinates(@Param("deptId") String deptId) {
    List<DeptUnitDto> unitList = baseMapper.getUnitByDept(deptId);
    List<String> units = new ArrayList<>();
    if (unitList.size() > 0) {
      for (int i = 0; i < unitList.size(); i++) {
        units.add(unitList.get(i).getUnitId());
      }
    }
    return baseMapper.getCoordinates(units);
  }

  @Cacheable(value = "tqCoordinates")
  public List<CoordinateDto> getTQCoordinates() {
    return baseMapper.getTQCoordinates();
  }

  @Cacheable(value = "yhCoordinates")
  public List<CoordinateDto> getYHCoordinates() {
    return baseMapper.getYHCoordinates();
  }

  @Cacheable(value = "sfzCoordinates")
  public List<CoordinateDto> getSFZCoordinates() {
    return baseMapper.getSFZCoordinates();
  }

  @Cacheable(value = "tfCoordinates")
  public List<CoordinateDto> getTFCoordinates() {
    return baseMapper.getTFCoordinates();
  }

  public boolean saveUnit() {
    return baseMapper.saveUnit();
  }

  public boolean deleteUnit() {
    return baseMapper.deleteUnit();
  }

  public boolean deleteEvent(){
    return baseMapper.deleteEvent();
  }


  /**
   * @description : 根据城市查询
   * @author : Xingyuyang
   * @date : 2019/4/24 0024 14:27
   * @return :
   *     java.util.List<com.elephant.framework.galaxy.aroad.module.roadcondition.dto.staticByCityDto>
   */
  public List<staticByYHCityDto> staticByYHCity() {
    return baseMapper.staticByYHCity();
  }

  /**
   * @Description: 查询所有服务区所属路线名称和编码的树形结构 @Param: []
   *
   * @return:
   *     java.util.List<com.elephant.framework.galaxy.travel.module.basic_resources.entity.BaseServiceArea> @Author:
   *     songwenjie @Date: 2019/4/10
   */
  //  @Cacheable(value="cityTree")
  public BaseTreeDto getTree() {
    List<CityNameDto> cityTree = baseMapper.getTree();
    List<SingleNameDto> Jinan = new ArrayList<>();
    List<SingleNameDto> Qingdao = new ArrayList<>();
    List<SingleNameDto> Zibo = new ArrayList<>();
    List<SingleNameDto> Dongying = new ArrayList<>();
    List<SingleNameDto> Yantai = new ArrayList<>();
    List<SingleNameDto> Weihai = new ArrayList<>();
    List<SingleNameDto> Rizhao = new ArrayList<>();
    List<SingleNameDto> Taian = new ArrayList<>();
    List<SingleNameDto> Binzhou = new ArrayList<>();
    List<SingleNameDto> Dezhou = new ArrayList<>();
    List<SingleNameDto> Liaocheng = new ArrayList<>();
    List<SingleNameDto> Heze = new ArrayList<>();
    List<SingleNameDto> Weifang = new ArrayList<>();
    List<SingleNameDto> Linyi = new ArrayList<>();
    List<SingleNameDto> Jining = new ArrayList<>();
    List<SingleNameDto> Zaozhuang = new ArrayList<>();
    List<CitysWithOrganizes> citysWithOrganizes = new ArrayList<>();
    cityTree.forEach(
        a -> {
          if (a.getCityName().equals("济南")) {
            Jinan.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("青岛")) {
            Qingdao.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("东营")) {
            Dongying.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("临沂")) {
            Linyi.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("日照")) {
            Rizhao.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("潍坊")) {
            Weifang.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("淄博")) {
            Zibo.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("烟台")) {
            Yantai.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("菏泽")) {
            Heze.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("济宁")) {
            Jining.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("泰安")) {
            Taian.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("威海")) {
            Weihai.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("枣庄")) {
            Zaozhuang.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("滨州")) {
            Binzhou.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("德州")) {
            Dezhou.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
          if (a.getCityName().equals("聊城")) {
            Liaocheng.add(SingleNameDto.builder().organizeName(a.getOrganizeName()).build());
          }
        });

    {
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("济南").names(Jinan).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("青岛").names(Qingdao).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("淄博").names(Zibo).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("潍坊").names(Weifang).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("东营").names(Dongying).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("滨州").names(Binzhou).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("德州").names(Dezhou).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("威海").names(Weihai).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("烟台").names(Yantai).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("聊城").names(Liaocheng).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("泰安").names(Taian).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("临沂").names(Linyi).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("日照").names(Rizhao).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("枣庄").names(Zaozhuang).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("济宁").names(Jining).build());
      citysWithOrganizes.add(CitysWithOrganizes.builder().city("菏泽").names(Heze).build());
    }
    return BaseTreeDto.builder().citysWithOrganizes(citysWithOrganizes).build();
  }
  /**
   * 根据路线编号，中心桩好查询路况事件
   * @param lxbh
   * @param zxzh
   * @Auth zyw
   * @return
   */
  @Override
  public List<RoadConditionEvent> getLksjForQS(String lxbh, Double zxzh) {
    List<RoadConditionEvent> list = roadConditionEventMapper.getLksjForQS(lxbh,zxzh);
    return list;
  }

}
