package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.HotSpotTemperatureResultDao;
import com.huatek.busi.dto.HotSpotTemperatureResultDto;
import com.huatek.busi.entity.HotSpotTemperatureResult;
import com.huatek.busi.mapper.HotSpotTemperatureResultMapper;
import com.huatek.busi.service.HotSpotTemperatureResultService;
import com.huatek.busi.util.Constant;
import com.huatek.busi.util.TransformerFormulaCal;
import com.huatek.busi.vo.HotSpotTemperatureResultVo;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.util.uuid.Uuid;
import net.sf.json.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.huatek.busi.util.GisAssessUtilsNew.setStep;

/**
 * <p>
 * 热点温度计算结果表 服务实现类
 * </p>
 *
 * @author dong
 * @since 2023-07-19
 */
@Component
@Service(interfaceClass = HotSpotTemperatureResultService.class)
@Transactional(rollbackFor = Exception.class)
public class HotSpotTemperatureResultServiceImpl implements HotSpotTemperatureResultService {

    @Autowired
    private HotSpotTemperatureResultMapper hotSpotTemperatureResultMapper;

    @Autowired
    private HotSpotTemperatureResultDao hotSpotTemperatureResultDao;

    public static TransformerFormulaCal transformerFormulaCalInstance = TransformerFormulaCal.getInstance();

    @Override
    public PageInfo<HotSpotTemperatureResultVo> page(String siteId,String mainTreeId, LocalDate start, LocalDate end, Integer page, Integer limit, String type) {
        return PageHelper.startPage(page == null ? 1 :page , limit == null? 1 : limit)
                .doSelectPageInfo(() -> hotSpotTemperatureResultMapper.findByParams(siteId,mainTreeId,start,end, type));
    }

    @Override
    public boolean calculate(HotSpotTemperatureResultDto dto) {
        //环境温度
        double environmentTemperature = StringUtils.isEmpty(dto.getEnvironmentTemperature())?Constant.ENVIRONMENT_TEMPERATURE:Double.parseDouble(dto.getEnvironmentTemperature());
        double indexOfOil = StringUtils.isEmpty(dto.getIndexOfOil())?Constant.X:Double.parseDouble(dto.getIndexOfOil());
        double loadCoefficient =  StringUtils.isEmpty(dto.getLoadCoefficient())?Constant.K_NEW:Double.parseDouble(dto.getLoadCoefficient());
        if (StringUtils.isEmpty(dto.getTopOilTemperature())){
            throw new OwlException("额定电流下的顶层油温升不能为空");
        }
        double topOilTemperature = Double.parseDouble(dto.getTopOilTemperature());
        double lossRatio =  StringUtils.isEmpty(dto.getLossRatio())?Constant.R:Double.parseDouble(dto.getLossRatio());
        if (StringUtils.isEmpty(dto.getWindingTemperature())){
            throw new OwlException("绕组测温最高点温度不能为空");
        }
        double windingTemperature = Double.parseDouble(dto.getWindingTemperature());
        double windingIndex =  StringUtils.isEmpty(dto.getWindingIndex())?Constant.Y:Double.parseDouble(dto.getWindingIndex());

        Double result = transformerFormulaCalInstance.calcHotPointTemprature2(environmentTemperature,indexOfOil,loadCoefficient,topOilTemperature,lossRatio,windingTemperature,windingIndex);
        HotSpotTemperatureResult temperatureResult = new HotSpotTemperatureResult();
        temperatureResult.setId(Uuid.getUUID());
        temperatureResult.setSiteId(dto.getSiteId());
        temperatureResult.setMainTreeId(dto.getMainTreeId());
        temperatureResult.setEnvironmentTemperature(String.valueOf(environmentTemperature));
        temperatureResult.setIndexOfOil(String.valueOf(indexOfOil));
        temperatureResult.setLoadCoefficient(String.valueOf(loadCoefficient));
        temperatureResult.setTopOilTemperature(String.valueOf(topOilTemperature));
        temperatureResult.setLossRatio(String.valueOf(lossRatio));
        temperatureResult.setWindingTemperature(String.valueOf(windingTemperature));
        temperatureResult.setWindingIndex(String.valueOf(windingIndex));
        temperatureResult.setResult(String.valueOf(result));
        temperatureResult.setAssessTime(LocalDateTime.now());
        temperatureResult.setType("1"); //手动
        temperatureResult.setArithmeticType("1");
        JSONArray jsonArray = new JSONArray();
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("环境温度(θa)",environmentTemperature);
        jsonObject.put("额定电流下的顶层油温升(θor)",topOilTemperature);
        jsonObject.put("额定电流下的负载损耗和空载损耗之比(R)",lossRatio);
        jsonObject.put("负载系数(K)",loadCoefficient);
        jsonObject.put("油的指数(x)",indexOfOil);
        jsonObject.put("额定负载下热点对绕组顶部油的温差(Hgr)",windingTemperature-windingTemperature);
        jsonObject.put("绕组指数(y)",windingIndex);
        setStep(jsonArray, 1, "热点温度计算", jsonObject.toJSONString(), String.valueOf(result));
        temperatureResult.setCalculationStep(jsonArray.toString());
        int add = hotSpotTemperatureResultDao.add(temperatureResult);
        return add>0;
    }

    @Override
    public HotSpotTemperatureResultVo selectById(String id) {
        HotSpotTemperatureResult result = hotSpotTemperatureResultDao.get(id);
        HotSpotTemperatureResultVo resultVo = new HotSpotTemperatureResultVo();
        BeanUtils.copyProperties(result,resultVo);
        return resultVo;
    }
}
