package com.backendsys.service.System;

import com.backendsys.aspect.HttpRequestAspect;
import com.backendsys.entity.System.SysResourcePointsDTO;
import com.backendsys.entity.System.SysUserDTO;
import com.backendsys.entity.System.SysUserPointsDTO;
import com.backendsys.enums.UserPointActivityType;
import com.backendsys.enums.UserPointOperatorType;
import com.backendsys.exception.CustException;
import com.backendsys.mapper.System.SysResourcePointsMapper;
import com.backendsys.mapper.System.SysUserMapper;
import com.backendsys.mapper.System.SysUserPointsHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysResourceServiceImpl implements SysResourceService {

    @Autowired
    private HttpRequestAspect httpRequestAspect;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserPointsHistoryMapper sysUserPointsHistoryMapper;

    @Autowired
    private SysResourcePointsMapper sysResourcePointsMapper;

    /**
     * 消耗积分 (查询对应积分，查询我的积分，更新我的积分，新增积分记录)
     */
    @Override
    public void consumptionPoint(String resource_type, String resource_tag, Integer num) {

        // 1.查询积分资源
        SysResourcePointsDTO sysResourcePointsDTO = new SysResourcePointsDTO();
        sysResourcePointsDTO.setResource_type(resource_type);
        sysResourcePointsDTO.setResource_tag(resource_tag);
        SysResourcePointsDTO resourcePointDetail = sysResourcePointsMapper.queryResourcePointsDetail(sysResourcePointsDTO);

        Float consumption_point = num == null ? resourcePointDetail.getResource_points() : (resourcePointDetail.getResource_points() * num);

        // 判断积分资源状态是否启用 (如果不可用，就不扣除积分了)
        System.out.println(resourcePointDetail);
        if (resourcePointDetail.getStatus() == 1) {

            Long user_id = httpRequestAspect.getUserId();

            // 2.查询我的积分
            SysUserPointsDTO sysUserPointsDTO = new SysUserPointsDTO();
            sysUserPointsDTO.setTarget_user_id(user_id);
            Map<String, Object> myPointDetail = sysUserMapper.queryUserPoint(sysUserPointsDTO);
            Float my_point = (Float) myPointDetail.get("point_balance");

            if (consumption_point > my_point) {
                throw new CustException("用户积分不足，当前积分: " + my_point + ", 需要积分: " + consumption_point);
            }

            // 3.更新我的积分
            Float pointBalance = my_point - consumption_point;

            SysUserDTO sysUserDTO = new SysUserDTO();
            sysUserDTO.setUser_id(user_id);
            sysUserDTO.setPoint_balance(pointBalance);
            sysUserMapper.updateUserInfo(sysUserDTO);

            // 4.新增积分记录
            // { target_user_id } 上面已经赋值
            sysUserPointsDTO.setPoint_adjustment(-(consumption_point));
            sysUserPointsDTO.setPoint_balance(pointBalance);
            sysUserPointsDTO.setActivity_type(UserPointActivityType.USED.getCode());
            sysUserPointsDTO.setActivity_type_description(UserPointActivityType.USED.getDescription());

            sysUserPointsDTO.setActivity_detail(resourcePointDetail.getResource_name());
            sysUserPointsDTO.setOperator_type(UserPointOperatorType.SYSTEM.getCode());
            sysUserPointsHistoryMapper.insertUserPointsHistory(sysUserPointsDTO);

        }
    }

}
