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.RoomApplicantTypeEnum;
import com.xjrsoft.common.enums.StudyStatusEnum;
import com.xjrsoft.module.room.entity.WfRoomApplicant;
import com.xjrsoft.module.room.mapper.WfRoomApplicantMapper;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.entity.StudentChangeClass;
import com.xjrsoft.module.student.mapper.StudentChangeClassMapper;
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.util.Map;

/**
 * 学生住宿申请后，修改学籍信息中的就读方式
 */
@Component("wf_room_applicant_node")
public class WfRoomApplicantNode extends NodeComponent {
    @Autowired
    private WfRoomApplicantMapper wfRoomApplicantMapper;
    @Autowired
    private IBaseStudentSchoolRollService studentSchoolRollService;
    @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) {
            //查询出数据
            WfRoomApplicant wfRoomApplicant = wfRoomApplicantMapper.selectById(formId);
            BaseStudentSchoolRoll schoolRoll = studentSchoolRollService.getOne(
                new QueryWrapper<BaseStudentSchoolRoll>().lambda()
                .eq(BaseStudentSchoolRoll::getUserId, wfRoomApplicant.getApplicantUserId())
            );
            if(RoomApplicantTypeEnum.ToBeBoarder.getCode().equals(wfRoomApplicant.getRecedeType())){
                schoolRoll.setStduyStatus(StudyStatusEnum.InResidence.getCode());
            }else if(RoomApplicantTypeEnum.ToBeDayPupil.getCode().equals(wfRoomApplicant.getRecedeType())){
                schoolRoll.setStduyStatus(StudyStatusEnum.AttendDaySchool.getCode());
            }
            //修改学生班级
            studentSchoolRollService.updateById(schoolRoll);
        }
    }
}
