package com.hotent.baseInfo.manager.impl;

import cn.hutool.core.bean.BeanUtil;
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.exception.BaseException;
import com.hotent.base.util.StringUtil;
import com.hotent.baseInfo.dao.CostDocumentTemplateFileDao;
import com.hotent.baseInfo.manager.CostDocumentTemplateFileManager;
import com.hotent.baseInfo.model.CostDocumentTemplate;
import com.hotent.baseInfo.dao.CostDocumentTemplateDao;
import com.hotent.baseInfo.manager.CostDocumentTemplateManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.baseInfo.model.CostDocumentTemplateFile;
import com.hotent.baseInfo.model.CostDocumentWh;
import com.hotent.baseInfo.req.CostDocumentTemplatePageReq;
import com.hotent.baseInfo.req.CostDocumentTemplateReq;
import com.hotent.constant.BaseConstant;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.util.ContextUtil;
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.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;


/**
 * 监审文书模板表 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-17
 */
@Service
public class CostDocumentTemplateManagerImpl extends BaseManagerImpl<CostDocumentTemplateDao, CostDocumentTemplate> implements CostDocumentTemplateManager {

	@Autowired
	private CostDocumentTemplateFileManager costDocumentTemplateFileManager;

	@Autowired
	private CostDocumentTemplateFileDao costDocumentTemplateFileDao;

	@Override
	public CostDocumentTemplate getDetail(String id) {
    	CostDocumentTemplate costDocumentTemplate = this.get(id);
    	

		return costDocumentTemplate;
	}

	@Override
	public List<CostDocumentTemplate> getWhCateList() {
		LambdaQueryWrapper<CostDocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostDocumentTemplate::getIsWh, "0");
		queryWrapper.eq(CostDocumentTemplate::getIsDeleted, "0");
		return this.list(queryWrapper);
	}

	@Override
	public IPage<CostDocumentTemplate> getTemplateList(CostDocumentTemplatePageReq req) {
		LambdaQueryWrapper<CostDocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtil.isNotEmpty(req.getDocumentName())){
			queryWrapper.like(CostDocumentTemplate::getDocumentName, req.getDocumentName());
		}

		if(StringUtil.isNotEmpty(req.getStatus())){
			queryWrapper.eq(CostDocumentTemplate::getStatus, req.getStatus());
		}
		queryWrapper.eq(CostDocumentTemplate::getIsDeleted, BaseConstant.NORMAL_STATUS);
		// 创建分页对象
		IPage<CostDocumentTemplate> page = new Page<>(req.getPageNum(), req.getPageSize());
		page.getRecords().forEach(item -> {

		});
		return this.page(page, queryWrapper);
	}

	@Override
	@ Transactional(rollbackFor = Exception.class)
	public String create(CostDocumentTemplateReq entityReq) {
		LambdaQueryWrapper<CostDocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostDocumentTemplate::getDocumentName, entityReq.getDocumentName());
		CostDocumentTemplate costDocumentTemplate = this.getOne(queryWrapper, false);
		IUser user = ContextUtil.getCurrentUser();
		entityReq.setCreateBy(user.getAccount());
		entityReq.setCreateTime(LocalDateTime.now());

		CostDocumentTemplate entity = BeanUtil.copyProperties(entityReq, CostDocumentTemplate.class);
		if(costDocumentTemplate== null ){
			entity.setIsDeleted(BaseConstant.NORMAL_STATUS);
			entity.setCreateBy(user.getAccount());
			entity.setCreateTime(LocalDateTime.now());
			super.save(entity);
			List<CostDocumentTemplateFile> costDocumentTemplateFiles = entityReq.getCostDocumentTemplateFiles();
			if (costDocumentTemplateFiles != null){
				costDocumentTemplateFiles.forEach(item -> {
					item.setDocumentId(entity.getId().toString());
//					int tableExists = this.costDocumentTemplateFileDao.tableExists(item.getTableName());
//					if (tableExists==0){
//						throw new BaseException("请检查表是否存在 表名:"+item.getTableName()+" 字段名:"+item.getColName());
//					}
//					int colExists = this.costDocumentTemplateFileDao.colExists(item.getTableName(), item.getColName());
//					if(colExists==0){
//						throw new BaseException("请检查表是否存在 表名:"+item.getTableName()+" 字段名:"+item.getColName());
//					}
					item.setIsDeleted(BaseConstant.NORMAL_STATUS);
				});
			}

			costDocumentTemplateFileManager.saveBatch(entityReq.getCostDocumentTemplateFiles());
			return entity.getId().toString();
		}else{

			throw new BaseException("文书名称重复");
		}

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateTemplate(CostDocumentTemplateReq req) {
		LambdaQueryWrapper<CostDocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostDocumentTemplate::getDocumentName, req.getDocumentName());
		CostDocumentTemplate costDocumentTemplate = this.getOne(queryWrapper, false);
		CostDocumentTemplate entity = BeanUtil.copyProperties(req, CostDocumentTemplate.class);
		if(costDocumentTemplate== null || costDocumentTemplate.getId().equals(entity.getId())){
			if (costDocumentTemplate!=null){
				entity.setStatus(costDocumentTemplate.getStatus());
				entity.setIsDeleted(costDocumentTemplate.getIsDeleted());
				entity.setUpdateTime(LocalDateTime.now());
				entity.setCreateTime(costDocumentTemplate.getCreateTime());
				entity.setCreateBy(costDocumentTemplate.getCreateBy());
			}
			super.update(entity);
			LambdaQueryWrapper<CostDocumentTemplateFile> deleteWrapper = new LambdaQueryWrapper<>();
			deleteWrapper.eq(CostDocumentTemplateFile::getDocumentId, entity.getId());
			costDocumentTemplateFileManager.remove(deleteWrapper);
			List<CostDocumentTemplateFile> costDocumentTemplateFiles = req.getCostDocumentTemplateFiles();

			if (costDocumentTemplateFiles != null ){
				costDocumentTemplateFiles.forEach(item -> {
					item.setDocumentId(entity.getId().toString());
//					int tableExists = this.costDocumentTemplateFileDao.tableExists(item.getTableName());
//					if (tableExists==0){
//						throw new BaseException("请检查表是否存在 表名:"+item.getTableName()+" 字段名:"+item.getColName());
//					}
//					int colExists = this.costDocumentTemplateFileDao.colExists(item.getTableName(), item.getColName());
//					if(colExists==0){
//						throw new BaseException("请检查表是否存在 表名:"+item.getTableName()+" 字段名:"+item.getColName());
//					}

				});
				costDocumentTemplateFileManager.saveBatch(costDocumentTemplateFiles);
			}
		}else{
			if(!req.getId().equals(costDocumentTemplate.getId())){
				throw new BaseException("文书名称重复");
			}

		}
	}

	@Override
	public void deleteByIds(List<String> ids) {
		List<CostDocumentTemplate> costDocumentTemplates = this.listByIds(ids);
		costDocumentTemplates.forEach(item -> {
			//逻辑删除
			item.setIsDeleted(BaseConstant.DELETE_FLAG);
		});
		this.updateBatchById(costDocumentTemplates);

	}

	@Override
	public CostDocumentTemplate getByAlias(String sdhz) {
		QueryWrapper<CostDocumentTemplate> wrapper = new QueryWrapper<>();
		wrapper.eq("alias",sdhz);
		return this.getOne(wrapper);
	}


	@Override
	public void update(CostDocumentTemplate entity) {
		LambdaQueryWrapper<CostDocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CostDocumentTemplate::getDocumentName, entity.getDocumentName());
		CostDocumentTemplate costDocumentTemplate = this.getOne(queryWrapper, false);
		IUser user = ContextUtil.getCurrentUser();
		entity.setUpdateBy(user.getAccount());
		entity.setUpdateTime(LocalDateTime.now());
		if(costDocumentTemplate== null){
			super.update(entity);
		}else{

			throw new BaseException("文书名称重复");
		}
	}

	@Override
	public boolean removeById(Serializable id) {
		LambdaQueryWrapper<CostDocumentTemplateFile> lambdaQuery = new LambdaQueryWrapper<CostDocumentTemplateFile>();
		lambdaQuery.eq(CostDocumentTemplateFile::getDocumentId, id);
		costDocumentTemplateFileManager.list(lambdaQuery).forEach(item -> {
			//逻辑删除
			item.setIsDeleted(BaseConstant.DELETE_FLAG);
			costDocumentTemplateFileManager.update(item);
		});

		return super.removeById(id);
	}


}
