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

import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.points.available.service.PointsAvailableService;
import com.topdraw.business.module.points.available.service.dto.PointsAvailableQueryCriteria;
import com.topdraw.business.module.points.available.service.dto.PointsAvailableQueryType;
import com.topdraw.business.module.points.detail.service.PointsDetailService;
import com.topdraw.business.module.points.detail.service.dto.PointsDetailQueryCriteria;
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.domian.result.CustomPointsResult;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.common.ResultInfo;
import com.topdraw.config.LocalConstants;
import com.topdraw.util.TimestampUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
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("/ucEngine/api/pointsOperation")
public class PointsOperationController {

    private static final Logger LOG = LoggerFactory.getLogger(PointsOperationController.class);

    @Autowired
    PointsOperationService pointsOperationService;
    @Autowired
    PointsDetailService pointsDetailService;
    @Autowired
    PointsAvailableService pointsAvailableService;
    @Autowired
    UserTvService userTvService;
    @Autowired
    UserWeixinService userWeixinService;
    @Autowired
    MemberService memberService;


    @GetMapping(value = "/pagePointsDetails")
    @ApiOperation("查询PointsDetail")
    public ResultInfo pagePointsDetails(PointsDetailQueryCriteria criteria, Pageable pageable) {
        return ResultInfo.successPage(pointsDetailService.queryAll(criteria,pageable));
    }

    @GetMapping(value = "/pageAvailablePoints")
    @ApiOperation("查询PointsAvailable")
    public ResultInfo pageAvailablePoints(PointsAvailableQueryCriteria criteria, Pageable pageable) {
        PointsAvailableQueryType queryType = criteria.getQueryType();
        // 可用
        if (queryType == PointsAvailableQueryType.AVAILABLE_ONLY) {
            criteria.setExpireTime(TimestampUtil.now());
        }
        return ResultInfo.successPage(pointsAvailableService.queryAll(criteria,pageable));
    }

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

    @PostMapping(value = "/grantPointsByManual")
    @ApiOperation("手动发放积分")
    public ResultInfo grantPointsByManual(@Validated @RequestBody TempPoints tempPoints) {
        Long memberId = tempPoints.getMemberId();
        Long points = tempPoints.getPoints();
        Assert.notNull(memberId,"memberId can't be null!");
        Assert.notNull(points,"points can't be null!");
        MemberDTO memberDTO = this.memberService.findById(memberId);
        if (Objects.nonNull(memberDTO)) {
            String code = memberDTO.getCode();
            Assert.notNull(code,"code can't be null!");
            tempPoints.setMemberCode(code);
            this.pointsOperationService.grantPointsByManualByTempPoints(tempPoints);
        }
        return ResultInfo.success();
    }

    /**
     * 通过用户账户id消耗积分
     * @param tempIptvUser
     * @return
     */
    @PostMapping(value = "/customPointsByUserTvPlatformAccount")
    @ApiOperation("通过大屏账户积分消耗")
    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("会员信息不存在");
            }
            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("积分消耗")
    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("会员信息不存在");
        }
        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("积分消耗")
    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);
    }



}