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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.bf.constants.PlanPowerCutConstants;
import com.hdkj.lt.bf.entity.FhzgDistCustomer;
import com.hdkj.lt.bf.entity.dto.FhzgDistCustomerCountDTO;
import com.hdkj.lt.bf.mapper.FhzgDistCustomerMapper;
import com.hdkj.lt.bf.service.FhzgDistCustomerService;
import com.hdkj.lt.core.bizms.modle.dto.FhzgDistCustomerDTO;
import com.hdkj.lt.core.bizms.modle.dto.FhzgFaultDistCustomerDTO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 配电用户信息表 服务实现类
 *
 * @author zhousc
 * @since 2025-05-07
 */
@Service
public class FhzgDistCustomerServiceImpl extends ServiceImpl<FhzgDistCustomerMapper, FhzgDistCustomer> implements FhzgDistCustomerService {

    @Override
    public Page<FhzgDistCustomer> getCustomerList(FhzgDistCustomerDTO dto) {
        Page<FhzgDistCustomer> page = new Page<>();
        page.setCurrent(dto.getPageNum());
        page.setSize(dto.getPageSize());
        List<FhzgDistCustomer> list = baseMapper.selectCustomerList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public Page<FhzgDistCustomer> getCustomerList(FhzgFaultDistCustomerDTO dto) {
        Page<FhzgDistCustomer> page = new Page<>();
        page.setCurrent(dto.getPageNum());
        page.setSize(dto.getPageSize());
        List<FhzgDistCustomer> list = baseMapper.selectCustomerListByFaultId(page, dto);
        return page.setRecords(list);
    }

    /**
     *
     * @author luorui
     * @date 2025/5/20 14:05
     * @discription 根据计划planId,统计停电区域光伏用户,低压/中压数量
     *
     **/
    @Override
    public FhzgDistCustomerCountDTO getDistCustomerCount(FhzgDistCustomerDTO dto) {
        FhzgDistCustomerCountDTO customerCountDTO = new FhzgDistCustomerCountDTO();
        List<FhzgDistCustomer> list = baseMapper.getDistCustomerCount(dto);
        if (CollUtil.isNotEmpty(list)) {
            Map<String, Integer> countMap = list.stream().collect(Collectors.toMap(FhzgDistCustomer::getUserType, FhzgDistCustomer::getCount));
            Integer mediumCustomerCount = countMap.get(PlanPowerCutConstants.UserType.MEDIUM);
            Integer lowCustomerCount = countMap.get(PlanPowerCutConstants.UserType.LOW);
            customerCountDTO.setMediumCustomerCount(Objects.isNull(mediumCustomerCount) ? 0 : mediumCustomerCount);
            customerCountDTO.setLowCustomerCount(Objects.isNull(lowCustomerCount) ? 0 : lowCustomerCount);
        } else {
            customerCountDTO.setMediumCustomerCount(0);
            customerCountDTO.setLowCustomerCount(0);
        }
        return customerCountDTO;
    }

    @Override
    public FhzgDistCustomerCountDTO getDistCustomerCount(FhzgFaultDistCustomerDTO dto) {
        FhzgDistCustomerCountDTO customerCountDTO = new FhzgDistCustomerCountDTO();
        List<FhzgDistCustomer> list = baseMapper.getDistCustomerCountByFaultId(dto);
        if (CollUtil.isNotEmpty(list)) {
            Map<String, Integer> countMap = list.stream().collect(Collectors.toMap(FhzgDistCustomer::getUserType, FhzgDistCustomer::getCount));
            Integer mediumCustomerCount = countMap.get(PlanPowerCutConstants.UserType.MEDIUM);
            Integer lowCustomerCount = countMap.get(PlanPowerCutConstants.UserType.LOW);
            customerCountDTO.setMediumCustomerCount(Objects.isNull(mediumCustomerCount) ? 0 : mediumCustomerCount);
            customerCountDTO.setLowCustomerCount(Objects.isNull(lowCustomerCount) ? 0 : lowCustomerCount);
        } else {
            customerCountDTO.setMediumCustomerCount(0);
            customerCountDTO.setLowCustomerCount(0);
        }
        return customerCountDTO;
    }


}
