package com.hotent.baseInfo.manager.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hotent.base.util.StringUtil;
import com.hotent.baseInfo.manager.AuditedUnitManager;
import com.hotent.baseInfo.model.AuditedUnit;
import com.hotent.baseInfo.model.CostDocumentTemplateFile;
import com.hotent.baseInfo.dao.CostDocumentTemplateFileDao;
import com.hotent.baseInfo.manager.CostDocumentTemplateFileManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.project.manager.CostProjectTaskManager;
import com.hotent.project.model.CostProjectDocumentFile;
import com.hotent.project.model.CostProjectTask;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.exception.BaseException;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.User;
import com.hotent.uc.util.ContextUtil;
import com.hotent.util.CostDataUtil;
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.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.hotent.base.util.BeanUtils;


/**
 * 监审文书文件内容表 服务实现类
 *
 * @author 超级管理员
 * @company 山西清众科技股份有限公司
 * @since 2025-10-14
 */
@Service
public class CostDocumentTemplateFileManagerImpl extends BaseManagerImpl<CostDocumentTemplateFileDao, CostDocumentTemplateFile> implements CostDocumentTemplateFileManager {

    @Autowired
    private AuditedUnitManager auditedUnitManager;

    @Autowired
    private UserManager userService;

    @Autowired
    private CostProjectTaskManager costProjectTaskManager;

    @Autowired
    private CostDocumentTemplateFileManager costTemplateFileManager;

    @Override
    public CostDocumentTemplateFile getDetail(String id) {
        CostDocumentTemplateFile costDocumentTemplateFile = this.get(id);


        return costDocumentTemplateFile;
    }

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



    @Override
    public List<CostDocumentTemplateFile> queryByDocumentId(String documentId,String whereValue,String unitId) {
        List<CostDocumentTemplateFile> documentFileList = this.getDocumentFileList(documentId);
        QueryWrapper<CostDocumentTemplateFile> eq = new QueryWrapper<CostDocumentTemplateFile>().eq("document_id", documentId);
        for (CostDocumentTemplateFile costProjectDocumentFile : documentFileList) {
            try {
                if (StringUtil.isEmpty(costProjectDocumentFile.getWhereName())
                        && StringUtil.isEmpty(costProjectDocumentFile.getWhereValue())
                        && StringUtil.isNotEmpty(costProjectDocumentFile.getTableName())) {
                    String tableName = costProjectDocumentFile.getTableName();
                    List<CostProjectTask> taskByProjectId = costProjectTaskManager.findTaskByProjectId(whereValue,unitId);
                    if (taskByProjectId!=null && !taskByProjectId.isEmpty()) {
                        String replace = tableName.replace("?", "'" + taskByProjectId.get(0).getId() + "'");
                        String  colValue= costTemplateFileManager.getColValue2(replace);
                        costProjectDocumentFile.setDataValue(CostDataUtil.convertToChineseDate(colValue));
                    }

                }else  if (StringUtil.isNotEmpty(costProjectDocumentFile.getTableName())
                &&StringUtil.isNotEmpty(costProjectDocumentFile.getWhereName())){
                    if(StringUtil.isEmpty(costProjectDocumentFile.getWhereValue())){
                        costProjectDocumentFile.setWhereValue(whereValue);
                    } else {

                        String whereValue1 = costProjectDocumentFile.getWhereValue();
                        String replace = whereValue1.replace("?", "'" + whereValue + "'");
                        if (replace.contains("&")) {
                            AuditedUnit auditedUnit = auditedUnitManager.get(unitId);
                            replace = replace.replace("&", "'" + auditedUnit.getUnitId() + "'");
                        }
                        costProjectDocumentFile.setWhereValue(replace);
                    }
                    String colValue = this.baseMapper.getColValue(costProjectDocumentFile.getTableName(), costProjectDocumentFile.getColName(), costProjectDocumentFile.getWhereName(), costProjectDocumentFile.getWhereValue());
                    //获取whereValue字段值
                    costProjectDocumentFile.setDataValue(CostDataUtil.convertToChineseDate(colValue));
                }
            } catch (Exception e) {
                throw new BaseException("获取字段失败:" + costProjectDocumentFile.getColName());
            }
        }
        return documentFileList;
    }




    @Override
    public List<CostDocumentTemplateFile> getDocumentFileList(String documentId) {
        LambdaQueryWrapper<CostDocumentTemplateFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostDocumentTemplateFile::getDocumentId, documentId);
        List<CostDocumentTemplateFile> list = this.list(queryWrapper);
        if (ObjectUtil.isEmpty(list)) {
            throw new BaseException("没有找到对应的数据");
        }
        return list;
    }

    @Override
    public String getColValue(String tableName, String colName, String whereName, String whereValue) {
        return baseMapper.getColValue(tableName, colName, whereName, whereValue);
    }

    @Override
    public String getColValue2(String tableName) {
        return baseMapper.getColValue2(tableName);
    }

    @Override
    public List<Map<String, Object>> getSurveyTemplate(String id) {

        return baseMapper.getSurveyTemplate(id);
    }
}
