package com.hotent.project.manager.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.baseInfo.model.CostCatalogUnit;
import com.hotent.project.manager.CostProjectApprovalManager;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectDocument;
import com.hotent.project.model.CostProjectMaterial;
import com.hotent.project.dao.CostProjectMaterialDao;
import com.hotent.project.manager.CostProjectMaterialManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.project.req.CostProjectBasePageReq;
import com.hotent.req.PageReq;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateManager;
import com.hotent.surveyinfo.model.CostSurveyFdTemplate;
import com.hotent.uc.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.hotent.base.util.BeanUtils;


/**
 * 报送资料要求 服务实现类
 *
 * @author 超级管理员
 * @company 山西清众科技股份有限公司
 * @since 2025-09-26
 */
@Service
public class CostProjectMaterialManagerImpl extends BaseManagerImpl<CostProjectMaterialDao, CostProjectMaterial> implements CostProjectMaterialManager {



    @Autowired
    private CostSurveyFdTemplateManager costSurveyFdTemplateManager;

    @Autowired
    private CostProjectApprovalManager costProjectApprovalManager;




    @Override
    public CostProjectMaterial getDetail(String id) {
        CostProjectMaterial costProjectMaterial = this.get(id);


        return costProjectMaterial;
    }

    @Override
    @Transactional
    public void createOrUpdate(CostProjectMaterial costProjectMaterial) {
        //新建或更新
        this.saveOrUpdate(costProjectMaterial);
    }

    @Override
    public boolean save(CostProjectMaterial entity){
        String projectId = entity.getProjectId();
        if(ObjectUtil.isEmpty(projectId)){
            throw new BaseException("项目id不能为空");
        }
        CostProjectApproval costProjectApproval = costProjectApprovalManager.get(projectId);
        if (ObjectUtil.isEmpty(costProjectApproval)) {
            throw new BaseException("项目不存在");
        }
        entity.setCatalogId(costProjectApproval.getCatalogId());
        return super.save(entity);
    }



    @Override
    public IPage<CostProjectMaterial> pageList(CostProjectBasePageReq req) {
        LambdaQueryWrapper<CostProjectMaterial> qw = new LambdaQueryWrapper<>();
        qw.eq(CostProjectMaterial::getProjectId, req.getProjectId());
        IPage<CostProjectMaterial> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<CostProjectMaterial> pageList = this.page(page, qw);

        List<String> ids = pageList.getRecords().stream().filter(u -> "3".equals(u.getFormatRequired())).map(CostProjectMaterial::getTemplateId).distinct().collect(Collectors.toList());

        Map<String, CostSurveyFdTemplate> fdTemplateMap;
        if(!ObjectUtil.isEmpty(ids)) {
            List<CostSurveyFdTemplate> costSurveyFdTemplates = this.costSurveyFdTemplateManager.listByIds(ids);
            fdTemplateMap = costSurveyFdTemplates.stream().collect(Collectors.toMap(CostSurveyFdTemplate::getSurveyTemplateId, Function.identity()));
        } else {
            fdTemplateMap = null;
        }


        List<CostProjectMaterial> records = pageList.getRecords();
        records.forEach(costCatalogUnit -> {
            if ("3".equals(costCatalogUnit.getFormatRequired())) {
                if(fdTemplateMap!=null) {
                    CostSurveyFdTemplate costSurveyFdTemplate = fdTemplateMap.get(costCatalogUnit.getTemplateId());
                    costCatalogUnit.setSurveyTemplateName(costSurveyFdTemplate.getSurveyTemplateName());
                }else{
                    costCatalogUnit.setSurveyTemplateName("-");
                }
            }else{
                costCatalogUnit.setSurveyTemplateName("-");
            }
        });
        return pageList;
    }

    @Override
    public List<CostProjectMaterial> getListByProjectId(String projectId) {
        LambdaQueryWrapper<CostProjectMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostProjectMaterial::getProjectId, projectId);
        List<CostProjectMaterial> list = this.list(queryWrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }
}
