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

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dto.cablecurrent.CableCurrentPageDto;
import com.huatek.busi.dto.cablecurrent.CableCurrentResultDto;
import com.huatek.busi.entity.BusiCableCurrentResult;
import com.huatek.busi.mapper.CableCurrentResultMapper;
import com.huatek.busi.service.cablecurrent.ICableCurrentResultService;
import com.huatek.busi.util.CableCurrentCal;
import com.huatek.busi.vo.cablecurrent.CableCurrentPageVo;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;


@Slf4j
@Service(interfaceClass = ICableCurrentResultService.class)
@Transactional(rollbackFor = Exception.class)
public class CableCurrentResultServiceImpl extends BaseDao<BusiCableCurrentResult, String> implements ICableCurrentResultService {

    @Autowired
    CableCurrentResultMapper busiCableCurrentResultMapper;
    @Override
    public boolean calculateByInputItemDate(String token, CableCurrentResultDto cableCurrentResultDto) {

        //计算Δθ
        BigDecimal tempDiff=cableCurrentResultDto.getMeaTemp().subtract(cableCurrentResultDto.getAmbientTemp());
        //计算R1
        BigDecimal r1= CableCurrentCal.calR1(cableCurrentResultDto.getDiameter(), cableCurrentResultDto.getConductivity(),cableCurrentResultDto.getResistanceTempCoefficient());
        //计算ys
        BigDecimal ys=CableCurrentCal.calYs(r1);
        //计算R
        BigDecimal r=CableCurrentCal.calcR(r1,ys);
        //计算T1
        BigDecimal t1=CableCurrentCal.calcT1(cableCurrentResultDto.getDiameter(),cableCurrentResultDto.getInsulationThick());
        BigDecimal t2=cableCurrentResultDto.getT2();
        BigDecimal t3=cableCurrentResultDto.getT3();
        BigDecimal t4=null;
        if (StringUtils.isNotEmpty(cableCurrentResultDto.getT4())) {
            t4=new BigDecimal(cableCurrentResultDto.getT4());
        }else{
             t4=CableCurrentCal.calcT4(cableCurrentResultDto.getExternalDiameterDC());
        }

        BigDecimal cc=CableCurrentCal.calcC(cableCurrentResultDto.getDielectricConstant(),cableCurrentResultDto.getExternalDiameterCI(),cableCurrentResultDto.getExternalDiameterCC());
        BigDecimal wd=CableCurrentCal.calcWd(cableCurrentResultDto.getRatedVoltage(),cableCurrentResultDto.getDielectricLossFactor(),cc);
        BigDecimal i=CableCurrentCal.calcCurrent(cableCurrentResultDto.getCablesNumber(),r,wd,t1,t2,t3,t4,tempDiff);
        BusiCableCurrentResult busiCableCurrentResult=new BusiCableCurrentResult();
        BeanUtils.copyProperties(cableCurrentResultDto, busiCableCurrentResult);
        if("1".equals(busiCableCurrentResult.getFlag())){
            busiCableCurrentResult.setPumpingCurrent(i);
        }else{
            busiCableCurrentResult.setElectricPowerCurrent(i);
        }
        busiCableCurrentResult.setWd(wd);
        busiCableCurrentResult.setTempDiff(tempDiff);
        busiCableCurrentResult.setT1(t1);
        busiCableCurrentResult.setT4(t4);
        busiCableCurrentResult.setR(r);
        busiCableCurrentResult.setΛ1(CableCurrentCal.λ1);
        busiCableCurrentResult.setΛ2(CableCurrentCal.λ2);
        busiCableCurrentResult.setCreateTime(LocalDateTime.now());
        busiCableCurrentResult.setId(Uuid.getUUID());
        busiCableCurrentResult.setCc(cc);
        busiCableCurrentResult.setName(cableCurrentResultDto.getName());
        //保存入库
        return this.add(busiCableCurrentResult)>0;
    }

    @Override
    public BusiCableCurrentResult calculateByInfo(String id) {
        return this.get(id);
    }

    @Override
    public PageInfo<CableCurrentPageVo> queryList(CableCurrentPageDto cableCurrentPageDto) {
        return PageHelper.startPage(cableCurrentPageDto.getPage(), cableCurrentPageDto.getLimit())
                .doSelectPageInfo(() -> busiCableCurrentResultMapper.selectListPage(cableCurrentPageDto));
    }


}
