package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.WeChatUser;
import com.ruoyi.common.enums.MeetingType;
import com.ruoyi.common.enums.ReviewStatus;
import com.ruoyi.common.enums.SysYesNo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CpsContentInfo;
import com.ruoyi.system.domain.CpsUsrApply;
import com.ruoyi.system.mapper.CpsContentInfoMapper;
import com.ruoyi.system.mapper.CpsUsrApplyMapper;
import com.ruoyi.system.service.ICpsUsrApplyService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * cps_usr_apply与会申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-03
 */
@Service
public class CpsUsrApplyServiceImpl implements ICpsUsrApplyService {
    @Autowired
    private CpsUsrApplyMapper cpsUsrApplyMapper;

    @Autowired
    private CpsContentInfoMapper cpsContentInfoMapper;

    /**
     * 查询cps_usr_apply与会申请
     *
     * @param id cps_usr_apply与会申请主键
     * @return cps_usr_apply与会申请
     */
    @Override
    public CpsUsrApply selectCpsUsrApplyById(Long id) {
        return cpsUsrApplyMapper.selectCpsUsrApplyById(id);
    }

    @Override
    public CpsUsrApply selectCpsUsrApplyByTel() {
        WeChatUser loginUser = (WeChatUser) SecurityUtils.getLoginUser();
        CpsUsrApply cpsUsrApply = cpsUsrApplyMapper.selectCpsUsrApplyByTel(loginUser.getTel(), MeetingType.MEETING.getCode());
        if (ObjectUtils.isEmpty(cpsUsrApply)) {
            return null;
        }
        if (ObjectUtils.isEmpty(cpsUsrApply.getContentId())) {
            return cpsUsrApply;
        }
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(cpsUsrApply.getContentId());
        cpsUsrApply.setContent(cpsContentInfo);
        return cpsUsrApply;
    }

    @Override
    public CpsUsrApply selectCpsUsrApplyThemeMeeting(String meetType) {
        WeChatUser loginUser = (WeChatUser) SecurityUtils.getLoginUser();
        CpsUsrApply cpsUsrApply = cpsUsrApplyMapper.selectCpsUsrApplyByTel(loginUser.getTel(), meetType);
        if (ObjectUtils.isEmpty(cpsUsrApply) || ObjectUtils.isEmpty(cpsUsrApply.getContentId())) {
            return null;
        }
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(cpsUsrApply.getContentId());
        if (ObjectUtils.isEmpty(cpsContentInfo)) {
            return null;
        }
        cpsUsrApply.setContent(cpsContentInfo);
        return cpsUsrApply;
    }

    /**
     * 查询cps_usr_apply与会申请列表
     *
     * @param cpsUsrApply cps_usr_apply与会申请
     * @return cps_usr_apply与会申请
     */
    @Override
    @DataScope(deptAlias = "usrApply")
    public List<CpsUsrApply> selectCpsUsrApplyList(CpsUsrApply cpsUsrApply) {
        return cpsUsrApplyMapper.selectCpsUsrApplyList(cpsUsrApply);
    }

    /**
     * 新增cps_usr_apply与会申请
     *
     * @param cpsUsrApply cps_usr_apply与会申请
     * @return 结果
     */
    @Override
    public int insertCpsUsrApply(CpsUsrApply cpsUsrApply) {
        cpsUsrApply.setCreateTime(DateUtils.getNowDate());
        return cpsUsrApplyMapper.insertCpsUsrApply(cpsUsrApply);
    }

    /**
     * 修改cps_usr_apply与会申请
     *
     * @param cpsUsrApply cps_usr_apply与会申请
     * @return 结果
     */
    @Override
    public int updateCpsUsrApply(CpsUsrApply cpsUsrApply) {
        cpsUsrApply.setUpdateTime(DateUtils.getNowDate());
        return cpsUsrApplyMapper.updateCpsUsrApply(cpsUsrApply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int rejectCpsUsrApply(CpsUsrApply cpsUsrApply) {
        cpsUsrApply.setUpdateTime(DateUtils.getNowDate());
        CpsUsrApply applyInfo = cpsUsrApplyMapper.selectCpsUsrApplyById(cpsUsrApply.getId());
        if (SysYesNo.YES.getCode().equals(applyInfo.getConferenceFlag())) {
            cpsUsrApplyMapper.updateCpsUsrApply(new CpsUsrApply() {{
                setId(applyInfo.getConferenceRel());
                setReviewMsg(cpsUsrApply.getReviewMsg());
                setReviewState(ReviewStatus.NO_PASS.getCode());
            }});
        }
        return cpsUsrApplyMapper.updateCpsUsrApply(cpsUsrApply);
    }

    /**
     * 批量删除cps_usr_apply与会申请
     *
     * @param ids 需要删除的cps_usr_apply与会申请主键
     * @return 结果
     */
    @Override
    public int deleteCpsUsrApplyByIds(Long[] ids) {
        return cpsUsrApplyMapper.deleteCpsUsrApplyByIds(ids);
    }

    /**
     * 删除cps_usr_apply与会申请信息
     *
     * @param id cps_usr_apply与会申请主键
     * @return 结果
     */
    @Override
    public int deleteCpsUsrApplyById(Long id) {
        return cpsUsrApplyMapper.deleteCpsUsrApplyById(id);
    }
}
