package com.prolog.rdc.tms.service.basicConfigure.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.prolog.product.core.easyexcel.annotation.ValidationPropertyExt;
import com.prolog.rdc.tms.config.enums.ImportLabelEnum;
import com.prolog.rdc.tms.constant.StatusConstant;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.annotation.ExcelProperty;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.config.enums.ImportObjEnum;
import com.prolog.rdc.tms.model.dto.basicConfigure.ImportStdFeildQueryDTO;
import com.prolog.rdc.tms.service.basicConfigure.ImportStdFeildService;

import cn.hutool.core.util.ReflectUtil;

/**
 * 
 *
 * 基础配置-标准模板详情实现层
 *
 * @className :ImportStdFeildServiceImpl
 * @author :fanzw
 * @creationTime :2022/06/21
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/06/21
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class ImportStdFeildServiceImpl implements ImportStdFeildService {

	/**
	 * 标准模板列查询
	 */
	@Override
	public List<ImportStdFeildQueryDTO> list(String importObjNo) {
		ToolKits.isParamNull(importObjNo, "请求不为空");
		// 人员权限
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 返回对象集合
		List<ImportStdFeildQueryDTO> importStdFeildQueryDTOs = Lists.newArrayList();
		try {
			Class<?> clazz = ImportObjEnum.valueOf(importObjNo).getClazz();
			ToolKits.isParamNull(clazz, "标准模板对象不存在");
			// 获取属性信息
			Field[] fields = ReflectUtil.getFields(clazz);
			int sort = 1;
			for (Field field : fields) {
				if (field.getName().equals("serialVersionUID")) {
					continue;
				}
				// 反射找到对应字段
				Field displayName = clazz.getDeclaredField(field.getName());
				// 获取字段上的注解
				ExcelProperty annotation = displayName.getAnnotation(ExcelProperty.class);
				ValidationPropertyExt validationPropertyExt = displayName.getAnnotation(ValidationPropertyExt.class);
				// 返回对象
				ImportStdFeildQueryDTO importStdFeildQueryDTO = new ImportStdFeildQueryDTO();
				ImportLabelEnum importLabelEnum = ImportLabelEnum.getByLabel(field.getName());
				if (ObjectUtil.isNotNull(importLabelEnum)) {
					importStdFeildQueryDTO.setLabelType(importLabelEnum.getLabelType());
				}
				if (ObjectUtil.isNotNull(validationPropertyExt)) {
					importStdFeildQueryDTO.setRequired(IndexedColors.RED.equals(validationPropertyExt.headFontColor()) ? StatusConstant.WHETHER_YES : StatusConstant.WHETHER_NO);
				} else {
					importStdFeildQueryDTO.setRequired(StatusConstant.WHETHER_NO);
				}
				importStdFeildQueryDTO.setFieldid(ToolKits.getUUID());
				importStdFeildQueryDTO.setImportObjNo(importObjNo);
				// field.getName()获取属性名
				importStdFeildQueryDTO.setStdColNo(field.getName());
				importStdFeildQueryDTO.setStdColName(annotation.value().length == 0 ? null : annotation.value()[0]);
				importStdFeildQueryDTO.setSort(sort++);
				importStdFeildQueryDTOs.add(importStdFeildQueryDTO);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ParameterException("获取标准模板详情失败");
		}
		return importStdFeildQueryDTOs;
	}

	/**
	 * 生成标准导入字段
	 * 
	 * @param
	 * @return
	 * @throws @date 2022/1/13 12:16
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void generate(String importObjNo) throws Exception {
		// 人员权限
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		List<ImportStdFeildQueryDTO> models = new ArrayList<>();
		Class<?> clazz = ImportObjEnum.valueOf(importObjNo).getClazz();

		ToolKits.isParamNull(clazz, "导入对象名称不存在");
		Field[] fields = ReflectUtil.getFields(clazz);

		int sort = 1;
		for (Field field : fields) {
			ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
			if (ToolKits.isNotEmpty(annotation)) {
				ImportStdFeildQueryDTO model = new ImportStdFeildQueryDTO();
				String[] value = annotation.value();
				ToolKits.isParamNull(value, "ExcelProperty注释value不应该为空");
				String name = field.getName();
				model.setFieldid(UUID.randomUUID().toString().replace("-", ""));
				model.setImportObjNo(importObjNo);
				model.setStdColNo(name);
				model.setStdColName(value[0]);
				model.setSort(sort);
				models.add(model);
				sort++;
			}
		}
	}
}
