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

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.busi.entity.BusiOfflineGisRepair;
import com.huatek.frame.base.util.BeanUtils;
import org.apache.poi.hssf.record.DVALRecord;
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.BusiOfflineOtherRepairDao;
import com.huatek.busi.dto.BusiOfflineOtherRepairDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.entity.BusiOfflineOtherRepair;
import com.huatek.busi.mapper.BusiOfflineOtherRepairMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.Other.BusiOfflineOtherRepairService;
import com.huatek.busi.vo.BusiOfflineOtherRepairVo;
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 = BusiOfflineOtherRepairService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineOtherRepairServiceImpl implements BusiOfflineOtherRepairService {

    @Autowired
    private BusiOfflineOtherRepairMapper busiOfflineOtherRepairMapper;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;
    @Autowired
    private BusiOfflineOtherRepairDao busiOfflineOtherRepairDao;

	@Override
	public PageInfo<BusiOfflineOtherRepairVo> findBusiOfflineOtherRepairPage(
			BusiOfflineOtherRepairDto busiOfflineOtherRepairDto) {
		// TODO Auto-generated method stub{
	        return PageHelper.startPage(busiOfflineOtherRepairDto.getPage(), busiOfflineOtherRepairDto.getLimit())
	            .doSelectPageInfo(() -> busiOfflineOtherRepairMapper.selectBusiOfflineOtherRepairPage(busiOfflineOtherRepairDto));
	}

	@Override
	public BusiOfflineOtherRepairVo findBusiOfflineOtherRepairVo(String id) {
		return busiOfflineOtherRepairMapper.getBusiOfflineOtherRepairVo(id);
	}

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

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

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

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

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

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

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

	@Override
	public BusiOfflineOtherRepairVo findBusiOfflineOtherRepair(String mainTreeId, String siteId) {
		return busiOfflineOtherRepairMapper.findBusiOfflineOtherRepair(mainTreeId, siteId);
	}

	@Override
	public List<BusiOfflineOtherRepairVo> findBusiOfflineOtherRepairList(
			BusiOfflineOtherRepairDto busiOfflineOtherRepairDto) {
		return busiOfflineOtherRepairMapper.findBusiOfflineOtherRepairList(busiOfflineOtherRepairDto);
	}

	@Override
	public BusiOfflineOtherRepairVo isFinishedOverhaul(String malfunctionId) {
		return busiOfflineOtherRepairMapper.isFinishedOverhaul(malfunctionId);
	}

}
