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.baseInfo.manager.CostCatalogManager;
import com.hotent.baseInfo.manager.CostInformationManager;
import com.hotent.baseInfo.model.CostCatalog;
import com.hotent.baseInfo.model.CostCatalogInformation;
import com.hotent.baseInfo.dao.CostCatalogInformationDao;
import com.hotent.baseInfo.manager.CostCatalogInformationManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.baseInfo.model.CostInformation;
import com.hotent.baseInfo.req.CostBaseInfoPageReq;
import com.hotent.baseInfo.resp.CostCatalogInformationPageResp;
import com.hotent.config.EipConfig;
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.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-29
 */
@Service
public class CostCatalogInformationManagerImpl extends BaseManagerImpl<CostCatalogInformationDao, CostCatalogInformation> implements CostCatalogInformationManager {


    @Autowired
    private CostCatalogManager costCatalogManager;


    @Autowired
    private CostInformationManager costInformationManager;


    @Override
    public CostCatalogInformation getDetail(String id) {
        CostCatalogInformation costCatalogInformation = this.get(id);


        return costCatalogInformation;
    }

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

    @Override
    @Transactional
    public void deleteByIds(List<String> ids) {
        List<CostCatalogInformation> lists = this.listByIds(ids);

        if (ids.size() != lists.size()) {
            throw new BaseException("需要取消关联的文件不存在");
        }

        super.removeByIds(ids);
    }

    @Override
    public IPage<CostCatalogInformationPageResp> pageList(CostBaseInfoPageReq req) {

        if (ObjectUtil.isEmpty(req.getCatalogId())) {
            throw new BaseException("目录id不能为空");
        }
        LambdaQueryWrapper<CostCatalogInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostCatalogInformation::getCatalogId, req.getCatalogId());



        IPage<CostCatalogInformation> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<CostCatalogInformation> respPage = this.page(page, queryWrapper);
        List<String> ids = respPage.getRecords().stream().map(CostCatalogInformation::getInformationId).collect(Collectors.toList());
        if(ObjectUtil.isEmpty(ids)){
            return new Page<>();
        }

        Map<String, CostInformation> costInformationMap = costInformationManager.listByIds(ids).stream().collect(Collectors.toMap(CostInformation::getId, Function.identity()));
        List<CostCatalogInformationPageResp> respList = respPage.getRecords().stream().map(item -> {
            CostCatalogInformationPageResp costCatalogInformationPageResp = new CostCatalogInformationPageResp();
            CostInformation costInformation = costInformationMap.get(item.getInformationId());
            costCatalogInformationPageResp.setId(item.getId());
            costCatalogInformationPageResp.setProjectName(costInformation.getTitle());
            costCatalogInformationPageResp.setDocumentNumber(costInformation.getDocumentNumber());
            costCatalogInformationPageResp.setUploadUrl(EipConfig.getImgUrl()+costInformation.getUploadUrl());
            return costCatalogInformationPageResp;
        }).collect(Collectors.toList());

        IPage<CostCatalogInformationPageResp> resp = new Page<>(req.getPageNum(), req.getPageSize(),respPage.getTotal());
        resp.setRecords(respList);
        return resp;
    }

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

    @Override
    public void create(CostCatalogInformation costCatalogInformation) {
        if (ObjectUtil.isEmpty(costCatalogInformation.getCatalogId())) {
            throw new BaseException("监审目录不能为空");
        }


        if (ObjectUtil.isEmpty(costCatalogInformation.getInformationId())) {
            throw new BaseException("政策文件不能为空");
        }

        CostCatalog costCatalog = costCatalogManager.get(costCatalogInformation.getCatalogId());

        if (costCatalog == null) {
            throw new BaseException("监审目录不存在");
        }
        CostInformation costInformation = costInformationManager.get(costCatalogInformation.getInformationId());
        if (costInformation == null) {
            throw new BaseException("政策文件不存在");
        }
        super.create(costCatalogInformation);
    }


}
