package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.StaffManage;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.staffTrainTime.StaffTrainTime;
import com.ruoyi.system.mapper.StaffTrainTimeMapper;
import com.ruoyi.system.service.IStaffTrainTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 员工培训时长Service业务层处理
 *
 * @author boman
 * @date 2025-02-21
 */
@Service
public class StaffTrainTimeServiceImpl implements IStaffTrainTimeService {
    @Autowired
    private StaffTrainTimeMapper staffTrainTimeMapper;

    /**
     * 查询员工培训时长
     *
     * @param trainTimeId 员工培训时长主键
     * @return 员工培训时长
     */
    @Override
    public StaffTrainTime selectStaffTrainTimeByTrainTimeId(Long trainTimeId) {
        return staffTrainTimeMapper.selectStaffTrainTimeByTrainTimeId(trainTimeId);
    }

    /**
     * 查询员工培训时长列表
     *
     * @param staffTrainTime 员工培训时长
     * @return 员工培训时长
     */
    @Override
    public List<StaffTrainTime> selectStaffTrainTimeList(StaffTrainTime staffTrainTime) {
        //根据时长进行排序
        List<StaffTrainTime> staffTrainTimes = staffTrainTimeMapper.selectStaffTrainTimeList(staffTrainTime);
        if (staffTrainTimes != null) {
            staffTrainTimes.sort(Comparator.comparingInt(StaffTrainTime::getStaffTime));
        }
        return staffTrainTimes;
    }

    /**
     * 新增员工培训时长
     *
     * @param staffTrainTime 员工培训时长
     * @return 结果
     */
    @Override
    public int insertStaffTrainTime(StaffTrainTime staffTrainTime) {
        staffTrainTime.setUserId(SecurityUtils.getUserId());
        //判断是否已经有这个员工的信息
        List<StaffTrainTime> staffTrainTimes = staffTrainTimeMapper.selectStaffTrainTimeList(staffTrainTime);
        if (staffTrainTimes != null && staffTrainTimes.size() > 0) {
            int staffTime = staffTrainTimes.get(0).getStaffTime();
            //更新阅读时长
            staffTrainTime.setStaffTime(staffTime + staffTrainTime.getStaffTime());
            Long trainTimeId = staffTrainTimes.get(0).getTrainTimeId();
            staffTrainTime.setTrainTimeId(trainTimeId);
            return staffTrainTimeMapper.updateStaffTrainTime(staffTrainTime);
        }
        StaffManage staffManage = SecurityUtils.getLoginUser().getStaffManage();
        if (staffManage != null){
            staffTrainTime.setStaffId(staffManage.getStaffId());
            staffTrainTime.setStaffName(staffManage.getStaffName());
            staffTrainTime.setCreateTime(DateUtils.getNowDate());
            return staffTrainTimeMapper.insertStaffTrainTime(staffTrainTime);
        }

        return 1;
    }

    /**
     * 修改员工培训时长
     *
     * @param staffTrainTime 员工培训时长
     * @return 结果
     */
    @Override
    public int updateStaffTrainTime(StaffTrainTime staffTrainTime) {
        staffTrainTime.setUpdateTime(DateUtils.getNowDate());
        return staffTrainTimeMapper.updateStaffTrainTime(staffTrainTime);
    }

    /**
     * 批量删除员工培训时长
     *
     * @param trainTimeIds 需要删除的员工培训时长主键
     * @return 结果
     */
    @Override
    public int deleteStaffTrainTimeByTrainTimeIds(Long[] trainTimeIds) {
        return staffTrainTimeMapper.deleteStaffTrainTimeByTrainTimeIds(trainTimeIds);
    }

    /**
     * 删除员工培训时长信息
     *
     * @param trainTimeId 员工培训时长主键
     * @return 结果
     */
    @Override
    public int deleteStaffTrainTimeByTrainTimeId(Long trainTimeId) {
        return staffTrainTimeMapper.deleteStaffTrainTimeByTrainTimeId(trainTimeId);
    }
}
