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.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.StringUtil;
import com.hotent.baseInfo.manager.CostCatalogSurveyManager;
import com.hotent.baseInfo.model.CostCatalogSurvey;
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.req.ConstSurveyPageParam;
import com.hotent.surveyinfo.req.ConstSurveyPageReq;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.User;
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 java.util.*;
import java.util.stream.Collectors;
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;
	@Autowired
	private UserManager userService;

	@Autowired
	private CostCatalogSurveyManager costCatalogSurveyManager;



	@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);
		// 根据查询条件过滤
	}

	@Override
	public PageList<CostSurveyTemplate> getCostSurveyListPage( ConstSurveyPageParam params) throws Exception {
		IUser iUser = ContextUtil.getCurrentUser();
		User user = userService.getByAccount(iUser.getAccount());
		if(iUser.isAdmin()){
			params.setDataScope(null);
		}else{
			params.setDataScope(user.getDataScope());
		}
		params.setProvinceCode(user.getProvinceCode());
		params.setCityCode(user.getCityCode());
		params.setCountyCode(user.getCountyCode());
		Page<CostSurveyTemplate> objectPage = new Page<>(params.getPage(),params.getPageSize());
		//Page<CostSurveyTemplate> page =baseMapper.getCostSurveyTemplatePage(objectPage,params);
		Page<CostSurveyTemplate> page =baseMapper.getCostSurveyTemplatePage1(objectPage,params.getContentType()
				,params.getType(),params.getDataScope()
				,params.getProvinceCode(),params.getCityCode()
				,params.getCountyCode(),params.getKeyword(),params.getStatus());
		return new PageList<>(page);
	}

	@Override
	public List<CostSurveyTemplate> taskListByCatalogId(String catalogId) {
		List<CostCatalogSurvey> list = costCatalogSurveyManager.list(
				new LambdaQueryWrapper<CostCatalogSurvey>()
						.eq(CostCatalogSurvey::getCatalogId, catalogId)
		);
		List<String> ids = list.stream().map(CostCatalogSurvey::getSurveyId).collect(Collectors.toList());
		if (ids.isEmpty()){
			return Collections.emptyList();
		}
		return this.listByIds(ids);
	}


}
