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 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 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 shopSkus = JSON.parseArray(shopProduct.getShopSku(), ShopSku.class); List newShopSkus = new ArrayList(); for(int k=0;k shopProductImgs = JSON.parseArray(shopProduct.getShopProductImg(), ShopProductImg.class); List newShopProductImgs = new ArrayList(); for(int a=0;a shopProductParamRefs = JSON.parseArray(shopProduct.getParamRefs(), ShopProductParamRef.class); List newShopProductParamRefs = new ArrayList(); for(int b=0;b 0) { if(StringUtils.isNotBlank(newShopProduct.getAttrs())){ String[] attrs = newShopProduct.getAttrs().split(","); shopProductAttrRefDao.deleteById(newShopProduct.getId()); List 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 shopSkus = JSON.parseArray(newShopProduct.getShopSku(), ShopSku.class); List skuList = shopSkuDao.selectByPid(newShopProduct.getId()); List ids = new ArrayList<>(); if(skuList != null && skuList.size()>0){ //查找被删除的数据 List list = getList(shopSkus, skuList); for(int c=0;c shopProductImgs = JSON.parseArray(newShopProduct.getShopProductImg(), ShopProductImg.class); List newShopProductImgs = new ArrayList<>(); for(int a=0;a shopProductParamRefs = JSON.parseArray(newShopProduct.getParamRefs(), ShopProductParamRef.class); List newShopProductParamRefs = new ArrayList(); for(int b=0;b paramList = shopProductParamDao.selectByModel(shopProductParam); if(CollectionUtils.isNotEmpty(paramList)){ for (ShopProductParam bean :paramList) { List 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 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 shopSkus = shopSkuDao.selectByPid(id); List shopProductImg = shopProductImgDao.selectByPid(id); List attrRefs = shopProductAttrRefDao.selectByPid(id); List 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 getList(List list1,List list2) { List 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 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 shopIds = StringUtils.strToCollToString(ids, ","); ShopProduct shopProduct = new ShopProduct(); shopProduct.setStatus(status); shopProductDao.updateByIds(shopIds, shopProduct); return new AjaxResult(AjaxResult.STATUS_SUCCESS,"修改成功"); } }