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

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.BusiOfflineGeneratorRepairDao;
import com.huatek.busi.dao.BusiOfflineTransformerRepairDao;
import com.huatek.busi.dto.BusiOfflineGeneratorRepairDto;
import com.huatek.busi.dto.BusiOfflineTransformerRepairDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.BusiOfflineGeneratorRepairMapper;
import com.huatek.busi.mapper.BusiOfflineTransformerRepairMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.generatro.BusiOfflineGeneratorRepairService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerRepairService;
import com.huatek.busi.vo.BusiOfflineGeneratorRepairVo;
import com.huatek.busi.vo.BusiOfflineTransformerRepairVo;
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;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Autowired
    private BusiOfflineGeneratorRepairMapper busiOfflineGeneratorRepairMapper;
    @Autowired
    private BusiOfflineGeneratorRepairDao busiOfflineGeneratorRepairDao;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public PageInfo<BusiOfflineGeneratorRepairVo>
    findBusiOfflineGeneratorRepairPage(BusiOfflineGeneratorRepairDto busiOfflineGeneratorRepairDto) {
        return PageHelper
                .startPage(busiOfflineGeneratorRepairDto.getPage(), busiOfflineGeneratorRepairDto.getLimit())
                .doSelectPageInfo(() -> busiOfflineGeneratorRepairMapper
                        .selectBusiOfflineGeneratorRepairPage(busiOfflineGeneratorRepairDto));
    }

    @Override
    public BusiOfflineGeneratorRepairVo findBusiOfflineGeneratorRepairVo(String id) {
        return busiOfflineGeneratorRepairMapper.getBusiOfflineGeneratorRepairVo(id);
    }

    @Override
    public int  saveOrUpdate(String token, BusiOfflineGeneratorRepairDto busiOfflineGeneratorRepairDto) {
        BusiOfflineGeneratorRepair busiOfflineGeneratorRepair = new BusiOfflineGeneratorRepair();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineGeneratorRepairDto, busiOfflineGeneratorRepair);
        LocalUser user = UserUtil.getLocalUser(token);
        //外挂设备
        BusiExternalEquipment busiExternalEquipment = busiExternalEquipmentService.findBySensorCode(
                busiOfflineGeneratorRepairDto.getSensorCode(), SiteConstants.ExternalTypeEnum.GEN.getValue());
        if (busiExternalEquipment != null) {
            busiOfflineGeneratorRepair.setSubEquipmentId(busiExternalEquipment.getId());
        }
        if (StringUtils.isEmpty(busiOfflineGeneratorRepairDto.getId())) {
            busiOfflineGeneratorRepair.setIsDelete(SiteConstants.Status.NORMAL.getValue());
            busiOfflineGeneratorRepair.setDealStatus(BusiConstant.DealStatus.TO_DEAL);
            busiOfflineGeneratorRepair.setCreateTime(LocalDateTime.now());
            if (user != null) {
                busiOfflineGeneratorRepair.setCreateUser(user.getUid());
                busiOfflineGeneratorRepair.setCreateName(user.getUserName());
            }
        } else {
            busiOfflineGeneratorRepair.setModifiedTime(LocalDateTime.now());
            if (user != null) {
                busiOfflineGeneratorRepair.setModifiedUser(user.getUid());
                busiOfflineGeneratorRepair.setModifiedName(user.getUserName());
            }
        }
        // 传感器信息
        EquipmentInfoDto equipmentInfoDto = busiSubTreeService
                .findEquipmentInfo(busiOfflineGeneratorRepairDto.getSensorCode(), SiteConstants.SiteTreeEnum.GEN.getValue());
        if (equipmentInfoDto != null) {
            //主设备——发电机标牌表
            busiOfflineGeneratorRepair.setMainEquipmentId(equipmentInfoDto.getMainEquipmentId());
            busiOfflineGeneratorRepair.setSiteId(equipmentInfoDto.getSiteId());
        }
        return busiOfflineGeneratorRepairDao.saveOrUpdate(busiOfflineGeneratorRepair);
    }

    //删除发电机修理单
    @Override
    public int deleteBusiOfflineGeneratorRepair(String token, List<String> idList) {
        return busiOfflineGeneratorRepairMapper.deleteBusiOfflineGeneratorRepair(idList);
    }

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

    @Override
    public BusiOfflineGeneratorRepairVo isFinishedOverhaul(String malfunctionId) {
        return busiOfflineGeneratorRepairMapper.isFinishedOverhaul(malfunctionId);
    }

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

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