package com.hotent.surveyinfo.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.surveyinfo.dao.CostSurveyFdTemplateVersionDao;
import com.hotent.surveyinfo.dao.CostSurveyTemplateVersionDao;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateVersionManager;
import com.hotent.surveyinfo.manager.CostSurveyTemplateVersionManager;
import com.hotent.surveyinfo.model.CostSurveyFdTemplateVersion;
import com.hotent.surveyinfo.model.CostSurveyTemplateVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


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

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

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

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

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

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

		return this.list(wrapper);
	}

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

		QueryWrapper<CostSurveyFdTemplateVersion> 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<CostSurveyFdTemplateVersion> getAllVersions() {
		return Collections.emptyList();
	}







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

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

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

	/**
	 * 生成版本号，格式为：yyyyMMdd + 4位流水号（如：202509270001）
	 * @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<CostSurveyFdTemplateVersion> selectByFdTemplateIdAndStatus(String surveyFdTemplateId, String status) {
		if (surveyFdTemplateId == null || surveyFdTemplateId.trim().isEmpty()) {
			return Collections.emptyList();
		}

		QueryWrapper<CostSurveyFdTemplateVersion> wrapper = new QueryWrapper<>();
		wrapper.eq("survey_template_id", surveyFdTemplateId);

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

		return this.list(wrapper);
	}


}
