package com.ygtx.gxt.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.common.utils.SecurityUtils;
import com.ygtx.gxt.mapper.GxtUserRuntimeScoreMapper;
import com.ygtx.gxt.domain.GxtUserRuntimeScore;
import com.ygtx.gxt.service.IGxtUserRuntimeScoreService;

/**
 * 维保运行天数得分Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-05
 */
@Service
public class GxtUserRuntimeScoreServiceImpl implements IGxtUserRuntimeScoreService 
{
    @Autowired
    private GxtUserRuntimeScoreMapper gxtUserRuntimeScoreMapper;

    /**
     * 查询维保运行天数得分
     * 
     * @param id 维保运行天数得分主键
     * @return 维保运行天数得分
     */
    @Override
    public GxtUserRuntimeScore selectGxtUserRuntimeScoreById(Long id)
    {
        return gxtUserRuntimeScoreMapper.selectGxtUserRuntimeScoreById(id);
    }

    /**
     * 查询维保运行天数得分列表
     * 
     * @param gxtUserRuntimeScore 维保运行天数得分
     * @return 维保运行天数得分
     */
    @Override
    public List<GxtUserRuntimeScore> selectGxtUserRuntimeScoreList(GxtUserRuntimeScore gxtUserRuntimeScore)
    {
        return gxtUserRuntimeScoreMapper.selectGxtUserRuntimeScoreList(gxtUserRuntimeScore);
    }

    /**
     * 根据rtType和月份查询维保运行天数得分列表
     * 
     * @param rtType 查询类型 1-部门所有用户  其他-当前用户
     * @param monthPeriod 月份
     * @return 维保运行天数得分集合
     */
    @Override
    public List<GxtUserRuntimeScore> selectGxtUserRuntimeScoreListByRtTypeAndMonth(Integer rtType, String monthPeriod, Long userId, Long deptId, String permission) {
        if (rtType==null) {
            rtType=0;
        }
        String[] parts = monthPeriod.split("-");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]) - 1; // Calendar.MONTH 是 0-based（0=1月）

        // 2. 获取该月的最后一刻（23:59:59.999）
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为当月1号
        calendar.add(Calendar.MONTH, 1); // 加1个月（下个月）
        calendar.add(Calendar.DAY_OF_MONTH, -1); // 减1天（当月最后一天）

        // 设置到当天的最后一毫秒（23:59:59.999）
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        Date lastDayOfMonth = calendar.getTime();

        // 3. 获取当前时间
        Date selTime = new Date();
        // 4. 比较
        if (selTime.after(lastDayOfMonth)) {
            selTime = lastDayOfMonth;
        }
        GxtUserRuntimeScore runtimeScore = new GxtUserRuntimeScore();
        runtimeScore.setPermission(permission);
        if (rtType == 2) {
            runtimeScore.setEndTime(selTime);
            runtimeScore.setDeptId(deptId);
            return gxtUserRuntimeScoreMapper.selectCurrentMonthDeptUserRuntimeScore(runtimeScore);
        } else if (rtType == 1) {
            runtimeScore.setDeptId(deptId);
        } else {
            runtimeScore.setUserId(userId);
        }
        runtimeScore.setMonthPeriod(monthPeriod);
        return gxtUserRuntimeScoreMapper.selectGxtUserRuntimeScoreList(runtimeScore);
    }


    @Override
    public List<GxtUserRuntimeScore> selectRuntimeScoreListByRtTypeAndMonth(Integer rtType, String monthPeriod, Long userId, Long deptId, String permission) {
        if (rtType==null) {
            rtType=0;
        }
        String[] parts = monthPeriod.split("-");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]) - 1; // Calendar.MONTH 是 0-based（0=1月）

        // 2. 获取该月的最后一刻（23:59:59.999）
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为当月1号
        calendar.add(Calendar.MONTH, 1); // 加1个月（下个月）
        calendar.add(Calendar.DAY_OF_MONTH, -1); // 减1天（当月最后一天）

        // 设置到当天的最后一毫秒（23:59:59.999）
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        Date lastDayOfMonth = calendar.getTime();

        // 3. 获取当前时间
        Date selTime = new Date();
        // 4. 比较
        if (selTime.after(lastDayOfMonth)) {
            selTime = lastDayOfMonth;
        }
        GxtUserRuntimeScore runtimeScore = new GxtUserRuntimeScore();
        runtimeScore.setPermission(permission);
        runtimeScore.setEndTime(selTime);
        runtimeScore.setDeptId(deptId);
        return gxtUserRuntimeScoreMapper.selectMonthDeptUserRuntimeScore(runtimeScore);
    }

    /**
     * 新增维保运行天数得分
     * 
     * @param gxtUserRuntimeScore 维保运行天数得分
     * @return 结果
     */
    @Override
    public int insertGxtUserRuntimeScore(GxtUserRuntimeScore gxtUserRuntimeScore)
    {
        return gxtUserRuntimeScoreMapper.insertGxtUserRuntimeScore(gxtUserRuntimeScore);
    }

    /**
     * 修改维保运行天数得分
     * 
     * @param gxtUserRuntimeScore 维保运行天数得分
     * @return 结果
     */
    @Override
    public int updateGxtUserRuntimeScore(GxtUserRuntimeScore gxtUserRuntimeScore)
    {
        return gxtUserRuntimeScoreMapper.updateGxtUserRuntimeScore(gxtUserRuntimeScore);
    }

    /**
     * 批量更新维保运行天数得分
     * 
     * @param gxtUserRuntimeScores 维保运行天数得分列表
     * @return 结果
     */
    @Override
    public int updateGxtUserRuntimeScores(List<GxtUserRuntimeScore> gxtUserRuntimeScores) {
        int result = 0;
        for (GxtUserRuntimeScore gxtUserRuntimeScore : gxtUserRuntimeScores) {
            result += gxtUserRuntimeScoreMapper.updateGxtUserRuntimeScore(gxtUserRuntimeScore);
        }
        return result;
    }

    /**
     * 批量删除维保运行天数得分
     * 
     * @param ids 需要删除的维保运行天数得分主键
     * @return 结果
     */
    @Override
    public int deleteGxtUserRuntimeScoreByIds(Long[] ids)
    {
        return gxtUserRuntimeScoreMapper.deleteGxtUserRuntimeScoreByIds(ids);
    }

    /**
     * 删除维保运行天数得分信息
     * 
     * @param id 维保运行天数得分主键
     * @return 结果
     */
    @Override
    public int deleteGxtUserRuntimeScoreById(Long id)
    {
        return gxtUserRuntimeScoreMapper.deleteGxtUserRuntimeScoreById(id);
    }
}