package com.huatek.busi.service.impl.gis;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.BusiConstant.DealStatus;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.dao.BusiOfflineGisRepairDao;
import com.huatek.busi.dto.BusiOfflineGisRepairDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.entity.BusiOfflineGisRepair;
import com.huatek.busi.mapper.BusiOfflineGisRepairMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.gis.BusiOfflineGisRepairService;
import com.huatek.busi.vo.BusiOfflineGisRepairVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

import lombok.extern.slf4j.Slf4j;

/**
 * GIS检修记录 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:39 PM
 */
@Slf4j
@Component
@Service(interfaceClass = BusiOfflineGisRepairService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineGisRepairServiceImpl implements BusiOfflineGisRepairService {

    @Autowired
    private BusiOfflineGisRepairMapper busiOfflineGisRepairMapper;
    @Autowired
    private BusiOfflineGisRepairDao busiOfflineGisRepairDao;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public PageInfo<BusiOfflineGisRepairVo>
        findBusiOfflineGisRepairPage(BusiOfflineGisRepairDto busiOfflineGisRepairDto) {
        return PageHelper.startPage(busiOfflineGisRepairDto.getPage(), busiOfflineGisRepairDto.getLimit())
            .doSelectPageInfo(() -> busiOfflineGisRepairMapper.selectBusiOfflineGisRepairPage(busiOfflineGisRepairDto));
    }

    @Override
    public BusiOfflineGisRepairVo findBusiOfflineGisRepairVo(String id) {
        return busiOfflineGisRepairMapper.getBusiOfflineGisRepairVo(id);
    }

    @Override
    public synchronized int saveOrUpdateGisRepair(String token, BusiOfflineGisRepairDto busiOfflineGisRepairDto) {
        LocalUser user = UserUtil.getLocalUser(token);
        BusiOfflineGisRepair busiOfflineGisRepair = new BusiOfflineGisRepair();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineGisRepairDto, busiOfflineGisRepair);
        //外挂装置
        BusiExternalEquipment busiExternalEquipment = busiExternalEquipmentService
            .findBySensorCode(busiOfflineGisRepairDto.getSensorCode(), SiteConstants.ExternalTypeEnum.GIS.getValue());
        if (busiExternalEquipment != null) {
            busiOfflineGisRepair.setSubEquipmentId(busiExternalEquipment.getId());
        }
        if (StringUtils.isEmpty(busiOfflineGisRepair.getId())) {
            busiOfflineGisRepair.setCreateTime(LocalDateTime.now());
            if (user != null) {
                busiOfflineGisRepair.setCreateUser(user.getUid());
                busiOfflineGisRepair.setCreateName(user.getUserName());
            }
            busiOfflineGisRepair.setIsDelete(Status.NORMAL.getValue());
            busiOfflineGisRepair.setDealStatus(DealStatus.TO_DEAL);
        } else {
            busiOfflineGisRepair.setModifiedTime(LocalDateTime.now());
            if (user != null) {
                busiOfflineGisRepair.setModifiedUser(user.getUid());
                busiOfflineGisRepair.setModifiedName(user.getUserName());
            }
        }
        // 传感器信息
        EquipmentInfoDto equipmentInfoDto =
            busiSubTreeService.findEquipmentInfo(busiOfflineGisRepairDto.getSensorCode(), SiteTreeEnum.GIS.getValue());
        if (equipmentInfoDto != null) {
            busiOfflineGisRepair.setMainEquipmentId(equipmentInfoDto.getMainEquipmentId());
            busiOfflineGisRepair.setSiteId(equipmentInfoDto.getSiteId());
        }
        return busiOfflineGisRepairDao.saveOrUpdate(busiOfflineGisRepair);
    }

    @Override
    public int deleteBusiOfflineGisRepair(String token, String id) {
        int count = busiOfflineGisRepairDao.del(id);
        return count;
    }

    @Override
    public void toRepair(String token, BusiEqmMalfunction busiEqmMalfunction) {
        BusiOfflineGisRepair busiOfflineGisRepair = new BusiOfflineGisRepair();
        busiOfflineGisRepair.setId(Uuid.getUUID());
        busiOfflineGisRepair.setSiteId(busiEqmMalfunction.getSiteId());
        busiOfflineGisRepair.setMainEquipmentId(busiEqmMalfunction.getMainEquipmentId());
        busiOfflineGisRepair.setSubEquipmentId(busiEqmMalfunction.getSubEquipmentId());
        busiOfflineGisRepair.setWarningId(busiEqmMalfunction.getId());
        busiOfflineGisRepair.setDiscoveryTime(busiEqmMalfunction.getWarningTime());
        busiOfflineGisRepair.setFailDescription(busiEqmMalfunction.getWarningMsg());
        busiOfflineGisRepair.setIsDelete(Status.NORMAL.getValue());
        busiOfflineGisRepair.setDealStatus(DealStatus.TO_DEAL);
        LocalUser user = UserUtil.getLocalUser(token);
        if (user != null) {
            busiOfflineGisRepair.setCreateUser(user.getUid());
            busiOfflineGisRepair.setCreateName(user.getUserName());
        }
        busiOfflineGisRepair.setCreateTime(LocalDateTime.now());
        busiOfflineGisRepair.setSensorCode(busiEqmMalfunction.getSensorId());
        busiOfflineGisRepair.setSignage(busiEqmMalfunction.getSensorBrand());
        busiOfflineGisRepair.setInstallLocation(busiEqmMalfunction.getSensorInstallLocation());
        busiOfflineGisRepairDao.add(busiOfflineGisRepair);
    }

    @Override
    public int deleteBusiOfflineGisRepair(String token, List<String> idList) {
        return busiOfflineGisRepairMapper.deleteBusiOfflineGisRepair(idList);
    }

    @Override
    public void dealGisRepair(String token, List<String> idList, String dealReasult) {
        if (idList == null || idList.size() == 0) {
            return;
        }
        for (String id : idList) {
            BusiOfflineGisRepair busiOfflineGisRepair = busiOfflineGisRepairDao.get(id);
            if (busiOfflineGisRepair == null) {
                log.debug("维修单信息不存在！ ID：{}", id);
                continue;
            }
            LocalUser user = UserUtil.getLocalUser(token);
            if (user != null) {
                busiOfflineGisRepair.setHandleUser(user.getUid());
                busiOfflineGisRepair.setHandleName(user.getUserName());
            }
            busiOfflineGisRepair.setHandleTime(LocalDateTime.now());
            busiOfflineGisRepair.setDealStatus("1");
            busiOfflineGisRepair.setDealResult(dealReasult);
            busiOfflineGisRepairDao.updateLocal(busiOfflineGisRepair);
        }
    }

    @Override
    public Integer getRepairCountForGis(Map<String, Object> params) {
        return this.busiOfflineGisRepairMapper.getRepairCountForGis(params);
    }

    @Override
    public Integer getRepairCount(Map<String, Object> params) {
        return this.busiOfflineGisRepairMapper.getRepairCount(params);
    }
    
    @Override
    public List<BusiOfflineGisRepairVo> findBusiOfflineGisRepair(String siteId, String mainTreeId) {
        return busiOfflineGisRepairMapper.findBusiOfflineGisRepair(siteId, mainTreeId);
    }

    @Override
    public BusiOfflineGisRepairVo isFinishedOverhaul(String malfunctionId) {
        return busiOfflineGisRepairMapper.isFinishedOverhaul(malfunctionId);
    }
}