package com.hotent.surveyinfo.manager.impl;

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.util.StringUtil;
import com.hotent.baseInfo.model.CostCatalog;
import com.hotent.surveyinfo.dao.CostSurveyTemplateHeadersDao;
import com.hotent.surveyinfo.dao.CostSurveyTemplateVersionDao;
import com.hotent.surveyinfo.manager.CostSurveyTemplateItemsManager;
import com.hotent.surveyinfo.model.CostSurveyTemplate;
import com.hotent.surveyinfo.dao.CostSurveyTemplateDao;
import com.hotent.surveyinfo.manager.CostSurveyTemplateManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.surveyinfo.model.CostSurveyTemplateHeaders;
import com.hotent.surveyinfo.model.CostSurveyTemplateItems;
import com.hotent.surveyinfo.model.CostSurveyTemplateVersion;
import com.hotent.surveyinfo.model.dto.CostSurveyData;
import com.hotent.surveyinfo.model.dto.CostSurveyField;
import com.hotent.surveyinfo.req.ConstSurveyPageReq;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.*;
import javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;
import javax.sql.DataSource;

/**
 * 成本调查表模板表 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-19
 */
@Service
public class CostSurveyTemplateManagerImpl extends BaseManagerImpl<CostSurveyTemplateDao, CostSurveyTemplate> implements CostSurveyTemplateManager {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	CostSurveyTemplateHeadersDao costSurveyTemplateheaderMapper;
	@Autowired
	CostSurveyTemplateItemsManager costSurveyTemplateItemsManager;
	@Autowired
	CostSurveyTemplateVersionDao costSurveyTemplateVersionMapper;
	@Autowired
	private DataSource dataSource;
	@Override
	public IPage<CostSurveyTemplate> getCostSurveyList(ConstSurveyPageReq req) {
		// 构建查询条件
		LambdaQueryWrapper<CostSurveyTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostSurveyTemplate::getIsDelete, "0")	;
		IUser user = ContextUtil.getCurrentUser();
		queryWrapper.eq(CostSurveyTemplate::getCreateBy, user.getAccount())	;
		// 根据查询条件过滤
		if (StringUtil.isNotEmpty(req.getType())) {
			queryWrapper.eq(CostSurveyTemplate::getType, req.getType());
		}
		if (StringUtil.isNotEmpty(req.getContentType())) {
			queryWrapper.like(CostSurveyTemplate::getContentType, req.getContentType());
		}
		if (StringUtil.isNotEmpty(req.getKeyword())) {
			queryWrapper.like(CostSurveyTemplate::getSurveyTemplateName, req.getKeyword()).or().like(CostSurveyTemplate::getSurveyTemplateNameYw, req.getKeyword());
		}
		if (req.getStatus() != null) {
			queryWrapper.eq(CostSurveyTemplate::getStatus, req.getStatus());
		}
		if (StringUtil.isNotEmpty(req.getCatalogId())) {
			queryWrapper.like(CostSurveyTemplate::getCatalogId, req.getCatalogId());
		}

		// 排序：按编码排序
        queryWrapper.orderByDesc(CostSurveyTemplate::getCreateTime);

		// 创建分页对象
		Page<CostSurveyTemplate> page = new Page<>(req.getPageNum(), req.getPageSize());
		return this.page(page,queryWrapper);
	}

	@Override
	public IPage<CostSurveyTemplate> getallCurrentCostSurveyList(ConstSurveyPageReq req) {
		// 构建查询条件
		LambdaQueryWrapper<CostSurveyTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostSurveyTemplate::getStatus, "0")	;
		// 根据查询条件过滤
		if (StringUtil.isNotEmpty(req.getType())) {
			queryWrapper.eq(CostSurveyTemplate::getType, req.getType());
		}
		if (StringUtil.isNotEmpty(req.getContentType())) {
			queryWrapper.like(CostSurveyTemplate::getContentType, req.getContentType());
		}
		if (StringUtil.isNotEmpty(req.getKeyword())) {
			queryWrapper.like(CostSurveyTemplate::getSurveyTemplateName, req.getKeyword()).or().like(CostSurveyTemplate::getSurveyTemplateNameYw, req.getKeyword());
		}
		if (req.getStatus() != null) {
			queryWrapper.eq(CostSurveyTemplate::getStatus, req.getStatus());
		}

		// 排序：按编码排序
//        queryWrapper.orderByAsc("code");

		// 创建分页对象
		Page<CostSurveyTemplate> page = new Page<>(req.getPageNum(), req.getPageSize());
		return this.page(page,queryWrapper);
	}

//	@Override
//	public List<CostSurveyTemplate> selectByCondition(CostSurveyTemplate costSurveyTemplate) {
//		return costSurveyTemplateDao.selectByCondition(costSurveyTemplate);
//	}

	@Override
	public CostSurveyTemplate getDetail(String id) {
    	CostSurveyTemplate costSurveyTemplate = this.get(id);


		return costSurveyTemplate;
	}
	@Override
	@Transactional
	public void createOrUpdate(CostSurveyTemplate costSurveyTemplate) {
		//新建或更新
		this.saveOrUpdate(costSurveyTemplate);
	}

	@Override
	public void batchRemove(List<String> ids) {
		if (ids != null && !ids.isEmpty()) {
			super.removeByIds(ids);
		}
	}

	@Override
	public List<CostSurveyTemplateHeaders> getHeadersByTemplateId(String templateId) {
		return Collections.emptyList();
	}

	@Override
	public List<CostSurveyTemplateItems> getItemsByTemplateId(String templateId) {
		return Collections.emptyList();
	}


	 //根据监管目录ID查询所有现行版本的所有成本调查表模板信息
	@Override
	public List<CostSurveyTemplate> listByCatalogId(String catalogId) {

		// 查询指定目录下的所有启用状态的模板
		LambdaQueryWrapper<CostSurveyTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostSurveyTemplate::getCatalogId, catalogId)	;

		queryWrapper.eq(CostSurveyTemplate::getStatus, "0")	;

		return this.list(queryWrapper);
		// 根据查询条件过滤
	}


}
