package com.ruoyi.system.service.impl.fgw;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fgw.FgwXmsb;
import com.ruoyi.system.domain.projectV2.ZsyzSbbzb;
import com.ruoyi.system.domain.projectV2.common.SendSmsConfig;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.fgw.FgwXmsbMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.fgw.FgwLdpsMapper;
import com.ruoyi.system.domain.fgw.FgwLdps;
import com.ruoyi.system.service.fgw.IFgwLdpsService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.constant.CommonConstants.LEADER;

/**
 * 发改委_领导批示Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-24
 */
@Service
public class FgwLdpsServiceImpl implements IFgwLdpsService 
{
    @Autowired
    private FgwLdpsMapper fgwLdpsMapper;
    @Autowired
    private FgwXmsbMapper fgwXmsbMapper;

    /**
     * 查询发改委_领导批示
     * 
     * @param id 发改委_领导批示主键
     * @return 发改委_领导批示
     */
    @Override
    public FgwLdps selectFgwLdpsById(Long id)
    {
        return fgwLdpsMapper.selectFgwLdpsById(id);
    }

    /**
     * 查询发改委_领导批示列表
     * 
     * @param fgwLdps 发改委_领导批示
     * @return 发改委_领导批示
     */
    @Override
    public List<FgwLdps> selectFgwLdpsList(FgwLdps fgwLdps)
    {

        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        String roleKey = "";
        // 如果是超级管理员/管理员，则不过滤数据
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0) {
            roleKey = roles.get(0).getRoleKey();
        }
        if (LEADER.equals(roleKey)) {
            //判断账号权限是不是领导，领导只能看见自己的
            fgwLdps.setPsnrId(currentUser.getUserId());
        }
        return fgwLdpsMapper.selectFgwLdpsList(fgwLdps);
    }

    /**
     * 新增发改委_领导批示
     * 
     * @param fgwLdps 发改委_领导批示
     * @return 结果
     */
    @Override
    public int insertFgwLdps(FgwLdps fgwLdps)
    {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        fgwLdps.setDeptId(user.getDeptId());
        fgwLdps.setPsr(user.getNickName());
        fgwLdps.setXgr(user.getUserName());
        fgwLdps.setPsnrId(user.getUserId());
        fgwLdps.setXgsj(DateUtils.getNowDate());
        fgwLdps.setCjsj(DateUtils.getNowDate());
        if(!"1".equals(fgwLdps.getType())){
            //修改主表中的领导批示内容
            FgwXmsb fgwXmsb = new FgwXmsb();
            fgwXmsb.setId(fgwLdps.getXmId());
            fgwXmsb.setPsnr(fgwLdps.getPsnr());
            fgwXmsbMapper.updateFgwXmsb(fgwXmsb);

        }

        return fgwLdpsMapper.insertFgwLdps(fgwLdps);
    }

    /**
     * 修改发改委_领导批示
     * 
     * @param fgwLdps 发改委_领导批示
     * @return 结果
     */
    @Override
    public int updateFgwLdps(FgwLdps fgwLdps)
    {

        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        fgwLdps.setXgsj(DateUtils.getNowDate());
        fgwLdps.setXgr(user.getUserName());
        fgwLdps.setPsr(user.getNickName());
        if(!"1".equals(fgwLdps.getType())){
            fgwLdps.setZcpsnr("");
            //修改主表中的领导批示内容
            FgwXmsb fgwXmsb = new FgwXmsb();
            fgwXmsb.setId(fgwLdps.getXmId());
            fgwXmsb.setPsnr(fgwLdps.getPsnr());
            fgwXmsbMapper.updateFgwXmsb(fgwXmsb);
        }

        return fgwLdpsMapper.updateFgwLdps(fgwLdps);
    }

    /**
     * 批量删除发改委_领导批示
     * 
     * @param ids 需要删除的发改委_领导批示主键
     * @return 结果
     */
    @Override
    public int deleteFgwLdpsByIds(Long[] ids)
    {
        return fgwLdpsMapper.deleteFgwLdpsByIds(ids);
    }

    /**
     * 删除发改委_领导批示信息
     * 
     * @param id 发改委_领导批示主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFgwLdpsById(Long id,Long xmId)
    {
        //查询最新的领导批示信息
        List<FgwLdps> fgwLdpsList = fgwLdpsMapper.selectFgwLdpsNew(xmId);
        if(fgwLdpsList.size()==1){
            //如果只有一条数据，则说明当前删除的就是最新的
            FgwXmsb fgwXmsb = new FgwXmsb();
            fgwXmsb.setId(xmId);
            fgwXmsb.setPsnr("");
            fgwXmsbMapper.updateFgwXmsb(fgwXmsb);
        }else{
            //判断第一条是否与要删除的ID相同
            if(fgwLdpsList.get(0).getId().equals(id)){
                //将主表中的批示内容换成第二条
                FgwXmsb fgwXmsb = new FgwXmsb();
                fgwXmsb.setId(xmId);
                fgwXmsb.setPsnr(fgwLdpsList.get(1).getPsnr());
                fgwXmsbMapper.updateFgwXmsb(fgwXmsb);
            }
        }
        return fgwLdpsMapper.deleteFgwLdpsById(id);
    }

    @Override
    public FgwLdps getInfoZc(FgwLdps fgwLdps) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        fgwLdps.setPsnrId(user.getUserId());
        return fgwLdpsMapper.getInfoZc(fgwLdps);
    }
}
