package com.srs.routine.service.impl;

import java.util.List;
import com.srs.common.utils.WeChatUtil;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.routine.mapper.RtStuMultiLevelReviewMapper;
import com.srs.routine.domain.RtStuMultiLevelReview;
import com.srs.routine.service.IRtStuMultiLevelReviewService;

/**
 * 审核信息Service业务层处理
 *
 * @author srs
 * @date 2024-05-17
 */
@Service
public class RtStuMultiLevelReviewServiceImpl extends ServiceImpl<RtStuMultiLevelReviewMapper, RtStuMultiLevelReview> implements IRtStuMultiLevelReviewService {
    @Autowired
    private RtStuMultiLevelReviewMapper rtStuMultiLevelReviewMapper;

    @Autowired
    public WeChatUtil weChatUtil;

    /**
     * 查询审核信息
     *
     * @param id 审核信息主键
     * @return 审核信息
     */
    @Override
    public RtStuMultiLevelReview selectRtStuMultiLevelReviewById(Long id) {
        return rtStuMultiLevelReviewMapper.selectRtStuMultiLevelReviewById(id);
    }

    @Override
    public RtStuMultiLevelReview selectRtStuMultiLevelReviewByReviewerIdentity(String reviewerIdentity, String stuNo) {
        return rtStuMultiLevelReviewMapper.selectRtStuMultiLevelReviewByReviewerIdentity(reviewerIdentity, stuNo);
    }

    /**
     * 查询审核信息列表
     *
     * @param rtStuMultiLevelReview 审核信息
     * @return 审核信息
     */
    @Override
    public List<RtStuMultiLevelReview> selectRtStuMultiLevelReviewList(RtStuMultiLevelReview rtStuMultiLevelReview) {
        return rtStuMultiLevelReviewMapper.selectRtStuMultiLevelReviewList(rtStuMultiLevelReview);
    }

    /**
     * 新增审核信息
     *
     * @param rtStuMultiLevelReview 审核信息
     * @return 结果
     */
    @Override
    public int insertRtStuMultiLevelReview(RtStuMultiLevelReview rtStuMultiLevelReview) {
        rtStuMultiLevelReview.setReviewTime(DateUtils.getNowDate());
        rtStuMultiLevelReview.setReviewer(SecurityUtils.getLoginUser().getUser().getNickName());
        rtStuMultiLevelReview.setReviewerId(SecurityUtils.getUserId());
        return rtStuMultiLevelReviewMapper.insertRtStuMultiLevelReview(rtStuMultiLevelReview);
    }

    /**
     * 修改审核信息
     *
     * @param rtStuMultiLevelReview 审核信息
     * @return 结果
     */
    @Override
    public int updateRtStuMultiLevelReview(RtStuMultiLevelReview rtStuMultiLevelReview) {
        rtStuMultiLevelReview.setReviewTime(DateUtils.getNowDate());
        rtStuMultiLevelReview.setReviewer(SecurityUtils.getLoginUser().getUser().getNickName());
        rtStuMultiLevelReview.setReviewerId(SecurityUtils.getUserId());
        return rtStuMultiLevelReviewMapper.updateRtStuMultiLevelReview(rtStuMultiLevelReview);
    }

    /**
     * 批量删除审核信息
     *
     * @param ids 需要删除的审核信息主键
     * @return 结果
     */
    @Override
    public int deleteRtStuMultiLevelReviewByIds(Long[] ids) {
        return rtStuMultiLevelReviewMapper.deleteRtStuMultiLevelReviewByIds(ids);
    }

    /**
     * 删除审核信息信息
     *
     * @param id 审核信息主键
     * @return 结果
     */
    @Override
    public int deleteRtStuMultiLevelReviewById(Long id) {
        return rtStuMultiLevelReviewMapper.deleteRtStuMultiLevelReviewById(id);
    }

    /**
     * 更新审核信息并同时更新学生证补办状态
     *
     * @param rtStuMultiLevelReview 审核信息
     * @return 结果
     */
    @Override
    public int updateRtStuMultiLevelReviewWithStuIdReissue(RtStuMultiLevelReview rtStuMultiLevelReview) {
        rtStuMultiLevelReview.setReviewTime(DateUtils.getNowDate());
        rtStuMultiLevelReview.setReviewer(SecurityUtils.getLoginUser().getUser().getNickName());
        rtStuMultiLevelReview.setReviewerId(SecurityUtils.getUserId());
       int  result = rtStuMultiLevelReviewMapper.updateRtStuMultiLevelReviewWithStuIdReissue(rtStuMultiLevelReview);
       if (result > 0) {
           String messageContent = rtStuMultiLevelReview.getNotes();
           if (messageContent == null || messageContent.trim().isEmpty()) {
               messageContent = "你申请办理的学生证制作完成，长堽校区前往经管楼学工处1-1办公室领取，里建校区前往“一站式”学生社区大厅领取";
           }

           weChatUtil.sendTextMessage(rtStuMultiLevelReview.getStuNo(), messageContent);

       }
       return result;
    }


}
