package org.dromara.progress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.facility.domain.FacBoxTransformer;
import org.dromara.facility.domain.FacInverter;
import org.dromara.facility.domain.FacPhotovoltaicPanel;
import org.dromara.facility.domain.enums.FacFinishStatusEnum;
import org.dromara.facility.service.IFacBoxTransformerService;
import org.dromara.facility.service.IFacInverterService;
import org.dromara.facility.service.IFacPhotovoltaicPanelService;
import org.dromara.progress.constant.PgsProgressCategoryConstant;
import org.dromara.progress.domain.PgsProgressCategory;
import org.dromara.progress.domain.PgsProgressPlan;
import org.dromara.progress.domain.PgsProgressPlanDetail;
import org.dromara.progress.domain.dto.progressplandetail.PgsProgressPlanDetailQueryReq;
import org.dromara.progress.domain.dto.progressplandetail.PgsProgressPlanDetailUpdateReq;
import org.dromara.progress.domain.vo.progressplandetail.PgsProgressPlanDetailFacilityVo;
import org.dromara.progress.domain.vo.progressplandetail.PgsProgressPlanDetailNumVo;
import org.dromara.progress.domain.vo.progressplandetail.PgsProgressPlanDetailVo;
import org.dromara.progress.mapper.PgsProgressPlanDetailMapper;
import org.dromara.progress.service.IPgsProgressCategoryService;
import org.dromara.progress.service.IPgsProgressPlanDetailService;
import org.dromara.progress.service.IPgsProgressPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 进度计划详情Service业务层处理
 *
 * @author lcj
 * @date 2025-05-27
 */
@Service
public class PgsProgressPlanDetailServiceImpl extends ServiceImpl<PgsProgressPlanDetailMapper, PgsProgressPlanDetail>
    implements IPgsProgressPlanDetailService {

    @Lazy
    @Resource
    private IPgsProgressPlanService progressPlanService;

    @Resource
    private IFacBoxTransformerService boxTransformerService;

    @Resource
    private IFacInverterService inverterService;

    @Resource
    private IFacPhotovoltaicPanelService photovoltaicPanelService;

    @Resource
    private IPgsProgressCategoryService progressCategoryService;

    /**
     * 查询进度计划详情
     *
     * @param id 主键
     * @return 进度计划详情
     */
    @Override
    public PgsProgressPlanDetailVo queryById(Long id) {
        PgsProgressPlanDetail progressPlanDetail = this.getById(id);
        if (progressPlanDetail == null) {
            throw new ServiceException("进度计划详情信息不存在", HttpStatus.NOT_FOUND);
        }
        return this.getVo(progressPlanDetail);
    }

    /**
     * 分页查询进度计划详情列表
     *
     * @param req       查询条件
     * @param pageQuery 分页参数
     * @return 进度计划详情分页列表
     */
    @Override
    public TableDataInfo<PgsProgressPlanDetailVo> queryPageList(PgsProgressPlanDetailQueryReq req, PageQuery pageQuery) {
        Page<PgsProgressPlanDetail> result = this.page(pageQuery.build(), this.buildQueryWrapper(req));
        return TableDataInfo.build(this.getVoPage(result));
    }

    /**
     * 查询符合条件的进度计划详情列表
     *
     * @param req 查询条件
     * @return 进度计划详情列表
     */
    @Override
    public List<PgsProgressPlanDetailVo> queryList(PgsProgressPlanDetailQueryReq req) {
        List<PgsProgressPlanDetail> list = this.list(this.buildQueryWrapper(req));
        return list.stream().map(this::getVo).toList();
    }

    /**
     * 分页查询进度计划详情设施列表
     *
     * @param id        主键
     * @param pageQuery 分页参数
     * @return 进度计划详情设施分页列表
     */
    @Override
    public TableDataInfo<PgsProgressPlanDetailFacilityVo> queryFacilityById(Long id, PageQuery pageQuery) {
        PgsProgressPlanDetail progressPlanDetail = this.getById(id);
        if (progressPlanDetail == null) {
            throw new ServiceException("进度计划详情信息不存在", HttpStatus.NOT_FOUND);
        }
        PgsProgressPlan progressPlan = progressPlanService.getById(progressPlanDetail.getProgressPlanId());
        if (progressPlan == null) {
            throw new ServiceException("进度计划信息不存在", HttpStatus.NOT_FOUND);
        }
        Long progressCategoryId = progressPlan.getProgressCategoryId();
        Long projectId = progressPlan.getProjectId();
        Long matrixId = progressPlan.getMatrixId();
        PgsProgressCategory progressCategory = progressCategoryService.getById(progressCategoryId);
        if (progressCategory == null) {
            throw new ServiceException("进度计划类别信息不存在", HttpStatus.NOT_FOUND);
        }
        String workType = progressCategory.getWorkType();
        Page<PgsProgressPlanDetailFacilityVo> resultPage = pageQuery.build();
        if (PgsProgressCategoryConstant.PHOTOVOLTAIC_PANEL_PROGRESS_CATEGORY_WORK_TYPE.contains(workType)) {
            LambdaQueryWrapper<FacPhotovoltaicPanel> lqw = new LambdaQueryWrapper<>();
            lqw.eq(FacPhotovoltaicPanel::getProjectId, projectId);
            lqw.eq(FacPhotovoltaicPanel::getMatrixId, matrixId);
            lqw.eq(FacPhotovoltaicPanel::getProgressCategoryId, progressCategoryId);
            lqw.eq(FacPhotovoltaicPanel::getFinishDate, progressPlanDetail.getDate());
            lqw.eq(FacPhotovoltaicPanel::getFinishType, FacFinishStatusEnum.FINISH.getValue());
            Page<FacPhotovoltaicPanel> result = photovoltaicPanelService.page(pageQuery.build(), lqw);
            List<FacPhotovoltaicPanel> records = result.getRecords();
            List<PgsProgressPlanDetailFacilityVo> facilityVoList = records.stream().map(photovoltaicPanel -> {
                PgsProgressPlanDetailFacilityVo facilityVo = new PgsProgressPlanDetailFacilityVo();
                facilityVo.setId(photovoltaicPanel.getId());
                facilityVo.setName(photovoltaicPanel.getName());
                facilityVo.setFinishType(photovoltaicPanel.getFinishType());
                return facilityVo;
            }).toList();
            resultPage.setRecords(facilityVoList);
        } else if (PgsProgressCategoryConstant.INVERTER_PROGRESS_CATEGORY_WORK_TYPE.contains(workType)) {
            LambdaQueryWrapper<FacInverter> lqw = new LambdaQueryWrapper<>();
            lqw.eq(FacInverter::getProjectId, projectId);
            lqw.eq(FacInverter::getMatrixId, matrixId);
            lqw.eq(FacInverter::getProgressCategoryId, progressCategoryId);
            lqw.eq(FacInverter::getFinishDate, progressPlanDetail.getDate());
            lqw.eq(FacInverter::getFinishType, FacFinishStatusEnum.FINISH.getValue());
            Page<FacInverter> result = inverterService.page(pageQuery.build(), lqw);
            List<FacInverter> records = result.getRecords();
            List<PgsProgressPlanDetailFacilityVo> facilityVoList = records.stream().map(inverter -> {
                PgsProgressPlanDetailFacilityVo facilityVo = new PgsProgressPlanDetailFacilityVo();
                facilityVo.setId(inverter.getId());
                facilityVo.setName(inverter.getName());
                facilityVo.setFinishType(inverter.getFinishType());
                return facilityVo;
            }).toList();
            resultPage.setRecords(facilityVoList);
        } else if (PgsProgressCategoryConstant.BOX_TRANSFORMER_PROGRESS_CATEGORY_WORK_TYPE.contains(workType)) {
            LambdaQueryWrapper<FacBoxTransformer> lqw = new LambdaQueryWrapper<>();
            lqw.eq(FacBoxTransformer::getProjectId, projectId);
            lqw.eq(FacBoxTransformer::getMatrixId, matrixId);
            lqw.eq(FacBoxTransformer::getProgressCategoryId, progressCategoryId);
            lqw.eq(FacBoxTransformer::getFinishDate, progressPlanDetail.getDate());
            lqw.eq(FacBoxTransformer::getFinishType, FacFinishStatusEnum.FINISH.getValue());
            Page<FacBoxTransformer> result = boxTransformerService.page(pageQuery.build(), lqw);
            List<FacBoxTransformer> records = result.getRecords();
            List<PgsProgressPlanDetailFacilityVo> facilityVoList = records.stream().map(boxTransformer -> {
                PgsProgressPlanDetailFacilityVo facilityVo = new PgsProgressPlanDetailFacilityVo();
                facilityVo.setId(boxTransformer.getId());
                facilityVo.setName(boxTransformer.getName());
                facilityVo.setFinishType(boxTransformer.getFinishType());
                return facilityVo;
            }).toList();
            resultPage.setRecords(facilityVoList);
        } else {
            throw new ServiceException("未知进度计划类别工作类型", HttpStatus.BAD_REQUEST);
        }
        return TableDataInfo.build(resultPage);
    }

    /**
     * 修改进度计划详情
     *
     * @param req 进度计划详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PgsProgressPlanDetailUpdateReq req) {
        return null;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PgsProgressPlanDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 获取进度计划详情视图对象
     *
     * @param progressPlanDetail 进度计划详情对象
     * @return 进度计划详情视图对象
     */
    @Override
    public PgsProgressPlanDetailVo getVo(PgsProgressPlanDetail progressPlanDetail) {
        return null;
    }

    /**
     * 获取进度计划详情视图对象列表
     *
     * @param progressPlanDetailList 进度计划详情对象列表
     * @return 进度计划详情视图对象列表
     */
    @Override
    public List<PgsProgressPlanDetailNumVo> getNumVoList(List<PgsProgressPlanDetail> progressPlanDetailList) {
        return progressPlanDetailList.stream().map(progressPlanDetail -> {
            PgsProgressPlanDetailNumVo progressPlanDetailNumVo = new PgsProgressPlanDetailNumVo();
            BeanUtils.copyProperties(progressPlanDetail, progressPlanDetailNumVo);
            return progressPlanDetailNumVo;
        }).toList();
    }

    /**
     * 获取进度计划详情查询条件封装
     *
     * @param req 查询条件
     * @return 查询条件封装
     */
    @Override
    public LambdaQueryWrapper<PgsProgressPlanDetail> buildQueryWrapper(PgsProgressPlanDetailQueryReq req) {
        LambdaQueryWrapper<PgsProgressPlanDetail> lqw = new LambdaQueryWrapper<>();
        if (req == null) {
            return lqw;
        }
        Long projectId = req.getProjectId();
        Long progressPlanId = req.getProgressPlanId();
        Date date = req.getDate();
        lqw.eq(ObjectUtils.isNotEmpty(projectId), PgsProgressPlanDetail::getProjectId, projectId);
        lqw.eq(ObjectUtils.isNotEmpty(progressPlanId), PgsProgressPlanDetail::getProgressPlanId, progressPlanId);
        lqw.eq(ObjectUtils.isNotEmpty(date), PgsProgressPlanDetail::getDate, date);
        return lqw;
    }

    /**
     * 获取进度计划详情分页对象视图
     *
     * @param progressPlanDetailPage 进度计划详情分页对象
     * @return 进度计划详情分页对象视图
     */
    @Override
    public Page<PgsProgressPlanDetailVo> getVoPage(Page<PgsProgressPlanDetail> progressPlanDetailPage) {
        return null;
    }
}
