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

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

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.frame.base.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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.Status;
import com.huatek.busi.dao.BusiOfflineCableRepairDao;
import com.huatek.busi.dto.BusiOfflineCableRepairDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiOfflineCableRepair;
import com.huatek.busi.mapper.BusiOfflineCableRepairMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.cable.BusiOfflineCableRepairService;
import com.huatek.busi.vo.BusiOfflineCableRepairVo;
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;
import org.springframework.util.StringUtils;


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

    @Autowired
    private BusiOfflineCableRepairMapper busiOfflineCableRepairMapper;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;
    @Autowired
    private BusiOfflineCableRepairDao busiOfflineCableRepairDao;

    @Override
    public PageInfo<BusiOfflineCableRepairVo> findBusiOfflineCableRepairPage(
            BusiOfflineCableRepairDto busiOfflineCableRepairDto) {
        return PageHelper.startPage(busiOfflineCableRepairDto.getPage(), busiOfflineCableRepairDto.getLimit())
                .doSelectPageInfo(() -> busiOfflineCableRepairMapper.selectBusiOfflineCableRepairPage(busiOfflineCableRepairDto));
    }

    @Override
    public BusiOfflineCableRepairVo findBusiOfflineCableRepairVo(String id) {
        return busiOfflineCableRepairMapper.getBusiOfflineCableRepairVo(id);
    }

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

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

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

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

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

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

    @Override
    public Integer getRepairCount(Map<String, Object> params) {
        Integer repairCount = busiOfflineCableRepairMapper.getRepairCount(params);
        return repairCount == null ? 0 : repairCount;
    }

    @Override
    public BusiOfflineCableRepairVo findBusiOfflineCableRepair(String mainTreeId, String siteId) {
        return busiOfflineCableRepairMapper.findBusiOfflineCableRepair(mainTreeId, siteId);
    }

    @Override
    public List<BusiOfflineCableRepairVo> findBusiOfflineCableRepairList(
            BusiOfflineCableRepairDto busiOfflineCableRepairDto) {
        return busiOfflineCableRepairMapper.findBusiOfflineCableRepairList(busiOfflineCableRepairDto);
    }

    @Override
    public BusiOfflineCableRepairVo isFinishedOverhaul(String malfunctionId) {
        return busiOfflineCableRepairMapper.isFinishedOverhaul(malfunctionId);
    }

}
