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

import java.time.LocalDateTime;
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.BusiOfflineArresterRepairDao;
import com.huatek.busi.dto.BusiOfflineArresterRepairDto;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.entity.BusiOfflineArresterRepair;
import com.huatek.busi.mapper.BusiOfflineArresterRepairMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.arrester.BusiOfflineArresterRepairService;
import com.huatek.busi.vo.BusiOfflineArresterRepairVo;
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;

/**
 * 出场线（避雷器）——检修单 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 8, 2019 4:38:28 PM
 */
@Component
@Service(interfaceClass = BusiOfflineArresterRepairService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineArresterRepairServiceImpl implements BusiOfflineArresterRepairService {

    @Autowired
    private BusiOfflineArresterRepairMapper busiOfflineArresterRepairMapper;
    @Autowired
    private BusiOfflineArresterRepairDao busiOfflineArresterRepairDao;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public PageInfo<BusiOfflineArresterRepairVo>
        findBusiOfflineArresterRepairPage(BusiOfflineArresterRepairDto busiOfflineArresterRepairDto) {
        return PageHelper.startPage(busiOfflineArresterRepairDto.getPage(), busiOfflineArresterRepairDto.getLimit())
            .doSelectPageInfo(() -> busiOfflineArresterRepairMapper
                .selectBusiOfflineArresterRepairPage(busiOfflineArresterRepairDto));
    }

    @Override
    public BusiOfflineArresterRepairVo findBusiOfflineArresterRepairVo(String id) {
        return busiOfflineArresterRepairMapper.getBusiOfflineArresterRepairVo(id);
    }

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

    @Override
    public int deleteBusiOfflineArresterRepair(String token, String[] ids) {
        int count = 0;
        if (ids == null || ids.length == 0) {
            return count;
        }
        for (String id : ids) {
            BusiOfflineArresterRepair busiOfflineArresterRepair = busiOfflineArresterRepairDao.get(id);
            if (busiOfflineArresterRepair == null) {
                continue;
            }
            busiOfflineArresterRepair.setIsDelete("2");
            count += busiOfflineArresterRepairDao.updateLocal(busiOfflineArresterRepair);
        }
        return count;
    }

    @Override
    public int dealBusiOfflineArresterRepair(String token, String[] ids, String dealResult) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        LocalUser user = UserUtil.getLocalUser(token);
        int count = 0;
        for (String id : ids) {
            BusiOfflineArresterRepair busiOfflineArresterRepair = busiOfflineArresterRepairDao.get(id);
            if (busiOfflineArresterRepair == null) {
                continue;
            }
            // 1:已处理
            busiOfflineArresterRepair.setDealStatus("1");
            busiOfflineArresterRepair.setDealResult(dealResult);
            if (user != null) {
                busiOfflineArresterRepair.setHandleUser(user.getUid());
                busiOfflineArresterRepair.setHandleTime(LocalDateTime.now());
                busiOfflineArresterRepair.setHandleName(user.getUserName());
            }
            count += busiOfflineArresterRepairDao.updateLocal(busiOfflineArresterRepair);
        }
        return count;
    }

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

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

    @Override
    public BusiOfflineArresterRepairVo isFinishedOverhaul(String malfunctionId) {
        return busiOfflineArresterRepairMapper.isFinishedOverhaul(malfunctionId);
    }
}