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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.base.model.response.PageVO;
import com.hdkj.lt.bf.constants.WholeStationPowerCutConstants;
import com.hdkj.lt.bf.entity.dto.WholeStationPowerCutReqDTO;
import com.hdkj.lt.bf.entity.vo.wholestation.*;
import com.hdkj.lt.bf.mapper.FhzgWholeStationPowerCutTempMapper;
import com.hdkj.lt.bf.service.FhzgWholeStationPowerCutTempService;
import com.hdkj.lt.core.bizms.modle.po.wholeStation.FhzgWholeStationPowerCutTemp;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author userlrk
 * @description 针对表【fhzg_whole_station_power_cut_temp】的数据库操作Service实现
 * @createDate 2025-08-15 10:43:36
 */
@Service
public class FhzgWholeStationPowerCutTempServiceImpl extends ServiceImpl<FhzgWholeStationPowerCutTempMapper, FhzgWholeStationPowerCutTemp>
        implements FhzgWholeStationPowerCutTempService {

    /**
     *
     * @author luorui
     * @date 2025/8/15 11:32
     * @discription 分页查询变电站停电事件
     *
     **/
    @Override
    public PageVO<WholeStationPowerCutVO> pageList(WholeStationPowerCutReqDTO reqDTO) {
        LambdaQueryWrapper<FhzgWholeStationPowerCutTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(FhzgWholeStationPowerCutTemp::getPowerCut, FhzgWholeStationPowerCutTemp::getId);
        //查询全量数据
        List<FhzgWholeStationPowerCutTemp> list = baseMapper.selectList(wrapper);
        PageVO<WholeStationPowerCutVO> powerCutVOPageVO = new PageVO<>();
        if(CollUtil.isNotEmpty(list)){
            List<WholeStationPowerCutVO> records = new ArrayList<>();
            list.forEach(e->{
                String powerCut = e.getPowerCut();
                //将json 转换成对象
                WholeStationPowerCutVO wholeStationPowerCutVO = JSON.parseObject(powerCut, WholeStationPowerCutVO.class);
                wholeStationPowerCutVO.setId(e.getId());
                //模拟条件查询
                if (filterSchemeState(wholeStationPowerCutVO, reqDTO.getSchemeState()) &&
                        filterStationName(wholeStationPowerCutVO, reqDTO.getStationName()) &&
                        filterCountyCode(wholeStationPowerCutVO, reqDTO.getCountyCode()) &&
                        filterSchemeState(wholeStationPowerCutVO, reqDTO.getPlanStartTime(), reqDTO.getPlanEndTime())
                ) {
                    records.add(wholeStationPowerCutVO);
                }
            });
            //手动处理分页
            powerCutVOPageVO.setRecords(records);
            powerCutVOPageVO.setTotal(Long.valueOf(records.size()));
            powerCutVOPageVO.setPageSize(reqDTO.getPageSize());
            powerCutVOPageVO.setPageNum(reqDTO.getPageNum());
        }
        return powerCutVOPageVO;
    }

    //过滤条件 停电开始时间
    private boolean filterSchemeState(WholeStationPowerCutVO wholeStationPowerCutVO, LocalDateTime startTime, LocalDateTime endTime) {
        if (Objects.isNull(startTime) && Objects.isNull(endTime)) {
            //没有查询条件
            return true;
        } else {
            LocalDateTime planStartTime = wholeStationPowerCutVO.getPlanStartTime();
            if (Objects.isNull(planStartTime)) {
                return false;
            } else {
                boolean start = true;
                boolean end = true;
                if (!Objects.isNull(startTime)) {
                    start = planStartTime.isAfter(startTime);
                }
                if (!Objects.isNull(endTime)) {
                    end = planStartTime.isBefore(endTime);
                }
                //在停电区间范围内
                return start && end;
            }
        }
    }


    //过滤条件 方案状态
    private boolean filterSchemeState(WholeStationPowerCutVO wholeStationPowerCutVO, String schemeState) {
        if (StrUtil.isBlank(schemeState)) {
            //没有查询条件
            return true;
        } else {
            return StrUtil.equals(schemeState, wholeStationPowerCutVO.getSchemeState());
        }
    }

    //过滤条件 变电站名称
    private boolean filterStationName(WholeStationPowerCutVO wholeStationPowerCutVO, String stationName) {
        if (StrUtil.isBlank(stationName)) {
            //没有查询条件
            return true;
        } else {
            return StrUtil.contains(wholeStationPowerCutVO.getStationName(), stationName);
        }
    }

    //过滤条件 所属区县
    private boolean filterCountyCode(WholeStationPowerCutVO wholeStationPowerCutVO, Long countyCode) {
        if (Objects.isNull(countyCode)) {
            //没有查询条件
            return true;
        } else {
            if (Objects.isNull(wholeStationPowerCutVO.getCountyCode())) {
                return false;
            } else {
                return countyCode.compareTo(wholeStationPowerCutVO.getCountyCode()) == 0;
            }
        }
    }

    /**
     *
     * @author luorui
     * @date 2025/8/15 15:52
     * @discription 根据id 查询不同页面的数据
     *
     **/
    @Override
    public Object getDataById(String queryType, WholeStationPowerCutReqDTO reqDTO) {
        Long id = reqDTO.getId();
        Optional.ofNullable(id).orElseThrow(() -> new BusinessException("事件id为空"));
        LambdaQueryWrapper<FhzgWholeStationPowerCutTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FhzgWholeStationPowerCutTemp::getId, id);
        FhzgWholeStationPowerCutTemp fhzgWholeStationPowerCutTemp;
        Object result;
        switch (queryType) {
            case WholeStationPowerCutConstants.QueryType.TRANSFER_PATH:
                wrapper.select(FhzgWholeStationPowerCutTemp::getTransferPath ,FhzgWholeStationPowerCutTemp::getId);
                fhzgWholeStationPowerCutTemp = baseMapper.selectOne(wrapper);
                String transferPath = fhzgWholeStationPowerCutTemp.getTransferPath();
                WholeStationPowerCutTransferPathVO wholeStationPowerCutTransferPathVO = JSON.parseObject(transferPath, WholeStationPowerCutTransferPathVO.class);
                result = wholeStationPowerCutTransferPathVO;
                break;
            case WholeStationPowerCutConstants.QueryType.TRANSFER_USER:
                wrapper.select(FhzgWholeStationPowerCutTemp::getTransferUser ,FhzgWholeStationPowerCutTemp::getId);
                fhzgWholeStationPowerCutTemp = baseMapper.selectOne(wrapper);
                String transferUser = fhzgWholeStationPowerCutTemp.getTransferUser();
                WholeStationPowerCutTransferUserVO wholeStationPowerCutTransferUserVO = JSON.parseObject(transferUser, WholeStationPowerCutTransferUserVO.class);
                //页面条件查询 过滤数据
                filterTransferUser(wholeStationPowerCutTransferUserVO, reqDTO);
                result = wholeStationPowerCutTransferUserVO;
                break;
            case WholeStationPowerCutConstants.QueryType.TRANSFER_SCHEME:
                wrapper.select(FhzgWholeStationPowerCutTemp::getTransferScheme ,FhzgWholeStationPowerCutTemp::getId);
                fhzgWholeStationPowerCutTemp = baseMapper.selectOne(wrapper);
                String transferScheme = fhzgWholeStationPowerCutTemp.getTransferScheme();
                WholeStationPowerCutSchemeVO wholeStationPowerCutSchemeVO = JSON.parseObject(transferScheme, WholeStationPowerCutSchemeVO.class);
                result = wholeStationPowerCutSchemeVO;
                break;
            case WholeStationPowerCutConstants.QueryType.TRANSFER_SCORE_LIST:
                wrapper.select(FhzgWholeStationPowerCutTemp::getTransferScoreList ,FhzgWholeStationPowerCutTemp::getId);
                fhzgWholeStationPowerCutTemp = baseMapper.selectOne(wrapper);
                String transferScoreList = fhzgWholeStationPowerCutTemp.getTransferScoreList();
                WholeStationPowerCutSchemeScoreVO schemeScoreListVO = JSON.parseObject(transferScoreList, WholeStationPowerCutSchemeScoreVO.class);
                result = schemeScoreListVO;
                break;
            default:
                throw new BusinessException("查询类型枚举不正确");
        }
        return result;
    }

    private void filterTransferUser(WholeStationPowerCutTransferUserVO wholeStationPowerCutTransferUserVO, WholeStationPowerCutReqDTO reqDTO) {
        List<AwaitTransferUserVO> awaitTransferUserVOList = wholeStationPowerCutTransferUserVO.getAwaitTransferUserVOList();
        if (CollUtil.isNotEmpty(awaitTransferUserVOList)) {
            awaitTransferUserVOList = awaitTransferUserVOList.stream().filter(e ->
                    filterLineName(e, reqDTO.getLineName()) && filterUserName(e, reqDTO.getUserName())
            ).collect(Collectors.toList());
            wholeStationPowerCutTransferUserVO.setAwaitTransferUserVOList(awaitTransferUserVOList);
        }
    }

    //过滤条件 变电站名称
    private boolean filterLineName(AwaitTransferUserVO awaitTransferUserVO, String lineName) {
        if (StrUtil.isBlank(lineName)) {
            //没有查询条件
            return true;
        } else {
            return StrUtil.contains(awaitTransferUserVO.getLineName(), lineName);
        }
    }

    //过滤条件 变电站名称
    private boolean filterUserName(AwaitTransferUserVO awaitTransferUserVO, String userName) {
        if (StrUtil.isBlank(userName)) {
            //没有查询条件
            return true;
        } else {
            return StrUtil.contains(awaitTransferUserVO.getUserName(), userName);
        }
    }
}




