

package com.lideeyunji.core.framework.enhance.example.dbform.student;

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.config.aspect.enhance.plugin.BeforeAdvicePlugin;
import com.lideeyunji.core.framework.mapper.example.ExampleDbFormMapper;
import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import com.lideeyunji.tool.framework.yunji.utils.lideeYunJiUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 学生-新增-前置增强
 */
@Slf4j
@Component("studentBeforeEnhanceAdd")
public class ExampleStudentBeforeEnhanceAdd implements BeforeAdvicePlugin {

    @Autowired
    private ExampleDbFormMapper exampleDbFormMapper;

    @Override
    public void execute(EnhanceContext enhanceContext) {
        log.info("进入=======>studentBeforeEnhanceAdd=======>execute");
        Map<String, Object> params = enhanceContext.getParam().getParams();
        this.checkData(params);
    }

    /**
     * 校验数据合法性
     *
     * @param map
     */
    private void checkData(Map<String, Object> map) {
        Long sno = lideeYunJiUtils.getMap2Long(map, "sno");
        Long id = exampleDbFormMapper.getStudentBySon(sno);
        if (FuncBase.isNotEmpty(id)) {
            throw new lideeYunJiException("学号[" + sno + "]已存在！");
        }
    }
}
