MyMetaObjectHandler.java 3.65 KB
package com.huaheng.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.receipt.receiptDetailHistory.domain.ReceiptDetailHistory;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.srm.domain.SrmDetailHistory;
import com.huaheng.pc.srm.domain.SrmHeaderHistory;
import com.huaheng.pc.task.taskDetail.domain.TaskDetailHistory;
import com.huaheng.pc.task.taskHeader.domain.TaskHeaderHisotry;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Enzo Cotter on 2020/7/7.
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入元对象字段填充(用于插入时对公共字段的填充)
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        if (metaObject.getOriginalObject() instanceof ReceiptHeaderHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof ShipmentHeaderHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof ReceiptDetailHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof ShipmentDetailHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof SrmHeaderHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof SrmDetailHistory) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof TaskHeaderHisotry) {
            return;
        }
        if (metaObject.getOriginalObject() instanceof TaskDetailHistory) {
            return;
        }
        this.setFieldValByName("created", new Date(), metaObject);
        if (ShiroUtils.getUserName() != null) {
//            System.out.println(metaObject.getOriginalObject() instanceof TaskHeader);
//            if (metaObject.getOriginalObject() instanceof TaskHeader) {
//
//            } else {
//                this.setFieldValByName("createdBy", ShiroUtils.getUserName(), metaObject);
//            }
            this.setFieldValByName("createdBy", ShiroUtils.getUserName(), metaObject);
            this.setFieldValByName("lastUpdatedBy", ShiroUtils.getUserName(), metaObject);
        } else {
            this.setFieldValByName("createdBy", ShiroUtils.getLoginName(), metaObject);
            this.setFieldValByName("lastUpdatedBy", ShiroUtils.getLoginName(), metaObject);
        }
        this.setFieldValByName("lastUpdated", new Date(), metaObject);
    }

    /**
     * 更新元对象字段填充(用于更新时对公共字段的填充)
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastUpdated", new Date(), metaObject);
        if (ShiroUtils.getUserName() != null) {
            this.setFieldValByName("lastUpdatedBy", ShiroUtils.getUserName(), metaObject);
        } else {
            if (ShiroUtils.getUserName() != null) {
                this.setFieldValByName("lastUpdatedBy", ShiroUtils.getLoginName(), metaObject);
            }
        }
    }
}