package com.ruoyi.frequency.platformrecord.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.enums.PlatformRecordEnums;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.platformrecord.entity.PlatformRecord;
import com.ruoyi.frequency.platformrecord.mapper.PlatformRecordMapper;
import com.ruoyi.frequency.platformrecord.model.result.PlatformRecordCount;
import com.ruoyi.frequency.platformrecord.service.PlatformRecordService;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 平台流水记录Service业务层处理
 *
 * @author liwenlong
 * @date 2023-12-01
 */
@Service
public class PlatformRecordServiceImpl extends ServiceImpl<PlatformRecordMapper, PlatformRecord>  implements PlatformRecordService
{
    @Autowired
    private PlatformRecordMapper platformRecordMapper;

    /**
     * 查询平台流水记录
     *
     * @param id 平台流水记录主键
     * @return 平台流水记录
     */
    @Override
    public PlatformRecord selectPlatformRecordById(Long id)
    {
        return platformRecordMapper.selectPlatformRecordById(id);
    }

    @Autowired
    private UserService userService;

    /**
     * 查询平台流水记录列表
     *
     * @param platformRecord 平台流水记录
     * @return 平台流水记录
     */
    @Override
    public List<PlatformRecord> selectPlatformRecordList(PlatformRecord platformRecord)
    {

        List<PlatformRecord> platformRecords = platformRecordList(platformRecord);

        if (platformRecords.size() > 0){

            Set<UserVo> userVoSet = platformRecords.stream().map(s -> new UserVo(s.getUserType(), s.getUserId())).collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            platformRecords.stream().forEach(s->{
                s.setObjectTypeName(PlatformRecordEnums.getName(s.getObjectType()));
                s.setUserInfo(userMap.get(new UserVo(s.getUserType(), s.getUserId())));
            });
        }

        return platformRecords;
    }

    private List<PlatformRecord> platformRecordList(PlatformRecord platformRecord){
        List<PlatformRecord> platformRecords = platformRecordMapper.selectPlatformRecordList(platformRecord);
        return platformRecords;
    }

    @Override
    public PlatformRecordCount statistics(PlatformRecord platformRecord) {
        List<PlatformRecord> platformRecords = platformRecordList(platformRecord);
        List<Long>  platformRecordList = platformRecords.stream().map(x -> x.getId()).collect(Collectors.toList());
        platformRecordList.add(-1L);

        return platformRecordMapper.statistics(platformRecordList);
    }

    /**
     * 新增平台流水记录
     *
     * @param platformRecord 平台流水记录
     * @return 结果
     */
    @Override
    public int insertPlatformRecord(PlatformRecord platformRecord)
    {
        platformRecord.setCreateData();
        return platformRecordMapper.insert(platformRecord);
    }

    /**
     * 修改平台流水记录
     *
     * @param platformRecord 平台流水记录
     * @return 结果
     */
    @Override
    public int updatePlatformRecord(PlatformRecord platformRecord)
    {
        platformRecord.setUpdateData();
        return platformRecordMapper.updatePlatformRecord(platformRecord);
    }

    /**
     * 批量删除平台流水记录
     *
     * @param ids 需要删除的平台流水记录主键
     * @return 结果
     */
    @Override
    public int deletePlatformRecordByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(PlatformRecord::getDelFlag,delFlag).in(PlatformRecord::getId,ids).update();
        return ids.length;
    }

    @Override
    public void addPlatformRecord(UserVo userVo, Long objectId, BigDecimal price, PlatformRecordEnums enums) {
        if (price.setScale(2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(0)) != 0) {

            PlatformRecord platformRecord = new PlatformRecord()
                    .setObjectId(objectId)
                    .setUserType(userVo.getUserType())
                    .setUserId(userVo.getUserId())
                    .setPrice(price)
                    .setObjectType(enums.getCode());

            //获取平台资金池金额
            BigDecimal balance = platformRecordMapper.platformAmountCount();
            platformRecord.setBeforeBalance(balance);
            platformRecord.setBalance(balance.add(price));

            this.save(platformRecord);
        }
    }

}
