

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

import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.jeelowcode.core.framework.config.aspect.enhance.plugin.BeforeAdvicePlugin;
import com.jeelowcode.core.framework.mapper.example.ExampleDbFormMapper;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.utils.utils.FuncBase;
import com.jeelowcode.framework.utils.utils.JeeLowCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 学生-编辑-前置增强
 */
@Slf4j
@Component("studentBeforeEnhanceEdit")
public class ExampleStudentBeforeEnhanceEdit implements BeforeAdvicePlugin {

    @Autowired
    private ExampleDbFormMapper exampleDbFormMapper;

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

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