package com.xjrsoft.module.liteflow.node;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjrsoft.common.enums.ArchivesStatusEnum;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.student.entity.BaseClassMajorSet;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.entity.StudentTransfer;
import com.xjrsoft.module.student.mapper.BaseClassMajorSetMapper;
import com.xjrsoft.module.student.mapper.StudentTransferMapper;
import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 学生插班流程结束后
 */
@Component("student_transfer_node")
public class StudentTransferNode extends NodeComponent {
    @Autowired
    private StudentTransferMapper studentTransferMapper;
    @Autowired
    private IBaseStudentSchoolRollService studentSchoolRollService;

    @Autowired
    private BaseClassMapper baseClassMapper;

    @Autowired
    private BaseClassMajorSetMapper baseClassMajorSetMapper;

    @Override
    public void process() throws Exception {
        // 获取表单中数据编号
        Map<String, Object> params = this.getFirstContextBean();
        Object value = util.getFormDatKey(params,"id");
        Long formId = Convert.toLong(value);
        if (formId != null) {
            //查询出数据
            StudentTransfer studentTransfer = studentTransferMapper.selectById(formId);
            BaseClass baseClass = baseClassMapper.selectById(studentTransfer.getClassId());
            BaseClassMajorSet majorSet = baseClassMajorSetMapper.selectOne(
                new QueryWrapper<BaseClassMajorSet>().lambda()
                .eq(BaseClassMajorSet::getClassId, studentTransfer.getClassId())
                .eq(BaseClassMajorSet::getDeleteMark, DeleteMark.NODELETE.getCode())
            );
            BaseStudentSchoolRoll roll = new BaseStudentSchoolRoll();
            roll.setStduyStatus(studentTransfer.getStudyStatus());
            roll.setClassId(studentTransfer.getClassId());
            roll.setEnrollType(baseClass.getEnrollType());
            roll.setCreateDate(LocalDateTime.now());
            roll.setGradeId(baseClass.getGradeId());
            roll.setMajorSetId(majorSet.getMajorSetId());
            roll.setCreateUserId(studentTransfer.getCreateUserId());
            roll.setArchivesStatus(ArchivesStatusEnum.FB2901.getCode());
            roll.setUserId(studentTransfer.getStudentUserId());
            //新增学籍信息
            studentSchoolRollService.save(roll);
        }
    }
}
