Helius
2021-03-10 087cd794187727bf9a34fcef3027cd56f6dec436
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.matrix.system.shopXcx.action;
 
 
import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.shopXcx.bean.ShopCoupon;
import com.matrix.system.shopXcx.bean.ShopProduct;
import com.matrix.system.shopXcx.dao.ShopCouponDao;
import com.matrix.system.shopXcx.dao.ShopProductDao;
import org.apache.commons.collections.CollectionUtils;
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.beans.Transient;
import java.util.*;
 
/**
 * @author jyy
 * @description 优惠券
 * @date 2019-06-12 15:17
 */
@Controller
@RequestMapping(value = "admin/shopCoupon")
public class ShopCouponAction {
 
    @Autowired
    private ShopCouponDao shopCouponDao;
 
    @Autowired
    private ShopProductDao shopProductDao;
 
 
 
 
 
 
    //记录编辑前的值Before_Edit_Value
    public static final String BEV = "ShopCoupon_BEV";
 
 
    /**
     * 列表显示
     */
    @RequestMapping(value = "/showList")
    public @ResponseBody
    AjaxResult showList(ShopCoupon shopCoupon, PaginationVO pageVo) {
 
        pageVo.setSort("update_time");
        pageVo.setOrder("desc");
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        if (!user.getShopRole().equals(Dictionary.FLAG_YES_Y)) {
            shopCoupon.setShopId(user.getShopId());
        }
        shopCoupon.setCompanyId(user.getCompanyId());
        List<ShopCoupon> dataList = shopCouponDao.selectInPage(shopCoupon, pageVo);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList, shopCouponDao.selectTotalRecord(shopCoupon));
    }
 
    /**
     * 新增
     */
    @RemoveRequestToken
    @RequestMapping(value = "/addShopCoupon")
    @Transient
    public @ResponseBody
    AjaxResult addShopCoupon(ShopCoupon shopCoupon) {
        //如果结束时间早于开始时间
        if (shopCoupon.getBeginTime().after(shopCoupon.getEndTime())) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "结束时间不得早于开始时间!");
        }
        //如果抵消金额大于最低消费金额
        if (shopCoupon.getOffsetAmount().compareTo(shopCoupon.getMinAmount()) == 1) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "抵消金额不能大于最低消费金额(满减)!");
        }
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        shopCoupon.setCreateBy(user.getSuName());
        shopCoupon.setUpdateBy(user.getSuName());
        shopCoupon.setQuantityReceive(0);
        //设置为未开始状态
        shopCoupon.setCouponStatus(1);
        //如果是全部产品可以使用
        if (shopCoupon.getIsAll() == 1) {
            shopCoupon.setProductIds("");
            shopCoupon.setAttrIds("");
        }
        // @TODO wzy增加
        shopCoupon.setShopId(user.getShopId());
        shopCoupon.setCompanyId(user.getCompanyId());
        int i = shopCouponDao.insert(shopCoupon);
 
        productLabeling(shopCoupon);
 
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "优惠券");
        } else {
            throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
        }
    }
 
 
    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/modifyShopCoupon")
    public @ResponseBody
    AjaxResult modifyShopCoupon(ShopCoupon newShopCoupon) {
        //如果结束时间早于开始时间
        if (newShopCoupon.getBeginTime().after(newShopCoupon.getEndTime())) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "结束时间不得早于开始时间!");
        }
        //如果抵消金额大于最低消费金额
        if (newShopCoupon.getOffsetAmount().compareTo(newShopCoupon.getMinAmount()) == 1) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "抵消金额不能大于最低消费金额(满减)!");
        }
        //如果所有产品都优惠或没有选择产品
        if (newShopCoupon.getIsAll() == 1 || newShopCoupon.getProductIds() == null) {
            newShopCoupon.setProductIds("");
        }
        //如果所有产品都优惠或没有选择属性
        if (newShopCoupon.getIsAll() == 1 || newShopCoupon.getAttrIds() == null) {
            newShopCoupon.setAttrIds("");
        }
        ShopCoupon oldShopCoupon = WebUtil.getSessionAttribute(BEV);
        int i = 0;
        Map<String, Object> modifyMap = null;
        try {
            if (!ModelUtils.isModified(oldShopCoupon, newShopCoupon)) {
                i = MatrixConstance.DML_SUCCESSS;
            }
            modifyMap = ModelUtils.comparePojo2Map(oldShopCoupon, newShopCoupon);
        } catch (Exception e) {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newShopCoupon);
        }
        if (modifyMap.size() > 0) {
            modifyMap.put("id", oldShopCoupon.getId());
            shopCouponDao.updateByMap(modifyMap);
        }
        i = MatrixConstance.DML_SUCCESSS;
        WebUtil.removeSessionAttribute(BEV);
        if (i > 0) {
            shopCouponDao.updateCouponStatusById(newShopCoupon.getId());
            if (newShopCoupon.getKind() == 2) {
                // @TODO wzy增加
                SysUsers users  = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
                newShopCoupon.setShopId(users.getShopId());
 
                //更新产品标签
                shopProductDao.removeProductCouponLable(newShopCoupon.getId());
                productLabeling(newShopCoupon);
 
            }
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "优惠券");
        } else {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
        }
    }
 
 
    /**
     * 进入修改界面
     */
    @SaveRequestToken
    @RequestMapping(value = "/editForm")
    public ModelAndView editForm(Integer id) {
        ShopCoupon shopCoupon = new ShopCoupon();
        ModelAndView modelAndView = new ModelAndView("admin/shop/shopCoupon-form");
        if (id != null) {
            shopCoupon = shopCouponDao.selectById(id);
            WebUtil.setSessionAttribute(BEV, shopCoupon);
        }
        modelAndView.addObject("obj", shopCoupon);
        return modelAndView;
    }
 
 
    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    @Transactional(rollbackFor = Exception.class)
    public @ResponseBody
    AjaxResult del(String keys) {
        List<String> ids = StringUtils.strToCollToString(keys, ",");
        int i = shopCouponDao.deleteByIds(ids);
        for (String couponId : ids) {
            shopProductDao.removeProductCouponLable(Integer.valueOf(couponId));
        }
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
        } else {
            throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
        }
    }
 
    /**
     * 批量启用
     */
    @RequestMapping(value = "/startUsing")
    @Transactional(rollbackFor = Exception.class)
    public @ResponseBody
    AjaxResult startUsing(String keys) {
        List<String> ids = StringUtils.strToCollToString(keys, ",");
        List<String> newIds = new ArrayList<>();
        Date now = new Date();
        //如果结束已经晚于当前时间则跳过不开启
        for (String couponId : ids) {
            Integer id = Integer.valueOf(couponId);
            ShopCoupon shopCoupon = shopCouponDao.selectById(id);
            if (shopCoupon.getEndTime().after(now)) {
                newIds.add(couponId);
                //更新产品标签
                if (shopCoupon.getKind() == 2) {
                    //更新产品标签
                    productLabeling(shopCoupon);
                }
            }
        }
        int i = 0;
        if (CollectionUtils.isNotEmpty(newIds)) {
            i = shopCouponDao.updateStateByStateAndIds(newIds, 1);
        }
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, i);
        } else {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
        }
    }
 
    /**
     * 批量停用
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/blockUp")
    public @ResponseBody
    AjaxResult blockUp(String keys) {
        List<String> ids = StringUtils.strToCollToString(keys, ",");
        int i = shopCouponDao.updateStateByStateAndIds(ids, 2);
        for (String couponId : ids) {
            shopProductDao.removeProductCouponLable(Integer.valueOf(couponId));
        }
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, i);
        } else {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
        }
    }
 
 
    /**
     * 给产品打上合适的标签
     *
     * @param shopCoupon
     */
    private void productLabeling(ShopCoupon shopCoupon) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        Date now = new Date();
        Boolean needUpdate = false;
        //优惠券处于可用状态
        if (shopCoupon.getIsOpen() == 1 && shopCoupon.getBeginTime().before(now) &&
                shopCoupon.getEndTime().after(now)) {
            //查询优惠券适配的所有产品
            List<ShopProduct> shopProductList = new ArrayList<>();
            if (shopCoupon.getIsAll() == 1) {
                ShopProduct productQuery = new ShopProduct();
                productQuery.setDelFlag(2);
                productQuery.setCompanyId(user.getCompanyId());
                shopProductList = shopProductDao.selectByModel(productQuery);
            } else {
                if (StringUtils.isNotBlank(shopCoupon.getProductIds())) {
                    //根据产品id查询,合适的产品
                    shopProductList.addAll(shopProductDao.selectProductByIds(shopCoupon.getProductIds(), shopCoupon.getShopId()));
                }
                if (StringUtils.isNotBlank(shopCoupon.getAttrIds())) {
                    List<Integer> pIds = new ArrayList<>();
                    //根据属性id查询合适的属性
                    List<String> attrIds = StringUtils.strToColl(shopCoupon.getAttrIds(), ",");
                    for (String attrId : attrIds) {
                        List<ShopProduct> tempList = shopProductDao.selectProductByAttrid(attrId, shopCoupon.getShopId());
                        for (ShopProduct product : tempList) {
                            if (!pIds.contains(product.getId())) {
                                shopProductList.add(product);
                            }
                        }
                    }
                }
            }
            //更新优惠券标签
            if (CollectionUtils.isNotEmpty(shopProductList)) {
                for (ShopProduct product : shopProductList) {
                    if (product.getCouponId() != null) {
                        ShopCoupon coupon = shopCouponDao.selectById(product.getCouponId());
                        //之前的活动存在,且正在进行中
                        if (coupon != null && coupon.getCouponStatus() == 2) {
                            //比较活动金额
                            if (coupon.getMinAmount().compareTo(shopCoupon.getMinAmount()) <= 0) {
                                //当前优惠活动力度大则更新
                                needUpdate = true;
                            }
                        } else {
                            needUpdate = true;
                        }
                    } else {
                        needUpdate = true;
                    }
                    if (needUpdate) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", product.getId());
                        map.put("couponId", shopCoupon.getId());
                        shopProductDao.updateByMap(map);
                    }
                }
            }
        } else {
            //清除标签
            shopProductDao.removeProductCouponLable(shopCoupon.getId());
        }
    }
}