package com.hotent.project.manager.impl;

import java.util.Arrays;
import java.util.List;

import com.hotent.project.manager.CostProjectTaskManager;
import com.hotent.project.model.CostProjectTask;
import com.hotent.surveyinfo.manager.CostSurveyFdTemplateManager;
import com.hotent.surveyinfo.manager.CostSurveyTemplateManager;
import com.hotent.surveyinfo.model.CostSurveyFdTemplate;
import com.hotent.sys.persistence.manager.DataDictManager;
import com.hotent.sys.persistence.model.DataDict;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.project.dao.CostProjectTaskMaterialDao;
import com.hotent.project.manager.CostProjectTaskMaterialManager;
import com.hotent.project.model.CostProjectTaskMaterial;
import javax.annotation.Resource;


/**
 * 任务定制-报送资料要求 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-10-09
 */
@Service
public class CostProjectTaskMaterialManagerImpl extends BaseManagerImpl<CostProjectTaskMaterialDao, CostProjectTaskMaterial> implements CostProjectTaskMaterialManager {


	@Resource
	DataDictManager dataDictManager;
	@Autowired
	private CostSurveyFdTemplateManager costSurveyFdTemplateManager;

	@Autowired
	private CostProjectTaskManager costProjectTaskManager;

	@Override
	public CostProjectTaskMaterial getDetail(String id) {
    	CostProjectTaskMaterial costProjectTaskMaterial = this.get(id);
    	if (BeanUtils.isEmpty(costProjectTaskMaterial)) {
    		throw new RuntimeException("任务定制-报送资料要求不存在");
    	}
		return costProjectTaskMaterial;
	}

	@Override
	@Transactional
	public void createOrUpdate(CostProjectTaskMaterial costProjectTaskMaterial) {
		// 根据taskId获取任务节点
		if (costProjectTaskMaterial.getTaskId() != null) {
			CostProjectTask task = costProjectTaskManager.getById(costProjectTaskMaterial.getTaskId());
			if (task != null) {
				String currentNode = task.getCurrentNode();
				costProjectTaskMaterial.setTaskNode(currentNode);

				// 如果不是clcs节点，且有文件上传，需要根据文件类型设置informationType
				if (!"clcs".equals(currentNode) && costProjectTaskMaterial.getFileUrl() != null) {
					String fileUrl = costProjectTaskMaterial.getFileUrl();
					String fileExtension = getFileExtension(fileUrl);

					if (isWordFile(fileExtension)) {
						// Word文件设置为3
						costProjectTaskMaterial.setInformationType("1");
					} else if (isExcelFile(fileExtension)) {
						// Excel文件设置为2
						costProjectTaskMaterial.setInformationType("2");
					}else{
						costProjectTaskMaterial.setInformationType("1");
					}
				}
			}
		}

		//新建或更新
		this.saveOrUpdate(costProjectTaskMaterial);
	}

	/**
	 * 获取文件扩展名
	 */
	private String getFileExtension(String fileUrl) {
		if (fileUrl == null || fileUrl.isEmpty()) {
			return "";
		}
		int lastDotIndex = fileUrl.lastIndexOf(".");
		if (lastDotIndex > 0 && lastDotIndex < fileUrl.length() - 1) {
			return fileUrl.substring(lastDotIndex + 1).toLowerCase();
		}
		return "";
	}

	/**
	 * 判断是否为Word文件
	 */
	private boolean isWordFile(String extension) {
		return "doc".equals(extension) || "docx".equals(extension);
	}

	/**
	 * 判断是否为Excel文件
	 */
	private boolean isExcelFile(String extension) {
		return "xls".equals(extension) || "xlsx".equals(extension);
	}

	@Override
	@Transactional
	public void deleteById(String id) {
		this.removeById(id);
	}

	@Override
	@Transactional
	public void batchDelete(String[] ids) {
		this.removeByIds(Arrays.asList(ids));
	}

	@Override
	public List<CostProjectTaskMaterial> listByTaskId(String taskId) {
		QueryWrapper<CostProjectTaskMaterial> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("task_id", taskId);
		queryWrapper.orderByAsc("order_num");
		List<CostProjectTaskMaterial> list = this.list(queryWrapper);
		for (CostProjectTaskMaterial costProjectTaskMaterial : list) {
			DataDict byDictKey = dataDictManager.getByDictKey("1976557733440159744", costProjectTaskMaterial.getInformationType());
			if (!BeanUtils.isEmpty(byDictKey)){
				costProjectTaskMaterial.setInformationTypeName(byDictKey.getName());
			}
			if(costProjectTaskMaterial.getTemplateId() != null){
				CostSurveyFdTemplate manager = costSurveyFdTemplateManager.getById(costProjectTaskMaterial.getTemplateId());
				if (manager != null){
					costProjectTaskMaterial.setTemplateType(manager.getTemplateType());
				}
			}
			CostProjectTask task = costProjectTaskManager.getById(taskId);
			costProjectTaskMaterial.setAuditedUnitId(task.getAuditedUnitId()); ;
		}

		return list;
	}

}
