Pu Zhibing
2025-04-28 e9fec855200d3abfda6c2e737e63ed7477abcbf7
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.UserCouponRecordMapper;
import com.stylefeng.guns.modular.system.model.UserCouponRecord;
import com.stylefeng.guns.modular.system.service.IUserCouponRecordService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
 
@Service
public class UserCouponRecordServiceImpl extends ServiceImpl<UserCouponRecordMapper, UserCouponRecord> implements IUserCouponRecordService {
 
    @Resource
    private UserCouponRecordMapper userCouponRecordMapper;
 
 
    /**
     * 获取可用优惠券数量
     * @param uid
     * @param companyId
     * @param state
     * @param couponUseType
     * @return
     * @throws Exception
     */
    @Override
    public int queryAvailable(Integer uid, Integer companyId, Integer state, Integer couponUseType, Double money) throws Exception {
        return userCouponRecordMapper.queryAvailable(uid, companyId, state, couponUseType, money);
    }
 
 
    /**
     * 获取优惠券列表
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryCoupon(Integer uid, Integer companyId, Integer state, Integer couponUseType, Double money, Integer pageNum, Integer size) throws Exception {
        pageNum = (pageNum - 1) * size;
        return userCouponRecordMapper.queryCoupon(uid, companyId, state, couponUseType, money, pageNum, size);
    }
    
    @Override
    public List<Map<String, Object>> queryCoupon1(Integer uid, String cityCode, Integer state, Integer couponUseType, Double money, Integer pageNum, Integer size) throws Exception {
        if(null != pageNum && null != size){
            pageNum = (pageNum - 1) * size;
        }
        List<Map<String, Object>> list = userCouponRecordMapper.queryCoupon1(uid, cityCode, state, couponUseType, money, pageNum, size);
        for (Map<String, Object> map : list) {
            Object citys = map.get("citys");
            if(null != citys){
                List<String> names = new ArrayList<>();
                String s = citys.toString();
                JSONArray jsonArray = JSON.parseArray(s);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String name = jsonObject.getString("name");
                    names.add(name);
                }
                map.put("citys", JSON.toJSONString(names));
            }
        }
        return list;
    }
    
    /**
     * 获取优惠券列表
     * @param state
     * @param pageNum
     * @param size
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryMyCoupons(Integer state, Integer pageNum, Integer size, Integer uid) throws Exception {
        pageNum = (pageNum - 1) * size;
        List<Map<String, Object>> list = userCouponRecordMapper.queryMyCoupons(state, pageNum, size, uid);
        for (Map<String, Object> map : list) {
            Object citys = map.get("citys");
            if(null != citys){
                List<String> names = new ArrayList<>();
                String s = citys.toString();
                JSONArray jsonArray = JSON.parseArray(s);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String name = jsonObject.getString("name");
                    names.add(name);
                }
                map.put("citys", JSON.toJSONString(names));
            }
        }
        return list;
    }
 
    /**
     * 删除优惠券
     * @param id
     * @param uid
     * @throws Exception
     */
    @Override
    public ResultUtil delMyCoupon(Integer id, Integer uid) throws Exception {
        UserCouponRecord userCouponRecord = userCouponRecordMapper.selectById(id);
        if(!userCouponRecord.getUserId().equals(uid)){
            return ResultUtil.error("您不能删除此优惠券");
        }
        userCouponRecordMapper.deleteById(id);
        return ResultUtil.success();
    }
 
 
    /**
     * 赠送优惠券
     * @param id
     * @param uid
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil handselCoupon(Integer id, Integer uid, Integer userId) throws Exception {
        UserCouponRecord userCouponRecord = userCouponRecordMapper.selectById(id);
        if(userCouponRecord.getUserId().compareTo(uid) != 0){
            return ResultUtil.error("您不能赠送此优惠券");
        }
        if(userCouponRecord.getState() != 1){
            return ResultUtil.error("优惠券已无法使用");
        }
        userCouponRecord.setUserId(userId);
        userCouponRecordMapper.updateById(userCouponRecord);
        return ResultUtil.success();
    }
 
 
    /**
     * 修改过期优惠券状态
     * @throws Exception
     */
    @Override
    public void updateTimeOut() throws Exception {
        userCouponRecordMapper.updateTimeOut();
    }
}