package com.ruoyi.iit.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.iit.domain.IitFilePush;
import com.ruoyi.iit.domain.IitPushUserRel;
import com.ruoyi.iit.mapper.IitFileInfoMapper;
import com.ruoyi.iit.mapper.IitFilePushMapper;
import com.ruoyi.iit.mapper.IitPushUserRelMapper;
import com.ruoyi.iit.service.IIitFilePushService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 推送管理Service业务层处理
 *
 * @author Hao Li
 * @date 2025-04-11
 */
@Service
public class IitFilePushServiceImpl implements IIitFilePushService {
    @Autowired
    private IitFilePushMapper iitFilePushMapper;

    @Autowired
    private IitFileInfoMapper iitFileInfoMapper;

    @Autowired
    private IitPushUserRelMapper iitPushUserRelMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询推送管理
     *
     * @param id 推送管理主键
     * @return 推送管理
     */
    @Override
    public IitFilePush selectIitFilePushById(Long id) {
        return iitFilePushMapper.selectIitFilePushById(id);
    }

    /**
     * 查询推送管理列表
     *
     * @param iitFilePush 推送管理
     * @return 推送管理
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<IitFilePush> selectIitFilePushList(IitFilePush iitFilePush) {
        return iitFilePushMapper.selectIitFilePushList(iitFilePush);
    }

    /**
     * 新增推送管理
     *
     * @param iitFilePush 推送管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertIitFilePush(IitFilePush iitFilePush) {
        // 组装基础数据
        iitFilePush.setUserNumber(iitFilePush.getUserIds().size());
        iitFilePush.setUserId(SecurityUtils.getUserId());
        iitFilePush.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        iitFilePush.setDeptId(SecurityUtils.getDeptId());
        iitFilePush.setCreateTime(DateUtils.getNowDate());
        int insertResult = iitFilePushMapper.insertIitFilePush(iitFilePush);
        // 更新文件的推送次数
        iitFileInfoMapper.updatePushTimes(iitFilePush.getFileId());

        // 发送推送通知
        Long pushId = iitFilePush.getId();
        Date pushTime = DateUtils.getNowDate();
        List<IitPushUserRel> pushUserRelList = new ArrayList<>();
        for (Long userId : iitFilePush.getUserIds()) {
            IitPushUserRel pushUserRel = new IitPushUserRel();
            pushUserRel.setPushId(pushId);
            pushUserRel.setUserId(userId);
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            pushUserRel.setUserName(sysUser.getNickName());
            pushUserRel.setPushTime(pushTime);
            pushUserRel.setIsView(0);
            pushUserRelList.add(pushUserRel);
        }
        iitPushUserRelMapper.insertIitPushUserRelBatch(pushUserRelList);

        return insertResult;
    }

    /**
     * 修改推送管理
     *
     * @param iitFilePush 推送管理
     * @return 结果
     */
    @Override
    public int updateIitFilePush(IitFilePush iitFilePush) {
        return iitFilePushMapper.updateIitFilePush(iitFilePush);
    }

    /**
     * 批量删除推送管理
     *
     * @param ids 需要删除的推送管理主键
     * @return 结果
     */
    @Override
    public int deleteIitFilePushByIds(Long[] ids) {
        return iitFilePushMapper.deleteIitFilePushByIds(ids);
    }

    /**
     * 删除推送管理信息
     *
     * @param id 推送管理主键
     * @return 结果
     */
    @Override
    public int deleteIitFilePushById(Long id) {
        return iitFilePushMapper.deleteIitFilePushById(id);
    }
}
