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.DriveSchoolDynamic;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInfo;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolDynamicMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolDynamicService;
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 DriveSchoolDynamicServiceImpl implements IDriveSchoolDynamicService
{
    @Autowired
    private DriveSchoolDynamicMapper driveSchoolDynamicMapper;

    @Resource
    private AdminUserApi userApi;
    /**
     * 查询行业动态
     *
     * @param id 行业动态主键
     * @return 行业动态
     */
    @Override
    public DriveSchoolDynamic selectDriveSchoolDynamicById(Long id)
    {
        return driveSchoolDynamicMapper.selectDriveSchoolDynamicById(id);
    }

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;
    /**
     * 查询行业动态列表
     *
     * @param driveSchoolDynamic 行业动态
     * @return 行业动态
     */
    @Override
    public IPage<DriveSchoolDynamic> selectDriveSchoolDynamicList(DriveSchoolDynamic driveSchoolDynamic, Page<DriveSchoolDynamic> page)
    {
        Long deptId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolDynamic.getDeptId())){
            deptId = driveSchoolDynamic.getDeptId();
            DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.selectDriveSchoolInfoById(deptId);
            deptId = driveSchoolInfo.getDeptId();
        }else {
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            AdminUserRespDTO user = userApi.getUser(userId);
            deptId = user.getDeptId();
        }
        driveSchoolDynamic.setDeptId(deptId);
        return driveSchoolDynamicMapper.selectDriveSchoolDynamicList(driveSchoolDynamic,page);
    }

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

    /**
     * 新增行业动态
     *
     * @param driveSchoolDynamic 行业动态
     * @return 结果
     */
    @Override
    public int insertDriveSchoolDynamic(DriveSchoolDynamic driveSchoolDynamic)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolDynamic.setDeptId(deptId);
        return driveSchoolDynamicMapper.insertDriveSchoolDynamic(driveSchoolDynamic);
    }

    /**
     * 修改行业动态
     *
     * @param driveSchoolDynamic 行业动态
     * @return 结果
     */
    @Override
    public int updateDriveSchoolDynamic(DriveSchoolDynamic driveSchoolDynamic)
    {
        //driveSchoolDynamic.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolDynamicMapper.updateDriveSchoolDynamic(driveSchoolDynamic);
    }

    /**
     * 批量删除行业动态
     *
     * @param ids 需要删除的行业动态主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolDynamicByIds(Long[] ids)
    {
        return driveSchoolDynamicMapper.deleteDriveSchoolDynamicByIds(ids);
    }

    /**
     * 删除行业动态信息
     *
     * @param id 行业动态主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolDynamicById(Long id)
    {
        return driveSchoolDynamicMapper.deleteDriveSchoolDynamicById(id);
    }
}
