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

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

import com.huatek.busi.common.BusiConstant;
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 org.springframework.util.ObjectUtils;

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.BusiOfflineTransformerRepairDao;
import com.huatek.busi.dto.BusiOfflineTransformerRepairDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.entity.BusiOfflineTransformerRepair;
import com.huatek.busi.mapper.BusiOfflineTransformerRepairMapper;
import com.huatek.busi.service.BusiEqmFaultService;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerRepairService;
import com.huatek.busi.vo.BusiEqmFaultVo;
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;

/**
 * 变压器检修记录 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:39 PM
 */
@Slf4j
@Component
@Service(interfaceClass = BusiOfflineTransformerRepairService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineTransformerRepairServiceImpl implements BusiOfflineTransformerRepairService {
    @Autowired
    private BusiOfflineTransformerRepairMapper busiOfflineTransformerRepairMapper;
    @Autowired
    private BusiOfflineTransformerRepairDao busiOfflineTransformerRepairDao;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;
    @Autowired
    private BusiEqmFaultService busiEqmFaultService;

    @Override
    public PageInfo<BusiOfflineTransformerRepairVo>
        findBusiOfflineTransformerRepairPage(BusiOfflineTransformerRepairDto busiOfflineTransformerRepairDto) {
        return PageHelper
            .startPage(busiOfflineTransformerRepairDto.getPage(), busiOfflineTransformerRepairDto.getLimit())
            .doSelectPageInfo(() -> busiOfflineTransformerRepairMapper
                .selectBusiOfflineTransformerRepairPage(busiOfflineTransformerRepairDto));
    }

    @Override
    public BusiOfflineTransformerRepairVo findBusiOfflineTransformerRepairVo(String id) {
        return busiOfflineTransformerRepairMapper.getBusiOfflineTransformerRepairVo(id);
    }

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

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

    @Override
    public void toRepair(String token, BusiEqmMalfunction busiEqmMalfunction) {
        BusiOfflineTransformerRepair busiOfflineTransformerRepair = new BusiOfflineTransformerRepair();
    	if (StringUtils.isNotEmpty(busiEqmMalfunction.getThreeRatio())) {
    		//根据三比值获取对应故障信息
    		BusiEqmFaultVo busiEqmFaultVo = busiEqmFaultService.getBusiEqmFaultInfo(busiEqmMalfunction.getThreeRatio());
    		if (!ObjectUtils.isEmpty(busiEqmFaultVo)) {
    			busiOfflineTransformerRepair.setFaultFcode(busiEqmFaultVo.getFaultFcode());
    			busiOfflineTransformerRepair.setFaultFtype(busiEqmFaultVo.getFaultFtype());
    			busiOfflineTransformerRepair.setFaultShows(busiEqmFaultVo.getFaultFshow());
    			busiOfflineTransformerRepair.setFaultPossibleReasons(busiEqmFaultVo.getFaultPossibleReasons());
    			busiOfflineTransformerRepair.setFaultPosition(busiEqmFaultVo.getFaultPosition());
    			busiOfflineTransformerRepair.setFaultNotes(busiEqmFaultVo.getFaultNotes());
    		}
    	}
        busiOfflineTransformerRepair.setId(Uuid.getUUID());
        busiOfflineTransformerRepair.setSiteId(busiEqmMalfunction.getSiteId());
        busiOfflineTransformerRepair.setMainEquipmentId(busiEqmMalfunction.getMainEquipmentId());
        busiOfflineTransformerRepair.setSubEquipmentId(busiEqmMalfunction.getSubEquipmentId());
        busiOfflineTransformerRepair.setWarningId(busiEqmMalfunction.getId());
        busiOfflineTransformerRepair.setDiscoveryTime(busiEqmMalfunction.getWarningTime());
        busiOfflineTransformerRepair.setIsDelete(Status.NORMAL.getValue());
        busiOfflineTransformerRepair.setDealStatus(DealStatus.TO_DEAL);
        busiOfflineTransformerRepair.setFailDescription(busiEqmMalfunction.getWarningMsg());
        LocalUser user = UserUtil.getLocalUser(token);
        if (user != null) {
            busiOfflineTransformerRepair.setCreateUser(user.getUid());
            busiOfflineTransformerRepair.setCreateName(user.getUserName());
        }

        busiOfflineTransformerRepair.setCreateTime(LocalDateTime.now());
        busiOfflineTransformerRepair.setSensorCode(busiEqmMalfunction.getSensorId());
        busiOfflineTransformerRepair.setSignage(busiEqmMalfunction.getSensorBrand());
        busiOfflineTransformerRepair.setInstallLocation(busiEqmMalfunction.getSensorInstallLocation());
        busiOfflineTransformerRepairDao.add(busiOfflineTransformerRepair);
    }

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

    @Override
    public Integer getTransformerRepairCount(Map<String, Object> params) {
        return busiOfflineTransformerRepairMapper.getTransformerRepairCount(params);
    }

    @Override
    public BusiOfflineTransformerRepairVo isFinishedOverhaul(String malfunctionId) {
        return busiOfflineTransformerRepairMapper.isFinishedOverhaul(malfunctionId);
    }
}