PointsOperationController.java 8.44 KB
package com.topdraw.business.process.rest;

import com.topdraw.base.modules.annotation.AnonymousAccess;
import com.topdraw.base.modules.common.ResultInfo;
import com.topdraw.base.modules.exception.BadRequestException;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.business.module.user.weixin.service.UserWeixinService;
import com.topdraw.business.module.user.weixin.service.dto.UserWeixinDTO;
import com.topdraw.business.process.domian.TempCustomPointBean;
import com.topdraw.business.process.domian.TempPoints;
import com.topdraw.business.process.service.dto.CustomPointsResult;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.business.LocalConstants;
import com.topdraw.exception.GlobeExceptionMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author XiangHan
 * @date 2021-10-22
 */
@Api(tags = "PointsOperation管理")
@RestController
@RequestMapping("/uce/pointsOperation")
@Slf4j
public class PointsOperationController {

    @Autowired
    private UserTvService userTvService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private UserWeixinService userWeixinService;
    @Autowired
    private PointsOperationService pointsOperationService;

//    @Log("清除过期积分并计算总积分,供客户端会员查询积分时调用")
    @GetMapping(value = "/cleanInvalidPointsAndCalculateCurrentPoints/{id}")
    @ApiOperation("清除过期积分并计算总积分,供客户端会员查询积分时调用")
    @AnonymousAccess
    @Deprecated
    public ResultInfo cleanInvalidPointsAndCalculateCurrentPoints(@PathVariable("id") Long id) {
        Long aLong = this.pointsOperationService.cleanInvalidPointsAndCalculateCurrentPoints(id);
        return ResultInfo.success(Objects.isNull(aLong) ? 0L : aLong);
    }

//    @Log("手动发放积分")
    @PostMapping(value = "/addPoints")
    @ApiOperation("手动发放积分")
    @AnonymousAccess
    public ResultInfo addPoints(@Validated @RequestBody TempPoints tempPoints) {
        log.info("手动发放积分,参数 ==>>{} ", tempPoints);

        Long memberId = tempPoints.getMemberId();
        if (Objects.isNull(memberId)) {
            log.error("积分发放失败,参数错误,会员id 不存在");
            return ResultInfo.failure("积分发放失败,参数错误");
        }

        Long points = tempPoints.getPoints();
        if (Objects.isNull(points) || points <= 0L) {
            log.error("积分发放失败,参数错误,积分不存在或者积分小于0");
            return ResultInfo.failure("积分发放失败,参数错误");
        }

        MemberDTO memberDTO = this.memberService.findById(memberId);
        if (Objects.nonNull(memberDTO.getId())) {
            tempPoints.setMemberId(memberDTO.getId());
            tempPoints.setMemberCode(memberDTO.getCode());
            this.pointsOperationService.grantPointsByManualByTempPoints(tempPoints);
        }

        return ResultInfo.success();
    }

    /**
     * 通过用户账户id消耗积分
     * @param tempIptvUser
     * @return
     */
    @PostMapping(value = "/customPointsByUserTvPlatformAccount")
    @ApiOperation("通过大屏账户积分消耗")
    @AnonymousAccess
    public ResultInfo customPointsByUserTvPlatformAccount(@Validated @RequestBody TempCustomPointBean tempIptvUser) {
        String platformAccount = tempIptvUser.getPlatformAccount();
        Long points = tempIptvUser.getPoints();
        Long activityId = tempIptvUser.getActivityId();
        Integer evtType = tempIptvUser.getEvtType();
        Long mediaId = tempIptvUser.getMediaId();
        Long orderId = tempIptvUser.getOrderId();
        Integer deviceType = tempIptvUser.getDeviceType();

        TempPoints tempPoints = new TempPoints();
        UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
        if (Objects.nonNull(userTvDTO)) {
            Long memberId = userTvDTO.getMemberId();
            if (Objects.isNull(memberId)) {
//                return ResultInfo.failed("会员信息不存在");
                return ResultInfo.failure("会员信息不存在");
            }
            MemberDTO memberDTO = this.memberService.findById(memberId);
            tempPoints.setMemberCode(memberDTO.getCode());
            tempPoints.setMemberId(memberId);
        }

        tempPoints.setPoints(points);
        tempPoints.setDeviceType(deviceType);
        tempPoints.setAppCode(LocalConstants.APP_CODE_CHONGQING_CHONGSHU_VIS);
        tempPoints.setEvtType(evtType);
        tempPoints.setActivityId(activityId);
        tempPoints.setMediaId(mediaId);
        tempPoints.setOrderId(orderId);
        return this.customPoints(tempPoints);
    }

    /**
     * 通过用户账户id消耗积分
     * @param tempPoints
     * @return
     */
    @PostMapping(value = "/customPointsByUserId")
    @ApiOperation("积分消耗")
    @AnonymousAccess
    public ResultInfo customPointsByUserId(@Validated @RequestBody TempPoints tempPoints) {
        Long userId = tempPoints.getUserId();
        // 设备类型 1:大屏;2:小屏(微信)3.小屏(xx)
        Integer deviceType = tempPoints.getDeviceType();
        Long memberId = null;
        if (deviceType == LocalConstants.DEVICE_VIS) {
            memberId = this.getMemberIdByIpTvUserId(userId);
        }
        if (deviceType == LocalConstants.DEVICE_MOBILE) {
            memberId = this.getMemberIdByWeiXinUserId(userId);
        }
        if (Objects.isNull(memberId)) {
//           return ResultInfo.failed("会员信息不存在");
           return ResultInfo.failure("会员信息不存在");
        }
        tempPoints.setMemberId(memberId);
        return this.customPoints(tempPoints);
    }

    /**
     * 获取iptv账户对应的会员id
     * @param userId
     * @return
     */
    private Long getMemberIdByIpTvUserId(Long userId) {
        UserTvDTO userTvDTO = this.userTvService.findById(userId);
        return userTvDTO.getMemberId();
    }

    /**
     * 获取微信账户对应的会员id
     * @param userId
     * @return
     */
    private Long getMemberIdByWeiXinUserId(Long userId) {
        UserWeixinDTO userWeixinDTO = this.userWeixinService.findById(userId);
        return userWeixinDTO.getMemberId();
    }

    @PostMapping(value = "/consumePoints")
    @ApiOperation("积分消耗")
    @AnonymousAccess
    public ResultInfo customPoints(@Validated @RequestBody TempPoints tempPoints) {
        Integer pointsType = tempPoints.getPointsType();
        if (Objects.isNull(pointsType)) {
            tempPoints.setPointsType(0);
        }
        CustomPointsResult b = this.pointsOperationService.customPoints(tempPoints);
        String description = "操作成功";
        if (!b.isResult()) {
            description = "操作失败,积分不足";
        }
        return ResultInfo.success(Arrays.asList(b),description);
    }

    @PostMapping(value = "/consumeItemPoints")
    @ApiOperation("积分兑换商品")
    @AnonymousAccess
    public ResultInfo consumeItemPoints(@Validated @RequestBody TempPoints tempPoints) {
        log.info("pointsOperation ==>> consumeItemPoints ==>> param ==>> {}", tempPoints);
        Long memberId = tempPoints.getMemberId();
        Assert.notNull(memberId, GlobeExceptionMsg.MEMBER_ID_IS_NULL);
        Long points = tempPoints.getPoints();
        if (Objects.isNull(points) || points <= 0) {
            log.info("points ==>> {}", points);
            throw new BadRequestException("points error");
        }
        Long itemId = tempPoints.getItemId();
        Assert.notNull(itemId, "itemId can't be null");

        // 积分兑换商品
        tempPoints.setEvtType(30);
        tempPoints.setPointsType(0);
        CustomPointsResult b = this.pointsOperationService.customPoints(tempPoints);
        String description = "操作成功";
        if (!b.isResult()) {
            description = "操作失败,积分不足";
        }
        return ResultInfo.success(Arrays.asList(b),description);
    }



}