package com.ruoyi.abuwx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.abuwx.domain.InfoContent;
import com.ruoyi.abuwx.domain.InfoOperationRecord;
import com.ruoyi.abuwx.mapper.InfoContentMapper;
import com.ruoyi.abuwx.mapper.InfoOperationRecordMapper;
import com.ruoyi.abuwx.service.IInfoContentService;
import com.ruoyi.abuwx.vo.InfoContentVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 资讯信息Service业务层处理
 *
 * @author lujie
 * @date 2024-12-27
 */
@Service
public class InfoContentServiceImpl implements IInfoContentService {
    @Autowired
    private InfoContentMapper infoContentMapper;
    @Autowired
    private InfoOperationRecordMapper infoOperationRecordMapper;

    /**
     * 查询资讯信息
     *
     * @param id 资讯信息主键
     * @return 资讯信息
     */
    @Override
    public InfoContent selectInfoContentById(Long id) {
        return infoContentMapper.selectInfoContentById(id);
    }

    /**
     * 查询资讯信息列表
     *
     * @param infoContent 资讯信息
     * @return 资讯信息
     */
    @Override
    public List<InfoContent> selectInfoContentList(InfoContent infoContent) {
        return infoContentMapper.selectInfoContentList(infoContent);
    }

    /**
     * 新增资讯信息
     *
     * @param infoContent 资讯信息
     * @return 结果
     */
    @Override
    public int insertInfoContent(InfoContent infoContent) {
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        infoContent.setCreateTime(nowDate);
        infoContent.setCreateBy(username);
        int i = infoContentMapper.insertInfoContent(infoContent);
        if (i > 0) {
            InfoOperationRecord infoOperationRecord = new InfoOperationRecord();
            infoOperationRecord.setOperateType("创建");
            infoOperationRecord.setCreateBy(username);
            infoOperationRecord.setCreateTime(nowDate);
            infoOperationRecord.setInfoId(infoContent.getId());
            infoOperationRecordMapper.insertInfoOperationRecord(infoOperationRecord);
            return 1;
        }
        return 0;
    }

    /**
     * 修改资讯信息
     *
     * @param infoContent 资讯信息
     * @return 结果
     */
    @Override
    public int updateInfoContent(InfoContent infoContent) {
        InfoOperationRecord infoOperationRecord = new InfoOperationRecord();
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        infoContent.setUpdateTime(DateUtils.getNowDate());
        String checkStatus = infoContent.getCheckStatus();
        infoOperationRecord.setOperateType("修改");
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("5")) {
            infoContent.setPublishTime(nowDate);
            infoOperationRecord.setOperateType("发布");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("6")) {
            infoContent.setCancelTime(nowDate);
            infoOperationRecord.setOperateType("撤回发布");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("2")) {
            infoOperationRecord.setOperateType("发起申请");
        }
        if (StrUtil.isNotBlank(checkStatus) && checkStatus.equals("3")) {
            infoOperationRecord.setOperateType("审核");
        }
        infoOperationRecord.setCreateBy(username);
        infoOperationRecord.setCreateTime(nowDate);
        infoOperationRecord.setInfoId(infoContent.getId());
        infoOperationRecordMapper.insertInfoOperationRecord(infoOperationRecord);
        return infoContentMapper.updateInfoContent(infoContent);
    }

    /**
     * 批量删除资讯信息
     *
     * @param ids 需要删除的资讯信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoContentByIds(Long[] ids) {
        return infoContentMapper.deleteInfoContentByIds(ids);
    }

    /**
     * 删除资讯信息信息
     *
     * @param id 资讯信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoContentById(Long id) {
        return infoContentMapper.deleteInfoContentById(id);
    }

    @Override
    public List<InfoContentVo> selectInfoContentVoList(InfoContent infoContent) {
        return infoContentMapper.selectInfoContentVoList(infoContent);
    }
}

