Helius
2022-05-27 4351e71d782741143a98f86f6648acd16689165f
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package com.matrix.system.shopXcx.action;
 
 
import com.alibaba.fastjson.JSON;
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.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.tools.ServiceUtil;
import com.matrix.system.hive.action.BaseController;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
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.util.ArrayList;
import java.util.List;
 
/**
 * @description 产品表
 * @author jyy
 * @date 2019-06-10 10:58
 */
@Controller
@RequestMapping(value = "admin/shopProduct")
public class ShopProductAction extends BaseController {
 
    @Autowired
    private ShopProductDao shopProductDao;
 
    @Autowired
    private ShopProductAttributeDao shopProductAttributeDao;
 
    @Autowired
    private ShopProductParamDao shopProductParamDao;
 
    @Autowired
    private ShopParamValueDao shopParamValueDao;
 
    @Autowired
    private ShopProductAttrRefDao shopProductAttrRefDao;
 
    @Autowired
    private ShopSkuDao shopSkuDao;
 
    @Autowired
    private ShopProductImgDao shopProductImgDao;
 
    @Autowired
    private ShopProductParamRefDao shopProductParamRefDao;
 
    @Autowired
    private ServiceUtil serviceUtil;
 
    //记录编辑前的值Before_Edit_Value
    public static final String BEV="ShopProduct_BEV";
    
    
    /**
     * 列表显示
     */
    @RequestMapping(value =  "/showList")
    public @ResponseBody AjaxResult showList(ShopProduct shopProduct, PaginationVO pageVo) {
        SysUsers sysUsers = getMe();
        pageVo.setSort("createTime");
        pageVo.setOrder("desc");
        shopProduct.setDelFlag(AppConstance.DATA_USEABLE);
        shopProduct.setCompanyId(sysUsers.getCompanyId());
        List<ShopProduct> dataList = shopProductDao.selectInPage(shopProduct, pageVo);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
                shopProductDao.selectTotalRecord(shopProduct));
        return result;
    }
       
    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @RemoveRequestToken    
       @RequestMapping(value =  "/addShopProduct")
    public @ResponseBody AjaxResult addShopProduct(ShopProduct shopProduct) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        shopProduct.setCreateBy(user.getSuName());
        shopProduct.setUpdateBy(user.getSuName());
        shopProduct.setCompanyId(user.getCompanyId());
        shopProduct.setDelFlag(AppConstance.DATA_USEABLE);
        if(StringUtils.isNotBlank(shopProduct.getAttrValues())){
            String attrs = shopProduct.getAttrValues().replace("|", "").replace("-", "");
            shopProduct.setAttrValues(attrs.trim());
        }
 
//        if (!user.getShopRole().equals(Dictionary.FLAG_YES_Y)) {
//            shopProduct.setShopIds(user.getShopId().toString());
//        }
 
         int i=shopProductDao.insert(shopProduct);
        if(i > 0){
            if(StringUtils.isNotBlank(shopProduct.getAttrs())){
                String[] attrs = shopProduct.getAttrs().split(",");
                List<ShopProductAttrRef> shopProductAttrRefs = new ArrayList<>();
                //产品属性关联表插入数据
                for (int j = 0; j < attrs.length; j++) {
                    ShopProductAttribute shopProductAttribute = shopProductAttributeDao.selectById(Integer.valueOf(attrs[j]));
                    ShopProductAttrRef  shopProductAttrRef = new ShopProductAttrRef();
                    shopProductAttrRef.setAttrId(shopProductAttribute.getAttrId());
                    shopProductAttrRef.setPId(shopProduct.getId());
                    shopProductAttrRef.setAttrFullPath(getParentName(shopProductAttribute.getAttrId())+"/"+shopProductAttribute.getAttrId()+"/");
                    shopProductAttrRef.setCreateBy(user.getSuName());
                    shopProductAttrRef.setUpdateBy(user.getSuName());
                    shopProductAttrRefs.add(shopProductAttrRef);
                }
                shopProductAttrRefDao.batchInsert(shopProductAttrRefs);
            }
            //插入产品规格数据
            if(StringUtils.isNotBlank(shopProduct.getShopSku())){
                List<ShopSku> shopSkus = JSON.parseArray(shopProduct.getShopSku(), ShopSku.class);
                List<ShopSku> newShopSkus = new ArrayList<ShopSku>();
                for(int k=0;k<shopSkus.size();k++){
                    ShopSku  shopSku = shopSkus.get(k);
                    if(StringUtils.isNotBlank(shopSku.getAtrid())){
                        boolean isRepetition = serviceUtil.addCheckRepeat("shop_sku", "atrid", shopSku.getAtrid());
                        if(isRepetition){
                            return  new AjaxResult(AjaxResult.STATUS_FAIL,"品种编码不能重复");
                        }
                    }
                    shopSku.setPId(shopProduct.getId());
                    shopSku.setCreateBy(user.getSuName());
                    shopSku.setUpdateBy(user.getSuName());
                    newShopSkus.add(shopSku);
                }
                shopSkuDao.batchInsert(newShopSkus);
            }
 
            //插入产品图片数据
            if(StringUtils.isNotBlank(shopProduct.getShopProductImg())){
                List<ShopProductImg> shopProductImgs = JSON.parseArray(shopProduct.getShopProductImg(), ShopProductImg.class);
                List<ShopProductImg> newShopProductImgs = new ArrayList<ShopProductImg>();
                for(int a=0;a<shopProductImgs.size();a++){
                    ShopProductImg shopProductImg = shopProductImgs.get(a);
                    shopProductImg.setPId(shopProduct.getId());
                    shopProductImg.setCreateBy(user.getSuName());
                    shopProductImg.setUpdateBy(user.getSuName());
                    newShopProductImgs.add(shopProductImg);
                }
                if(CollectionUtils.isNotEmpty(newShopProductImgs)){
                    shopProductImgDao.batchInsert(newShopProductImgs);
                }
            }
            //插入产品参数
            if(StringUtils.isNotBlank(shopProduct.getParamRefs())){
                List<ShopProductParamRef> shopProductParamRefs = JSON.parseArray(shopProduct.getParamRefs(), ShopProductParamRef.class);
                List<ShopProductParamRef> newShopProductParamRefs = new ArrayList<ShopProductParamRef>();
                for(int b=0;b<shopProductParamRefs.size();b++){
                    ShopProductParamRef shopProductParamRef = shopProductParamRefs.get(b);
                    shopProductParamRef.setPId(shopProduct.getId());
                    shopProductParamRef.setCreateBy(user.getSuName());
                    shopProductParamRef.setUpdateBy(user.getSuName());
                    newShopProductParamRefs.add(shopProductParamRef);
                }
                if(CollectionUtils.isNotEmpty(newShopProductParamRefs)){
 
                    shopProductParamRefDao.batchInsert(newShopProductParamRefs);
                }
            }
             return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "产品表");
         }else {
            throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
        }
    }
 
    /**
     * 递归查询父节点id
     * @param attrId
     * @return
     */
    private String getParentName(Integer attrId){
        ShopProductAttribute shopProductAttribute = shopProductAttributeDao.selectById(attrId);
        if(shopProductAttribute != null && shopProductAttribute.getParentId() != 0){
            String attrfullpath ="/" + shopProductAttribute.getParentId();
            String fullpath = getParentName(shopProductAttribute.getParentId());
            return fullpath + attrfullpath;
        }else{
            return "";
        }
    }
 
 
 
 
    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @RemoveRequestToken    
       @RequestMapping(value = "/modifyShopProduct")
    public @ResponseBody AjaxResult modifyShopProduct(ShopProduct newShopProduct) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        if(StringUtils.isNotBlank(newShopProduct.getAttrValues())){
            String attrs = newShopProduct.getAttrValues().replace("|", "").replace("-", "");
            newShopProduct.setAttrValues(attrs.trim());
        }
 
//        if (!user.getShopRole().equals(Dictionary.FLAG_YES_Y)) {
//            newShopProduct.setShopIds(user.getShopId().toString());
//        }
 
        int i = shopProductDao.updateByModel(newShopProduct);
        if (i > 0) {
            if(StringUtils.isNotBlank(newShopProduct.getAttrs())){
                String[] attrs = newShopProduct.getAttrs().split(",");
                shopProductAttrRefDao.deleteById(newShopProduct.getId());
                List<ShopProductAttrRef> shopProductAttrRefs = new ArrayList<>();
                //产品属性关联表插入数据
                for (int j = 0; j < attrs.length; j++) {
                    ShopProductAttribute shopProductAttribute = shopProductAttributeDao.selectById(Integer.valueOf(attrs[j]));
                    ShopProductAttrRef  shopProductAttrRef = new ShopProductAttrRef();
                    shopProductAttrRef.setAttrId(shopProductAttribute.getAttrId());
                    shopProductAttrRef.setPId(newShopProduct.getId());
                    shopProductAttrRef.setAttrFullPath(getParentName(shopProductAttribute.getAttrId())+"/"+shopProductAttribute.getAttrId()+"/");
                    shopProductAttrRef.setCreateBy(user.getSuName());
                    shopProductAttrRef.setUpdateBy(user.getSuName());
                    shopProductAttrRefs.add(shopProductAttrRef);
                }
                shopProductAttrRefDao.batchInsert(shopProductAttrRefs);
            }else{
                shopProductAttrRefDao.deleteById(newShopProduct.getId());
            }
            //插入产品规格数据
            if(StringUtils.isNotBlank(newShopProduct.getShopSku())){
                List<ShopSku> shopSkus = JSON.parseArray(newShopProduct.getShopSku(), ShopSku.class);
                List<ShopSku> skuList = shopSkuDao.selectByPid(newShopProduct.getId());
                List<Integer> ids = new ArrayList<>();
                if(skuList != null && skuList.size()>0){
                        //查找被删除的数据
                        List<ShopSku> list = getList(shopSkus, skuList);
                        for(int c=0;c<list.size();c++){
                            shopSkuDao.deleteById(list.get(c).getId());
                        }
 
                }
                for(int k=0;k<shopSkus.size();k++){
                    ShopSku  shopSku = shopSkus.get(k);
                    //id不为空做更新操作
                    if(shopSku.getId() != null){
                        ShopSku oldSku = shopSkuDao.selectById(shopSku.getId());
                        boolean isRepetition;
                        if(StringUtils.isNotBlank(shopSku.getAtrid())){
                            if(shopSku.getAtrid().equals(oldSku.getAtrid())){
                                isRepetition = serviceUtil.updateCheckRepeat("shop_sku", "atrid",oldSku.getAtrid(),"atrid",shopSku.getAtrid());
                            }else{
                                isRepetition = serviceUtil.addCheckRepeat("shop_sku","atrid",shopSku.getAtrid());
                            }
                            if(isRepetition){
                                return  new AjaxResult(AjaxResult.STATUS_FAIL,"品种编码不能重复");
                            }
                        }
                        shopSkuDao.updateByModel(shopSku);
                    }else{
                        shopSku.setPId(newShopProduct.getId());
                        shopSku.setCreateBy(user.getSuName());
                        shopSku.setUpdateBy(user.getSuName());
                        if(StringUtils.isNotBlank(shopSku.getAtrid())){
                            boolean isRepetition = serviceUtil.addCheckRepeat("shop_sku", "atrid", shopSku.getAtrid());
                            if(isRepetition){
                                return  new AjaxResult(AjaxResult.STATUS_FAIL,"品种编码不能重复");
                            }
                        }
                        shopSkuDao.insert(shopSku);
                    }
                }
            }else{
                shopSkuDao.deleteById(newShopProduct.getId());
            }
 
            //插入产品图片数据
            if(StringUtils.isNotBlank(newShopProduct.getShopProductImg())){
                shopProductImgDao.deleteById(newShopProduct.getId());
                List<ShopProductImg> shopProductImgs = JSON.parseArray(newShopProduct.getShopProductImg(), ShopProductImg.class);
                List<ShopProductImg> newShopProductImgs = new ArrayList<>();
                for(int a=0;a<shopProductImgs.size();a++){
                    ShopProductImg shopProductImg = shopProductImgs.get(a);
                    shopProductImg.setPId(newShopProduct.getId());
                    shopProductImg.setCreateBy(user.getSuName());
                    shopProductImg.setUpdateBy(user.getSuName());
                    newShopProductImgs.add(shopProductImg);
                }
                if(CollectionUtils.isNotEmpty(newShopProductImgs)){
                    shopProductImgDao.batchInsert(newShopProductImgs);
                }
            }else{
                shopProductImgDao.deleteById(newShopProduct.getId());
            }
            //插入产品参数
            if(StringUtils.isNotBlank(newShopProduct.getParamRefs())){
                shopProductParamRefDao.deleteById(newShopProduct.getId());
                List<ShopProductParamRef> shopProductParamRefs = JSON.parseArray(newShopProduct.getParamRefs(), ShopProductParamRef.class);
                List<ShopProductParamRef> newShopProductParamRefs = new ArrayList<ShopProductParamRef>();
                for(int b=0;b<shopProductParamRefs.size();b++){
                    ShopProductParamRef shopProductParamRef = shopProductParamRefs.get(b);
                    shopProductParamRef.setPId(newShopProduct.getId());
                    shopProductParamRef.setCreateBy(user.getSuName());
                    shopProductParamRef.setUpdateBy(user.getSuName());
                    newShopProductParamRefs.add(shopProductParamRef);
                }
                if(CollectionUtils.isNotEmpty(newShopProductParamRefs)){
                    shopProductParamRefDao.batchInsert(newShopProductParamRefs);
                }
            }else{
                shopProductParamRefDao.deleteById(newShopProduct.getId());
            }
            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) {
        SysUsers sysUsers = getMe();
        ShopProduct shopProduct = new ShopProduct();
        ModelAndView modelAndView = new ModelAndView("admin/shop/shopProduct-form");
        if (id != null) {
            shopProduct = shopProductDao.selectById(id);
 
            WebUtil.setSessionAttribute(BEV, shopProduct);
        }
        ShopProductParam shopProductParam = new ShopProductParam();
        shopProductParam.setIsStart(AppConstance.IS_START);
        List<ShopProductParam> paramList = shopProductParamDao.selectByModel(shopProductParam);
        if(CollectionUtils.isNotEmpty(paramList)){
            for (ShopProductParam bean :paramList) {
                List<ShopParamValue> valueList = shopParamValueDao.selectByParamId(bean.getParamId());
                bean.setValueList(valueList);
            }
        }
        modelAndView.addObject("user", sysUsers);
        modelAndView.addObject("obj",shopProduct);
        modelAndView.addObject("valueList",paramList);
 
        return modelAndView;
    }
       
       
       /**
     * 删除
     */  
     @RequestMapping(value = "/del")
    public @ResponseBody AjaxResult del(String keys) {
        List<String> ids = StringUtils.strToCollToString(keys, ",");
        ShopProduct shopProduct = new ShopProduct();
        shopProduct.setDelFlag(AppConstance.DATA_DISABLE);
        int i =  shopProductDao.updateByIds(ids,shopProduct);
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, ids.size());
        } else {
            throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
        }
    }
 
 
    @RequestMapping(value = "/getParam")
    @ResponseBody
    public AjaxResult getParam(Integer id){
        List<ShopSku> shopSkus = shopSkuDao.selectByPid(id);
        List<ShopProductImg> shopProductImg = shopProductImgDao.selectByPid(id);
        List<ShopProductAttrRef> attrRefs = shopProductAttrRefDao.selectByPid(id);
        List<ShopProductParamRef> paramRefs = shopProductParamRefDao.selectByPid(id);
        AjaxResult result=    new AjaxResult();
        result.setStatus(AjaxResult.STATUS_SUCCESS);
        result.putInMap("shopSkus",shopSkus);
        result.putInMap("shopImg",shopProductImg);
        result.putInMap("AttrRefs",attrRefs);
        result.putInMap("paramRefValues",paramRefs);
         return result;
    }
 
 
    //查找不不同对象
    private List<ShopSku> getList(List<ShopSku> list1,List<ShopSku> list2) {
        List<ShopSku> list = new ArrayList<>();
        for (ShopSku sku : list2) {
            if (!list1.contains(sku)) {
                list.add(sku);
            }
        }
        return list;
    }
 
    /**
     * @author jyy
     * 查询所有产品
     */
    @RequestMapping(value = "/getAll")
    public @ResponseBody
    AjaxResult getAll() {
        SysUsers sysUsers = getMe();
        ShopProduct shopProductParam = new ShopProduct();
        shopProductParam.setCompanyId(sysUsers.getCompanyId());
        shopProductParam.setDelFlag(AppConstance.DATA_USEABLE);
        List<ShopProduct> productList = shopProductDao.selectByModel(shopProductParam);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, productList, productList.size());
    }
 
    /**
     * 商品上上下架
     * @param ids
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(String ids,Integer status){
        List<String> shopIds = StringUtils.strToCollToString(ids, ",");
        ShopProduct shopProduct = new ShopProduct();
        shopProduct.setStatus(status);
        shopProductDao.updateByIds(shopIds, shopProduct);
        return  new AjaxResult(AjaxResult.STATUS_SUCCESS,"修改成功");
    }
}