935090232@qq.com
2021-01-26 ea8780c9aba82b8e8705ece4608c2477c340c742
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.matrix.system.shopXcx.action;
 
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.service.BizUserService;
import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.shopXcx.bean.ShopScoreExchange;
import com.matrix.system.shopXcx.dao.ShopScoreExchangeDao;
import com.matrix.system.shopXcx.pojo.ExchangeScorePOJO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import java.util.Date;
 
 
/**
 * @author jyy
 * @description 微商城会员积分控制器
 * @date 2019-06-05 10:15
 */
@Controller
@RequestMapping(value = "admin/bizUserScore")
public class ShopBizUserScoreAction {
 
    @Autowired
    private BizUserService bizUserService;
    @Autowired
    private ShopScoreExchangeDao shopScoreExchangeDao;
 
    /**
     * 记录编辑前的值Before_Edit_Value
     */
    public static final String BEV = "ShopBizUserScore_BEV";
 
 
    /**
     * 进入积分兑换界面
     */
    @SaveRequestToken
    @RequestMapping(value = "/intoExchangePage")
    public ModelAndView exchangeScore(String userId) {
        BizUser bizUser = new BizUser();
        ModelAndView modelAndView = new ModelAndView("admin/shop/user-score-exchange");
        if (StringUtils.isNotBlank(userId)) {
            bizUser = bizUserService.findById(userId);
            if (bizUser.getCurrentScore() == null) {
                bizUser.setCurrentScore(0);
            }
        }
        modelAndView.addObject("obj", bizUser);
        return modelAndView;
    }
 
 
    /**
     * 兑换积分
     */
    @Transactional(rollbackFor = Exception.class)
    @RemoveRequestToken
    @RequestMapping(value = "/exchangeScore")
    public @ResponseBody
    AjaxResult exchangeScore(ExchangeScorePOJO pojo) {
        //校验参数中用户ID和用户兑换积分是否有效
        if (pojo == null || StringUtils.isBlank(pojo.getUserId())
                || pojo.getScore() == null || pojo.getScore() == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "提交参数有误!");
        }
        //兑换积分用户信息
        BizUser bizUser = bizUserService.findById(pojo.getUserId());
        if (bizUser == null) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "没有查询到相应用户!");
        }
        if (bizUser.getCurrentScore() == null) {
            bizUser.setCurrentScore(0);
        }
        //如果兑换积分大于用户当前积分
        if (pojo.getScore() > bizUser.getCurrentScore()) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "兑换积分大于用户当前积分!");
        }
        //剩余积分 = 当前积分 - 兑换积分
        Integer remainScore = bizUser.getCurrentScore() - pojo.getScore();
        //当前操作用户信息
        SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        //修改用户剩余积分
        BizUser updateModel = new BizUser();
        updateModel.setUserId(bizUser.getUserId());
        updateModel.setCurrentScore(remainScore);
        updateModel.setUpdateBy(loginUser.getSuName());
        updateModel.setUpdateTime(new Date());
        bizUserService.modifyByModel(updateModel);
        //添加积分兑换记录
        ShopScoreExchange insertExchange = new ShopScoreExchange();
        insertExchange.setCreateBy(loginUser.getSuName());
        insertExchange.setUpdateBy(loginUser.getSuName());
        insertExchange.setUserId(bizUser.getOpenId());
        insertExchange.setExchangeScore(pojo.getScore());
        insertExchange.setOperateId(loginUser.getSuId());
        insertExchange.setRemark(pojo.getRemarks());
        shopScoreExchangeDao.insert(insertExchange);
 
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "兑换成功!");
    }
 
 
}