| 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,"修改成功"); | 
|     } | 
| } |