package com.hdkj.lt.bf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.entity.UsersPage;
import com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeUsersDTO;
import com.hdkj.lt.bf.entity.dto.LoadSwitchLoseUsersDTO;
import com.hdkj.lt.bf.mapper.FhzgPlanTransferSchemeUsersMapper;
import com.hdkj.lt.bf.service.FhzgPlanTransferSchemeUsersService;
import com.hdkj.lt.core.bizms.modle.dto.FhzgPlanTransferSchemeUsersReqDTO;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferSchemeUsers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * @author luorui
 * @date 2025/5/17 11:50
 * 可靠性校验，损失用户信息查询
 *
 */
@Service
@Slf4j
public class FhzgPlanTransferSchemeUsersServiceImpl extends ServiceImpl<FhzgPlanTransferSchemeUsersMapper, FhzgPlanTransferSchemeUsers> implements FhzgPlanTransferSchemeUsersService {

    @Autowired
    FhzgPlanTransferSchemeUsersMapper fhzgPlanTransferSchemeUsersMapper;


    /**
     *
     * @author luorui
     * @date 2025/5/17 11:55
     * @discription 损失用户查询
     *
     **/
    @Override
    public UsersPage<FhzgPlanTransferSchemeUsersDTO> getFhzgPlanTransferSchemeUsersByIdPage(FhzgPlanTransferSchemeUsersReqDTO fhzgPlanTransferSchemeUsersReqDTO) {
        if (Objects.isNull(fhzgPlanTransferSchemeUsersReqDTO.getId())) {
            throw new BusinessException("组合方案id为空");
        }
        if (Objects.isNull(fhzgPlanTransferSchemeUsersReqDTO.getPageNum())) {
            throw new BusinessException("分页页码为空");
        }
        if (Objects.isNull(fhzgPlanTransferSchemeUsersReqDTO.getPageSize())) {
            throw new BusinessException("分页长度为空");
        }
        if (StrUtil.isBlank(fhzgPlanTransferSchemeUsersReqDTO.getQueryType())) {
            throw new BusinessException("查询类型为空");
        }
        Page<FhzgPlanTransferSchemeUsers> page = new Page(fhzgPlanTransferSchemeUsersReqDTO.getPageNum(), fhzgPlanTransferSchemeUsersReqDTO.getPageSize());
        Page<FhzgPlanTransferSchemeUsersDTO> fhzgPlanTransferSchemeUsersDTOList = null;
        if ("6".equals(fhzgPlanTransferSchemeUsersReqDTO.getQueryType())) {
            //配变低电压
            fhzgPlanTransferSchemeUsersDTOList = fhzgPlanTransferSchemeUsersMapper.getDistributionTransformerLowVoltage(page, fhzgPlanTransferSchemeUsersReqDTO);
            fhzgPlanTransferSchemeUsersDTOList.getRecords().forEach(i -> i.setVoltageLimitViolationMagnitude(i.getAfterTransferVoltage().subtract(new BigDecimal("10")).multiply(new BigDecimal("100")).divide(new BigDecimal("10"), RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_UP)));
        } else {
           fhzgPlanTransferSchemeUsersDTOList = fhzgPlanTransferSchemeUsersMapper.getFhzgPlanTransferSchemeUsersByIdPage(page, fhzgPlanTransferSchemeUsersReqDTO);
        }
        UsersPage<FhzgPlanTransferSchemeUsersDTO> result = new UsersPage<>();
        BeanUtil.copyProperties(fhzgPlanTransferSchemeUsersDTOList,result);

//        FhzgPlanTransferSchemeUsers usersPageSum = fhzgPlanTransferSchemeUsersMapper.getSum(fhzgPlanTransferSchemeUsersReqDTO);
//        result.setTotalLossPowers(usersPageSum.getTotalLossPowers());
//        result.setTotalLossCustHours(usersPageSum.getTotalLossCustHours());
        return result;

    }

    /**
     *
     * @author luorui
     * @date 2025/6/6 17:18
     * @discription 统计方案下，损失用户数据，聚合到对应的负荷开关下
     *
     **/
    @Override
    public List<LoadSwitchLoseUsersDTO> countLoadSwitchLoseUsersByScheme(Long schemeId) {
        if (Objects.isNull(schemeId)) {
            throw new BusinessException("方案编码为空");
        }
        List<LoadSwitchLoseUsersDTO> result = baseMapper.countLoadSwitchLoseUsersByScheme(schemeId);
        return result;
    }
}
