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

 import com.alibaba.dubbo.config.annotation.Reference;
 import com.alibaba.dubbo.config.annotation.Service;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.huatek.busi.common.SiteConstants;
 import com.huatek.busi.dao.ArresterAssessmentResultDao;
 import com.huatek.busi.entity.ArresterAssessmentResult;
 import com.huatek.busi.entity.BusiSiteTree;
 import com.huatek.busi.entity.TransformerLoadRate;
 import com.huatek.busi.mapper.ArresterAssessmentResultMapper;
 import com.huatek.busi.service.ArresterAssessmentResultService;
 import com.huatek.busi.service.param.BusiParamTransformerService;
 import com.huatek.busi.vo.ArresterAssessmentResultVo;
 import com.huatek.busi.vo.BusiGisCaculateStepVo;
 import com.huatek.busi.vo.BusiParamTransformerVoluntarilyVo;
 import com.huatek.torch.frame.tools.ConstantUtil;
 import com.huatek.torch.frame.tools.ResultUtil;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 import org.apache.commons.lang.time.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;

 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.util.*;

 /**
 * 避雷器评估
 * @author vent
 * @date 2023/03/31
 */
@Component
@Service(interfaceClass = ArresterAssessmentResultService.class)
@Transactional(rollbackFor = Exception.class)
public class ArresterAssessmentResultServiceImpl implements ArresterAssessmentResultService {

    @Autowired
    private ArresterAssessmentResultDao arresterAssessmentResultDao;
    @Autowired
    private ArresterAssessmentResultMapper arresterAssessmentResultMapper;
    @Reference(check = false)
    BusiParamTransformerService busiParamTransformerService;


    @Override
    public int saveArresterAssessmentResult(ArresterAssessmentResult arresterAssessmentResult) {
        return arresterAssessmentResultDao.add(arresterAssessmentResult);
    }

    @Override
    public PageInfo<ArresterAssessmentResultVo> findArresterAssessmentByMainTreeId(String siteId, String mainTreeId, Integer page, Integer limit, LocalDate startTime, LocalDate endTime, String type) {
    	return PageHelper.startPage(page == null ? 1 :page , limit == null? 1 : limit)
		        .doSelectPageInfo(() -> arresterAssessmentResultMapper.findArresterAssessmentByMainTreeId(siteId,mainTreeId,startTime,endTime,type));
    }

     @Override
     public Map<String, Object> syncParams(String token, String siteId, String mainTreeId, String number){
         List<BusiParamTransformerVoluntarilyVo> busiParamArresterVo = null;
         String eqType = arresterAssessmentResultMapper.findEqType(siteId,mainTreeId);
        if(Objects.equals(eqType, SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())) {
            busiParamArresterVo = arresterAssessmentResultMapper.findBusiBlqParam(siteId, mainTreeId, number);
        }
         busiParamTransformerService.saveOrUpdateBusiParamTransformer(token, busiParamArresterVo);
         return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", "");
     }

     @Override
     public Map<String, Object> findCalculateStep(String pid){
         BusiGisCaculateStepVo steps = arresterAssessmentResultMapper.findCalculateStep(pid);
         return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", steps);
     }

     @Override
     public List<TransformerLoadRate> findArresterLoadRate() {
        List<BusiSiteTree> mainTreeId = new ArrayList<>();
         List<BusiSiteTree> parentId = arresterAssessmentResultMapper.findParentId();
         for(BusiSiteTree busiSiteTree : parentId) {
             String id = busiSiteTree.getParentId();
             List<BusiSiteTree> part = arresterAssessmentResultMapper.findMainTreeId(id);
             if(part != null) {
                 for(int i = 0; i < part.size(); i++){
                     BusiSiteTree mainEquipmentId = part.get(i);
                     mainTreeId.add(mainEquipmentId);
                 }
             }
         }
         List<String> dateList = new ArrayList<>();
         List<TransformerLoadRate> results = new ArrayList<>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         for (int i = 0; i < 15; i++) {
             Date date = DateUtils.addDays(new Date(), -i);
             String formatDate = sdf.format(date);
             dateList.add(formatDate);
         }
         for (BusiSiteTree busiSiteTree : mainTreeId) {
             String mainEquipmentId = busiSiteTree.getId();
             for(int i = 0; i < dateList.size(); i++){
                 TransformerLoadRate transformerLoadRate = new TransformerLoadRate();
                 String date = dateList.get(i);
                 transformerLoadRate.setLoadTime(date);
                 TransformerLoadRate yValue = arresterAssessmentResultMapper.findAllLoadRate(mainEquipmentId , date);
                 transformerLoadRate.setMainTreeId(mainEquipmentId);
                 transformerLoadRate.setName(busiSiteTree.getName());
                 if(yValue != null) {
                     String value = yValue.getLoadRate();
                     transformerLoadRate.setLoadRate(value);
                     results.add(transformerLoadRate);
                 } else {
                     transformerLoadRate.setLoadRate("0.00");
                     results.add(transformerLoadRate);
                 }
             }
         }
         return results;
     }

}
