package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ProposalInfo;
import com.ruoyi.system.domain.ProposalUnitReply;
import com.ruoyi.system.domain.urge.ZxUrge;
import com.ruoyi.system.mapper.ProposalInfoMapper;
import com.ruoyi.system.mapper.ProposalUnitReplyMapper;
import com.ruoyi.system.mapper.ZxUrgeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IZxUrgeService;

/**
 * 政协催办Service业务层处理
 * 
 * @author boman
 * @date 2024-03-12
 */
@Service
public class ZxUrgeServiceImpl implements IZxUrgeService 
{
    @Autowired
    private ZxUrgeMapper zxUrgeMapper;
    @Autowired
    private ProposalInfoMapper proposalInfoMapper;
    @Autowired
    private ProposalUnitReplyMapper proposalUnitReplyMapper;

    /**
     * 查询政协催办
     * 
     * @param zxUrgeId 政协催办主键
     * @return 政协催办
     */
    @Override
    public ZxUrge selectZxUrgeByZxUrgeId(Long zxUrgeId)
    {
        return zxUrgeMapper.selectZxUrgeByZxUrgeId(zxUrgeId);
    }

    /**
     * 查询政协催办列表
     * 
     * @param zxUrge 政协催办
     * @return 政协催办
     */
    @Override
    public List<ZxUrge> selectZxUrgeList(ZxUrge zxUrge)
    {
        return zxUrgeMapper.selectZxUrgeList(zxUrge);
    }

    /**
     * 新增政协催办
     * 
     * @param zxUrge 政协催办
     * @return 结果
     */
    @Override
    public int insertZxUrge(ZxUrge zxUrge)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //查询提案或者社情民意
        if("1".equals(zxUrge.getUrgeType())){
            //提案
            ProposalInfo proposalInfo = proposalInfoMapper.selectProposalInfoByProposalId(zxUrge.getScoreId());
            //查询主办单位
            ProposalUnitReply proposalUnitReply = new ProposalUnitReply();
            proposalUnitReply.setProposalId(zxUrge.getScoreId());
            proposalUnitReply.setType("2");
            List<ProposalUnitReply> proposalUnitReplies = proposalUnitReplyMapper.selectProposalUnitReplyList(proposalUnitReply);
            if(proposalUnitReplies!=null && proposalUnitReplies.size()>0){
                zxUrge.setAcceptDeptId(proposalUnitReplies.get(0).getDeptId());
                zxUrge.setAcceptDept(proposalUnitReplies.get(0).getDeptName());
            }else {
                throw new ServiceException("当前提案未分配主办单位，催办失败！");
            }
            zxUrge.setUrgeTitle(proposalInfo.getTitle()+"的催办");
            zxUrge.setIssuerId(user.getUserId());
            zxUrge.setIssuer(user.getNickName());
        }else{

        }
        zxUrge.setUrgeTime(DateUtils.getNowDate());
        zxUrge.setCreateTime(DateUtils.getNowDate());
        return zxUrgeMapper.insertZxUrge(zxUrge);
    }

    /**
     * 修改政协催办
     * 
     * @param zxUrge 政协催办
     * @return 结果
     */
    @Override
    public int updateZxUrge(ZxUrge zxUrge)
    {
        zxUrge.setUpdateTime(DateUtils.getNowDate());
        return zxUrgeMapper.updateZxUrge(zxUrge);
    }

    /**
     * 批量删除政协催办
     * 
     * @param zxUrgeIds 需要删除的政协催办主键
     * @return 结果
     */
    @Override
    public int deleteZxUrgeByZxUrgeIds(Long[] zxUrgeIds)
    {
        return zxUrgeMapper.deleteZxUrgeByZxUrgeIds(zxUrgeIds);
    }

    /**
     * 删除政协催办信息
     * 
     * @param zxUrgeId 政协催办主键
     * @return 结果
     */
    @Override
    public int deleteZxUrgeByZxUrgeId(Long zxUrgeId)
    {
        return zxUrgeMapper.deleteZxUrgeByZxUrgeId(zxUrgeId);
    }
}
