package com.hotent.baseInfo.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.base.exception.BaseException;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.baseInfo.dao.CostCatalogUnitDao;
import com.hotent.baseInfo.manager.CostCatalogUnitManager;
import com.hotent.baseInfo.model.CostCatalogUnit;
import com.hotent.baseInfo.req.CostBaseInfoPageReq;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateManager;
import com.hotent.surveyinfo.model.CostSurveyFdTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 成本监审目录-提交资料清单 服务实现类
 *
 * @author 超级管理员
 * @company 山西清众科技股份有限公司
 * @since 2025-09-29
 */
@Service
public class CostCatalogUnitManagerImpl extends BaseManagerImpl<CostCatalogUnitDao, CostCatalogUnit> implements CostCatalogUnitManager {


    @Autowired
    private CostSurveyFdTemplateManager costSurveyFdTemplateManager;

    @Override
    public CostCatalogUnit getDetail(String id) {
        CostCatalogUnit costCatalogUnit = this.get(id);


        return costCatalogUnit;
    }

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

    @Override
    public IPage<CostCatalogUnit> pageList(CostBaseInfoPageReq req) {
        if (ObjectUtil.isEmpty(req.getCatalogId())) {
            throw new BaseException("目录id不能为空");
        }

        LambdaQueryWrapper<CostCatalogUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostCatalogUnit::getCatalogId, req.getCatalogId());
        queryWrapper.orderByAsc(CostCatalogUnit::getOrderNum);
        IPage<CostCatalogUnit> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<CostCatalogUnit> pageList = this.page(page, queryWrapper);
        if (ObjectUtil.isEmpty(pageList.getRecords())) {
            return new Page<>();
        }

        List<String> ids = pageList.getRecords().stream().filter(u -> "3".equals(u.getFormatRequired())).map(CostCatalogUnit::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<CostCatalogUnit> records = pageList.getRecords();
        records.forEach(costCatalogUnit -> {
            if ("3".equals(costCatalogUnit.getFormatRequired())) {
                if(fdTemplateMap!=null) {
                    if (fdTemplateMap.containsKey(costCatalogUnit.getTemplateId())) {
                        CostSurveyFdTemplate costSurveyFdTemplate = fdTemplateMap.get(costCatalogUnit.getTemplateId());
                        costCatalogUnit.setSurveyTemplateName(costSurveyFdTemplate.getSurveyTemplateName());
                    }
                }else{
                    costCatalogUnit.setSurveyTemplateName("-");
                }
            }else{
                costCatalogUnit.setSurveyTemplateName("-");
            }
        });

        return pageList;
    }

    @Override
    public List<CostCatalogUnit> getListByCatalogId(String catalogId) {
        LambdaQueryWrapper<CostCatalogUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostCatalogUnit::getCatalogId, catalogId);
        return this.list(queryWrapper);
    }
}
