package com.hotent.surveyinfo.manager.impl;

import com.hotent.surveyinfo.dao.CostSurveyTemplateDao;
import com.hotent.surveyinfo.model.CostSurveyTemplate;
import com.hotent.surveyinfo.model.CostSurveyTemplateVersion;
import com.hotent.surveyinfo.dao.CostSurveyTemplateVersionDao;
import com.hotent.surveyinfo.manager.CostSurveyTemplateVersionManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
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 javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;


/**
 * 成本调查表版本 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-19
 */
@Service
public class CostSurveyTemplateVersionManagerImpl extends BaseManagerImpl<CostSurveyTemplateVersionDao, CostSurveyTemplateVersion> implements CostSurveyTemplateVersionManager {
	@Autowired
	CostSurveyTemplateVersionDao  costSurveyTemplateVersionDao;

	@Override
	public CostSurveyTemplateVersion getDetail(String id) {
    	CostSurveyTemplateVersion costSurveyTemplateVersion = this.get(id);
    	

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

	@Override
	public List<CostSurveyTemplateVersion> selectAll() {
		return Collections.emptyList();
	}

	@Override
	public List<CostSurveyTemplateVersion> selectByStatus(String status) {
		if (status == null || status.trim().isEmpty()) {
			return Collections.emptyList();
		}

		QueryWrapper<CostSurveyTemplateVersion> wrapper = new QueryWrapper<>();
		wrapper.eq("status", status);

		return this.list(wrapper);
	}

	@Override
	public CostSurveyTemplateVersion selectCurrentVersion(String surveyTemplateId) {
		if (surveyTemplateId == null || surveyTemplateId.trim().isEmpty()) {
			return null;
		}

		QueryWrapper<CostSurveyTemplateVersion> wrapper = new QueryWrapper<>();
		wrapper.eq("survey_template_id", surveyTemplateId);
		wrapper.eq("status", "0");
		wrapper.orderByDesc("order_num");
		wrapper.last("LIMIT 1");

		return this.getOne(wrapper);
	}
	@Override
	public List<CostSurveyTemplateVersion> getAllVersions() {
		return Collections.emptyList();
	}







	@Override
	public List<CostSurveyTemplateVersion> selectBySurveyTemplateId(String surveyTemplateId) {
		return costSurveyTemplateVersionDao.selectBySurveyTemplateId(surveyTemplateId);
	}

	@Override
	public String selectMaxVersionByDate(String dateStr) {
		QueryWrapper<CostSurveyTemplateVersion> wrapper = new QueryWrapper<>();
		// 查询以指定日期开头的版本号
		wrapper.likeRight("version_no", dateStr);
		// 按版本号降序排列，获取最大的版本号
		wrapper.orderByDesc("version_no");
		wrapper.last("LIMIT 1");

		CostSurveyTemplateVersion version = this.getOne(wrapper);
		if (version != null) {
			return version.getVersionNo();
		}
		return null;
	}

	/**
	 * 生成版本号，格式为：yyyyMMdd + 4位流水号（如：202509270001）
	 * @param
	 * @return 生成的版本号
	 */
	@Override
	public String generateVersionNumber() {
		// 1. 获取当前日期字符串
		String dateStr = new java.text.SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

		// 2. 查询当日最大版本号
		String maxVersion = this.selectMaxVersionByDate(dateStr);

		int sequence = 1;
		if (maxVersion != null && maxVersion.startsWith(dateStr)) {
			// 3. 提取流水号部分并加1
			String sequenceStr = maxVersion.substring(8); // 取后4位
			sequence = Integer.parseInt(sequenceStr) + 1;
		}

		// 4. 格式化生成版本号
		return String.format("%s%04d", dateStr, sequence);
	}

	@Override
	public List<CostSurveyTemplateVersion> selectByTemplateIdAndStatus(String surveyTemplateId, String status) {
		if (surveyTemplateId == null || surveyTemplateId.trim().isEmpty()) {
			return Collections.emptyList();
		}

		QueryWrapper<CostSurveyTemplateVersion> wrapper = new QueryWrapper<>();
		wrapper.eq("survey_template_id", surveyTemplateId);

		// 如果状态参数不为空，则添加状态条件
		if (status != null && !status.trim().isEmpty()) {
			wrapper.eq("status", status);
		}
		wrapper.orderByDesc("create_time");
		return this.list(wrapper);
	}

}
