package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInfo;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolNews;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolNewsMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolNewsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thoughtworks.xstream.core.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 校内新闻Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-10
 */
@Service
public class DriveSchoolNewsServiceImpl implements IDriveSchoolNewsService
{
    @Autowired
    private DriveSchoolNewsMapper driveSchoolNewsMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询校内新闻
     *
     * @param id 校内新闻主键
     * @return 校内新闻
     */
    @Override
    public DriveSchoolNews selectDriveSchoolNewsById(Long id)
    {
        return driveSchoolNewsMapper.selectDriveSchoolNewsById(id);
    }


    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;
    /**
     * 查询校内新闻列表
     *
     * @param driveSchoolNews 校内新闻
     * @return 校内新闻
     */
    @Override
    public IPage<DriveSchoolNews> selectDriveSchoolNewsList(DriveSchoolNews driveSchoolNews, Page<DriveSchoolNews> page)
    {
        Long deptId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolNews.getDeptId())){
            deptId = driveSchoolNews.getDeptId();
            DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.selectDriveSchoolInfoById(deptId);
            deptId = driveSchoolInfo.getDeptId();
        }else {
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            AdminUserRespDTO user = userApi.getUser(userId);
            deptId = user.getDeptId();
        }
        driveSchoolNews.setDeptId(deptId);
        return driveSchoolNewsMapper.selectDriveSchoolNewsList(driveSchoolNews,page);
    }

    @Override
    public List<DriveSchoolNews> selectDriveSchoolNewsListAll(DriveSchoolNews driveSchoolNews)
    {
        Long deptId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolNews.getDeptId())){
            deptId = driveSchoolNews.getDeptId();
            DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.selectDriveSchoolInfoById(deptId);
            deptId = driveSchoolInfo.getDeptId();
        }else {
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            AdminUserRespDTO user = userApi.getUser(userId);
            deptId = user.getDeptId();
        }
        driveSchoolNews.setDeptId(deptId);
        return driveSchoolNewsMapper.selectDriveSchoolNewsListAll(driveSchoolNews);
    }

    /**
     * 新增校内新闻
     *
     * @param driveSchoolNews 校内新闻
     * @return 结果
     */
    @Override
    public int insertDriveSchoolNews(DriveSchoolNews driveSchoolNews)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolNews.setDeptId(deptId);
        return driveSchoolNewsMapper.insertDriveSchoolNews(driveSchoolNews);
    }

    /**
     * 修改校内新闻
     *
     * @param driveSchoolNews 校内新闻
     * @return 结果
     */
    @Override
    public int updateDriveSchoolNews(DriveSchoolNews driveSchoolNews)
    {
        //driveSchoolNews.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolNewsMapper.updateDriveSchoolNews(driveSchoolNews);
    }

    /**
     * 批量删除校内新闻
     *
     * @param ids 需要删除的校内新闻主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolNewsByIds(Long[] ids)
    {
        return driveSchoolNewsMapper.deleteDriveSchoolNewsByIds(ids);
    }

    /**
     * 删除校内新闻信息
     *
     * @param id 校内新闻主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolNewsById(Long id)
    {
        return driveSchoolNewsMapper.deleteDriveSchoolNewsById(id);
    }
}
