package com.storlead.sales.pojo.vo.excel;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.storlead.framework.common.util.ConvertUtils;
import com.storlead.sales.pojo.dto.excel.ExcelVerifyEntityOfMode;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-06-13 16:13
 */
@Data
@Log4j2
public class ExcelViewDataVO {

    public String columnDes;

    public String columnName;

    public String columnData;

    public <T extends ExcelVerifyEntityOfMode> ExcelViewVO getExcelViewDataVOS(T d) {
        ExcelViewVO excelViewVO = new ExcelViewVO();
        List<Field> fieldls  = Arrays.asList(d.getClass().getDeclaredFields());
        List<ExcelViewDataVO> ds = new ArrayList<>();
        for (Field field : fieldls) {
            ExcelViewDataVO vo = new ExcelViewDataVO();
            vo.setColumnName(field.getName());
            Excel column = field.getAnnotation(Excel.class);
            if (Objects.nonNull(column)) {
                vo.setColumnDes(column.name());
                String key1 = column.name();
                try {
                    Object value = FieldUtils.readField(d, field.getName(), true);

                    String value1 = Objects.isNull(value) ? "" : value.toString();
                    if (key1.equals("区域名称")
                            || key1.equals("归属人员账号")
                            || key1.equals("区域名称")
                            || key1.equals("归属人员")
                            || key1.contains("联系人")
                            || key1.contains("客户名称")) {
                        value1 = ConvertUtils.replaceTrimBlank(value1);
                    }
                    if (key1.contains("邮箱")
                            || key1.contains("网址")
                            || key1.equals("企业网址")
                            || key1.equals("企业电子邮箱")) {
                        value1 = ConvertUtils.replaceBlank(value1);
                    }

                    vo.setColumnData(value1);
                }catch (Exception e) {
                    log.error("getExcelViewDataVOS -- error"+field.getName(),e);
                    vo.setColumnData("");
                }
                ds.add(vo);
            }

        }
        excelViewVO.setRowData(ds);
        return excelViewVO;
    }
}
