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.manager.impl.BaseManagerImpl;
import com.hotent.baseInfo.dao.CostCatalogSurveyDao;
import com.hotent.baseInfo.manager.CostCatalogSurveyManager;
import com.hotent.baseInfo.model.CostCatalogSurvey;
import com.hotent.baseInfo.req.CostBaseInfoPageReq;
import com.hotent.baseInfo.resp.CostCatalogSurveyPageResp;
import com.hotent.req.IdReq;
import com.hotent.surveyinfo.manager.CostSurveyTemplateManager;
import com.hotent.surveyinfo.model.CostSurveyTemplate;
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 java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 监审目录-成本调查表 服务实现类
 *
 * @author 超级管理员
 * @company 山西清众科技股份有限公司
 * @since 2025-09-29
 */
@Service
public class CostCatalogSurveyManagerImpl extends BaseManagerImpl<CostCatalogSurveyDao, CostCatalogSurvey> implements CostCatalogSurveyManager {


    @Autowired
    private CostSurveyTemplateManager costSurveyTemplateManager;

    @Override
    public CostCatalogSurvey getDetail(String id) {
        CostCatalogSurvey costCatalogSurvey = this.get(id);


        return costCatalogSurvey;
    }

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

    @Override
    public IPage<CostCatalogSurveyPageResp> pageList(CostBaseInfoPageReq req) {
        if (ObjectUtil.isEmpty(req.getCatalogId())) {
            throw new BaseException("目录id不能为空");
        }
        LambdaQueryWrapper<CostCatalogSurvey> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostCatalogSurvey::getCatalogId, req.getCatalogId());
        queryWrapper.orderByDesc(CostCatalogSurvey::getCreateTime);

        IPage<CostCatalogSurvey> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<CostCatalogSurvey> respPage = this.page(page, queryWrapper);
        List<CostCatalogSurvey> records = respPage.getRecords();
        if(ObjectUtil.isEmpty(records)){
            return new Page<>();
        }
        List<CostCatalogSurveyPageResp> respList = new ArrayList<>();
        records.forEach(it ->{
            CostSurveyTemplate costSurveyTemplate = costSurveyTemplateManager.getById(it.getSurveyId());
            CostCatalogSurveyPageResp CostCatalogSurveyPageResp = new CostCatalogSurveyPageResp();
            if (costSurveyTemplate != null) {
                CostCatalogSurveyPageResp.setSurveyId(costSurveyTemplate.getSurveyTemplateId().toString());
                LocalDateTime createTime = costSurveyTemplate.getCreateTime();
                CostCatalogSurveyPageResp.setYear(String.valueOf(createTime.getYear()));
                CostCatalogSurveyPageResp.setStatus(costSurveyTemplate.getStatus());
                CostCatalogSurveyPageResp.setTemplateType(costSurveyTemplate.getTemplateType());
                CostCatalogSurveyPageResp.setSurveyTemplateName(costSurveyTemplate.getSurveyTemplateName());
            }
            CostCatalogSurveyPageResp.setId(it.getId());
            respList.add(CostCatalogSurveyPageResp);
        });

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

    //取消关联调查表
    @Override
    public void cleanByIds(IdReq req) {
        List<CostCatalogSurvey> costCatalogSurveys = this.listByIds(req.getIds());
        costCatalogSurveys.forEach(item -> {
            item.setSurveyId(null);
        });
        this.updateBatchById(costCatalogSurveys);

    }

}
