package com.ruoyi.system.service.impl;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UsageRecordMapper;
import com.ruoyi.system.domain.UsageRecord;
import com.ruoyi.system.service.IUsageRecordService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 使用记录Service业务层处理
 * 
 * @author boman
 * @date 2022-06-13
 */
@Service
public class UsageRecordServiceImpl implements IUsageRecordService 
{
    @Autowired
    private UsageRecordMapper usageRecordMapper;

    /**
     * 查询使用记录
     * 
     * @param recordId 使用记录主键
     * @return 使用记录
     */
    @Override
    public UsageRecord selectUsageRecordByRecordId(Long recordId)
    {
        return usageRecordMapper.selectUsageRecordByRecordId(recordId);
    }

    /**
     * 查询使用记录列表
     * 
     * @param usageRecord 使用记录
     * @return 使用记录
     */
    @Override
    public List<UsageRecord> selectUsageRecordList(UsageRecord usageRecord)
    {
        return usageRecordMapper.selectUsageRecordList(usageRecord);
    }

    /**
     * 新增使用记录
     * 
     * @param usageRecord 使用记录
     * @return 结果
     */
    @Override
    public int insertUsageRecord(UsageRecord usageRecord)
    {
        usageRecord.setCreateTime(DateUtils.getNowDate());
        return usageRecordMapper.insertUsageRecord(usageRecord);
    }

    /**
     * 修改使用记录
     * 
     * @param usageRecord 使用记录
     * @return 结果
     */
    @Override
    public int updateUsageRecord(UsageRecord usageRecord)
    {
        usageRecord.setUpdateTime(DateUtils.getNowDate());
        return usageRecordMapper.updateUsageRecord(usageRecord);
    }

    /**
     * 批量删除使用记录
     * 
     * @param recordIds 需要删除的使用记录主键
     * @return 结果
     */
    @Override
    public int deleteUsageRecordByRecordIds(Long[] recordIds)
    {
        return usageRecordMapper.deleteUsageRecordByRecordIds(recordIds);
    }

    /**
     * 删除使用记录信息
     * 
     * @param recordId 使用记录主键
     * @return 结果
     */
    @Override
    public int deleteUsageRecordByRecordId(Long recordId)
    {
        return usageRecordMapper.deleteUsageRecordByRecordId(recordId);
    }

    /**
     * 根据使用人id查询列表
     * @param recordId
     * @return
     */
    @Override
    public AjaxResult selectUsageRecordListByRecordId(Long recordId) {
        startPage();
        List<UsageRecord> usageRecords = usageRecordMapper.selectUsageRecordListByRecordId(recordId);
        Map<String, List<UsageRecord>> collect = usageRecords.stream().sorted(Comparator.comparing(UsageRecord::getRecordYear,Comparator.reverseOrder())).collect(Collectors.groupingBy(UsageRecord::getRecordYear, LinkedHashMap::new, Collectors.toList()));
        return AjaxResult.success(collect);
    }
}
