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.manager.impl.BaseManagerImpl;
import com.hotent.base.query.PageList;
import com.hotent.base.util.StringUtil;
import com.hotent.surveyinfo.dao.*;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateItemsManager;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateManager;
import com.hotent.surveyinfo.manager.CostSurveyTemplateItemsManager;
import com.hotent.surveyinfo.manager.CostSurveyTemplateManager;
import com.hotent.surveyinfo.model.*;
import com.hotent.surveyinfo.req.ConstSurveyFdPageParam;
import com.hotent.surveyinfo.req.ConstSurveyFdPageReq;
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 javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * 财务数据表模板表 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-19
 */
@Service
public class CostSurveyFdTemplateManagerImpl extends BaseManagerImpl<CostSurveyFdTemplateDao, CostSurveyFdTemplate> implements CostSurveyFdTemplateManager {

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

		queryWrapper.orderByDesc(CostSurveyFdTemplate::getCreateTime);
		// 排序：按编码排序
//        queryWrapper.orderByAsc("code");

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

	@Override
	public IPage<CostSurveyFdTemplate> getAllcurrentCostSurveyList(ConstSurveyFdPageReq req) {



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

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

		// 创建分页对象
		Page<CostSurveyFdTemplate> 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 CostSurveyFdTemplate getDetail(String id) {
		CostSurveyFdTemplate costSurveyTemplate = this.get(id);


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

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

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

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


//根据监管目录ID查询所有现行版本的所有财务数据表模板信息
	@Override
	public List<CostSurveyFdTemplate> listByCatalogId(String catalogId) {
		LambdaQueryWrapper<CostSurveyFdTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostSurveyFdTemplate::getCatalogId, catalogId)	;
		queryWrapper.eq(CostSurveyFdTemplate::getStatus, "0");
		return this.list(queryWrapper);
	}

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


}
