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 com.ruoyi.system.domain.CreditUser;
import com.ruoyi.system.domain.InterestsNumberTable;
import com.ruoyi.system.domain.InterestsTable;
import com.ruoyi.system.mapper.CreditUserMapper;
import com.ruoyi.system.mapper.InterestsNumberTableMapper;
import com.ruoyi.system.mapper.InterestsTableMapper;
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;
    @Autowired
    private CreditUserMapper creditUserMapper;
    @Autowired
    private InterestsNumberTableMapper interestsNumberTableMapper;
    @Autowired
    private InterestsTableMapper interestsTableMapper;

    /**
     * 查询使用记录
     * 
     * @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 AjaxResult insertUsageRecord(UsageRecord usageRecord)
    {
        //根据手机号查询使用人员信息
        CreditUser creditUser = new CreditUser();
        creditUser.setPhoneNum(usageRecord.getCreditPhone());
        creditUser = creditUserMapper.selectCreditUser(creditUser);
        //根据商家ID和分类id查询权益id
        InterestsTable tnterestsTable = interestsTableMapper.selectInterestsTable(usageRecord.getBusinessId(),usageRecord.getPolicyId());
        usageRecord.setInterestsId(tnterestsTable.getId());
        //查询用户权益等级对应的商家权益次数
        InterestsNumberTable interestsNumberTable = new InterestsNumberTable();
        interestsNumberTable.setInterestsId(usageRecord.getInterestsId());
        interestsNumberTable.setRightsRank(creditUser.getCreditLevel());
        interestsNumberTable = interestsNumberTableMapper.selectInterestsNumberTable(interestsNumberTable);
        //查询该用户已在该商家使用次数（按年来查询）
        String year =DateUtils.getCurrentYear();
        int count = usageRecordMapper.selectUsageRecordByRecordNum(usageRecord.getCreditPhone(),usageRecord.getInterestsId(),year);
        int index = (int) (interestsNumberTable.getNumber() - count);
        if(index<=0){
            return AjaxResult.error("当前用户无优惠次数，核销失败");
        }
        usageRecord.setSurplusNum(String.valueOf(index));
        usageRecord.setRecordYear(year);
        usageRecord.setCreditId(creditUser.getId());
        usageRecord.setRecordLevel(creditUser.getCreditLevel());
        usageRecord.setCreditName(creditUser.getUserName());
        usageRecord.setCreateTime(DateUtils.getNowDate());

        int c = usageRecordMapper.insertUsageRecord(usageRecord);

        return c > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改使用记录
     * 
     * @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);
    }
}
