package com.storlead.framework.mybatis.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.util.SpringContextUtils;
import lombok.SneakyThrows;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.format.annotation.DateTimeFormat;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j2;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-06 14:59
 */
@EqualsAndHashCode(callSuper = false)
@Log4j2
@Accessors(chain = true)
@Data
public class SysBaseField implements Serializable {

    @TableField(fill = FieldFill.INSERT)
    private Long ownerBy;

    @TableField(fill = FieldFill.INSERT)
    private Long createBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateBy;

    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JSONField(format ="yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @ApiModelProperty(value = "是否删除:1: 已删除 ,0: 未删除(默认)")
    private Integer isDelete;

    @ApiModelProperty(value = "是否启用(1:启用 0:禁用)")
    private Boolean enabled;

    @ApiModelProperty(value = "排序")
    private Integer sort;


    public static  <T>  void dictFileds(List<T> tList) {
        if (CollectionUtils.isEmpty(tList)) {
            return;
        }
//        for (T t : tList) {
        try {
            long be = System.currentTimeMillis();
            log.debug("dictUtils---convertDict");
            Object myClassInstance = SpringContextUtils.getBean("dictUtils");
            Class<?> clazz = myClassInstance.getClass();
            Method method = clazz.getMethod("convertDictls",List.class);
            // 调用未编写的方法
            method.invoke(myClassInstance,tList);
            log.debug("-------------------耗时 ==: "+(System.currentTimeMillis() - be));
        } catch (Exception e1) {
            log.error("方法不存在",e1);
        }
//        }
    }

    public void dictFiled() {
        dictFileds(Arrays.asList(this));
    }

    public static void dictFiled(Object t) {
        dictFileds(Arrays.asList(t));
    }

    public static  <T> void orgFileds(Object... vars) {
        orgFileds(Arrays.asList(vars));
    }
    public static  <T>  void orgFileds(List<T> tList) {
        if (CollectionUtils.isEmpty(tList)) {
            return;
        }
        try {
            log.debug("dictUtils---convertDict");
            Object myClassInstance = SpringContextUtils.getBean("orgAnnotateConvertUtils");
            Class<?> clazz = myClassInstance.getClass();
            Method method = clazz.getMethod("convertOrg",List.class);
            // 调用未编写的方法
            method.invoke(myClassInstance,tList);
        } catch (Exception e1) {
            log.error("方法不存在",e1);
        }
    }
    @SneakyThrows
    public void orgFiled() {
        orgFileds(Arrays.asList(this));
    }

    // Method to set empty strings to null
    public void setEmptyStringsToNull() {
        try {
            Field[] fields = this.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);  // Allow access to private fields
                if (field.getType().equals(String.class)) {
                    String value = (String) field.get(this);
                    if (value != null && value.isEmpty()) {
                        field.set(this, null);
                    }
                }
            }
        }catch (Exception e){
            log.error("转换失败",e);
        }
    }
    /**
     * 对象集合转换
     * @param tList
     * @param t
     * @return
     * @param <T>
     */
    public static  <T>  List<T> list(List tList,Class<T> t) {
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        List<T> vos = mapperFactory.getMapperFacade().mapAsList(tList,t);
        return vos;
    }
    /**
     * page 类转换
     * @param page
     * @param t
     * @return
     * @param <T>
     */
    public static  <T> IPage<T> page(IPage page, Class<T> t) {
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        IPage<T> pageVo = new Page<>(page.getCurrent(),page.getSize());
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return page;
        }
        List<T> vos = mapperFactory.getMapperFacade().mapAsList(page.getRecords(),t);
        pageVo.setTotal(page.getTotal());
        pageVo.setRecords(vos);
        return pageVo;
    }
}
