package com.gic.haoban.manage.web.log;

import com.gic.haoban.manage.api.enums.hm.HmWelcomeType;
import com.gic.log.record.bean.GicLogRecordBean;
import com.gic.log.record.service.LogRecordAfterService;
import com.gic.log.record.util.GicLogRecordEvaluationContext;
import com.gic.log.record.util.GicLogRecordOptTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @Author MUSI
 * @Date 2022/7/15 11:50 AM
 * @Description
 * @Version
 **/
@Component
public class LogRecordAfterServiceImpl extends LogRecordAfterService {

    public static final String WELCOME_TYPE = "welcome_type";
    public static final String WELCOME_OPT = "welcome_opt";

    @Override
    public void deal(GicLogRecordBean gicLogRecordBean) {
        String value = GicLogRecordEvaluationContext.getAttribute(WELCOME_OPT, String.class);
        Integer welcomeType = GicLogRecordEvaluationContext.getAttribute(WELCOME_TYPE, Integer.class);

        Integer welcomeDelType = GicLogRecordEvaluationContext.getAttribute("welcome_del_type", Integer.class);

        if ("add".equals(value)){
            // 新建欢迎语
            if (HmWelcomeType.NORMAL.getCode().equals(welcomeType)){
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.NORMAL_WELCOME_ADD);
            }else {
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.SPECIAL_WELCOME_ADD);
            }
        }
        if (StringUtils.equals("edit", value)){
            // 修改欢迎语
            if (HmWelcomeType.NORMAL.getCode().equals(welcomeType)){
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.NORMAL_WELCOME_UPDATE);
            }else {
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.SPECIAL_WELCOM_UPDATE);
            }
        }

        if (welcomeDelType != null) {
            // 删除欢迎语
            if (HmWelcomeType.NORMAL.getCode().equals(welcomeDelType)) {
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.NORMAL_WELCOME_DEL);
            }else {
                this.changeLogRecordOptType(gicLogRecordBean, GicLogRecordOptTypeEnum.SPECIAL_WELCOM_DEL);
            }
        }
    }

    private void changeLogRecordOptType(GicLogRecordBean gicLogRecordBean, GicLogRecordOptTypeEnum optTypeEnum){
        if (gicLogRecordBean == null || optTypeEnum == null){
            return;
        }
        gicLogRecordBean.setOptType(optTypeEnum.getType());
        gicLogRecordBean.setOptTypeName(optTypeEnum.getName());
        gicLogRecordBean.setSystemType(optTypeEnum.getSystemType());
    }
}
