| package com.matrix.system.shopXcx.action; | 
|   | 
| import com.alibaba.fastjson.JSONObject; | 
| 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.LogUtil; | 
| import com.matrix.core.tools.StringUtils; | 
| import com.matrix.core.tools.WebUtil; | 
| import com.matrix.system.common.bean.SysUsers; | 
| import com.matrix.system.common.tools.ServiceUtil; | 
| import com.matrix.system.hive.action.util.QueryUtil; | 
| import com.matrix.system.hive.plugin.util.CollectionUtils; | 
| import com.matrix.system.shopXcx.bean.ShopAdvertis; | 
| import com.matrix.system.shopXcx.bean.ShopAdvertisType; | 
| import com.matrix.system.shopXcx.dao.ShopAdvertisDao; | 
| import com.matrix.system.shopXcx.dao.ShopAdvertisTypeDao; | 
| import com.matrix.system.shopXcx.shopEnum.ShopAdNodeType; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Controller; | 
| import org.springframework.web.bind.annotation.RequestBody; | 
| import org.springframework.web.bind.annotation.RequestMapping; | 
| import org.springframework.web.bind.annotation.ResponseBody; | 
| import org.springframework.web.servlet.ModelAndView; | 
|   | 
| import java.util.Arrays; | 
| import java.util.List; | 
| import java.util.stream.Collectors; | 
|   | 
| /** | 
|  * @author jiangyouyao | 
|  * @description 广告分类表 | 
|  * @date 2019-06-05 11:02 | 
|  */ | 
| @Controller | 
| @RequestMapping(value = "admin/shopAdvertisType") | 
| public class ShopAdvertisTypeAction { | 
|   | 
|     @Autowired | 
|     private ShopAdvertisTypeDao shopAdvertisTypeDao; | 
|     @Autowired | 
|     private ServiceUtil serviceUtil; | 
|   | 
|     //记录编辑前的值Before_Edit_Value | 
|     public static final String BEV = "ShopAdvertisType_BEV"; | 
|     @Autowired | 
|     private ShopAdvertisDao shopAdvertisDao; | 
|   | 
|     /** | 
|      * 列表显示 | 
|      */ | 
|     @RequestMapping(value = "/showList") | 
|     public @ResponseBody | 
|     AjaxResult showList(ShopAdvertisType shopAdvertisType, PaginationVO pageVo) { | 
|         QueryUtil.setQueryLimitCom(shopAdvertisType); | 
|         List<ShopAdvertisType> dataList = shopAdvertisTypeDao.selectInPage(shopAdvertisType, pageVo); | 
|         AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList, | 
|                 shopAdvertisTypeDao.selectTotalRecord(shopAdvertisType)); | 
|         return result; | 
|     } | 
|   | 
|     /** | 
|      * 新增 | 
|      */ | 
|     @RemoveRequestToken | 
|     @RequestMapping(value = "/addShopAdvertisType") | 
|     public @ResponseBody | 
|     AjaxResult addShopAdvertisType(ShopAdvertisType shopAdvertisType) { | 
|         SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); | 
|         shopAdvertisType.setCreateBy(user.getSuName()); | 
|         shopAdvertisType.setUpdateBy(user.getSuName()); | 
|         shopAdvertisType.setShopId(user.getShopId()); | 
|         shopAdvertisType.setCompanyId(user.getCompanyId()); | 
|         //检查广告类别编号是否重复 | 
|         boolean b = serviceUtil.addCheckRepeat("shop_advertis_type", "adt_code", shopAdvertisType.getAdtCode()); | 
|         if (b) { | 
|             return new AjaxResult(AjaxResult.STATUS_FAIL, "广告类别编号已经存在"); | 
|         } | 
|         //检查广告类别名称是否重复 | 
|         boolean c = serviceUtil.addCheckRepeat("shop_advertis_type", "adt_name", shopAdvertisType.getAdtName()); | 
|         if (c) { | 
|             return new AjaxResult(AjaxResult.STATUS_FAIL, "广告类别名称已经存在"); | 
|         } | 
|         int i = shopAdvertisTypeDao.insert(shopAdvertisType); | 
|         if (i > 0) { | 
|             return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "广告分类表"); | 
|         } else { | 
|             throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL); | 
|         } | 
|     } | 
|   | 
|   | 
|     /** | 
|      * TODO 完善本方法 | 
|      */ | 
|     @RemoveRequestToken | 
|     @RequestMapping(value = "/modifyShopAdvertisType") | 
|     public @ResponseBody | 
|     AjaxResult modifyShopAdvertisType(@RequestBody ShopAdvertisType newShopAdvertisType) { | 
|         SysUsers user = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY); | 
|         if (StringUtils.isNotBlank(newShopAdvertisType.getNodeStyle())) { | 
|             String style = newShopAdvertisType.getNodeStyle(); | 
|             style = style.replaceAll("px", "rpx") | 
|                     .replaceAll("\"", "") | 
|                     .replaceAll("\\{", "") | 
|                     .replaceAll("}", "") | 
|                     .replaceAll(",", ";"); | 
|             newShopAdvertisType.setNodeStyle(style); | 
|         } | 
|         if (newShopAdvertisType.getAdtId() == null) { | 
|   | 
|             newShopAdvertisType.setCreateBy(user.getSuName()); | 
|             newShopAdvertisType.setUpdateBy(user.getSuName()); | 
|   | 
|             shopAdvertisTypeDao.insert(newShopAdvertisType); | 
|         } else { | 
|             shopAdvertisTypeDao.updateByModel(newShopAdvertisType); | 
|         } | 
|         if(CollectionUtils.isNotEmpty(newShopAdvertisType.getAds())){ | 
|   | 
|             if(newShopAdvertisType.getAdtId()!=null){ | 
|                 ShopAdvertis queryAd=new ShopAdvertis(); | 
|                 queryAd.setTypeId(newShopAdvertisType.getAdtId()); | 
|                 shopAdvertisDao.deleteByModel(queryAd); | 
|             } | 
|   | 
|             List<ShopAdvertis> collect = newShopAdvertisType.getAds().stream() | 
|                     .filter(ad -> ad != null && StringUtils.isNotBlank(ad.getImgUrl())) | 
|                     .collect(Collectors.toList()); | 
|             collect.forEach(ad -> { | 
|                 ad.setCreateBy(user.getSuName()); | 
|                 ad.setUpdateBy(user.getSuName()); | 
|                 ad.setTypeId(newShopAdvertisType.getAdtId()); | 
|             }); | 
|   | 
|             shopAdvertisDao.batchInsert(collect); | 
|         } | 
|   | 
|         return AjaxResult.buildSuccessInstance("修改成功"); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 进入修改界面 | 
|      */ | 
|     @SaveRequestToken | 
|     @RequestMapping(value = "/editForm") | 
|     public ModelAndView editForm(Integer id) { | 
|         ShopAdvertisType shopAdvertisType = new ShopAdvertisType(); | 
|         ModelAndView modelAndView = new ModelAndView("admin/shop/shopAdvertisType-form"); | 
|         if (id != null) { | 
|             shopAdvertisType = shopAdvertisTypeDao.selectById(id); | 
|             WebUtil.setSessionAttribute(BEV, shopAdvertisType); | 
|         } | 
|         modelAndView.addObject("obj", shopAdvertisType); | 
|         return modelAndView; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 删除 | 
|      */ | 
|     @RequestMapping(value = "/del") | 
|     public @ResponseBody | 
|     AjaxResult del(String keys) { | 
|         List<String> ids = StringUtils.strToCollToString(keys, ","); | 
|         int i = shopAdvertisTypeDao.deleteByIds(ids); | 
|         if (i > 0) { | 
|             return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i); | 
|         } else { | 
|             throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 查询所有 | 
|      */ | 
|     @RequestMapping(value = "/getAll") | 
|     public @ResponseBody | 
|     AjaxResult getAll() { | 
|         ShopAdvertisType advertisType=new ShopAdvertisType(); | 
|         QueryUtil.setQueryLimitCom(advertisType); | 
|         List<ShopAdvertisType> typeList = shopAdvertisTypeDao.selectByModel(advertisType); | 
|         return new AjaxResult(AjaxResult.STATUS_SUCCESS, typeList); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 进入修改界面 | 
|      */ | 
|     @SaveRequestToken | 
|     @RequestMapping(value = "/selectById") | 
|     public @ResponseBody | 
|     AjaxResult selectById(Integer id) { | 
|         ShopAdvertisType shopAdvertisType = shopAdvertisTypeDao.selectById(id); | 
|         if (ShopAdNodeType.NODE_TYPE_HUKUAI.equals(shopAdvertisType.getNodeType()) | 
|                 || ShopAdNodeType.NODE_TYPE_SWIPER.equals(shopAdvertisType.getNodeType())) { | 
|             //查询子元素 | 
|             //轮播图类型 | 
|             List<ShopAdvertis> advertisingList = shopAdvertisDao.selectListByType(shopAdvertisType.getAdtCode(), shopAdvertisType.getAdtCapacity()); | 
|             shopAdvertisType.setAds(advertisingList); | 
|         } | 
|         return AjaxResult.buildSuccessInstance(Arrays.asList(shopAdvertisType)); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 获取节点类型 | 
|      */ | 
|     @RequestMapping(value = "/getShopAdvertisType") | 
|     public @ResponseBody | 
|     AjaxResult getShopAdvertisType() { | 
|         return new AjaxResult(AjaxResult.STATUS_SUCCESS, ShopAdNodeType.getSelectValue()); | 
|     } | 
|   | 
|   | 
|     @RequestMapping("/getAdPage") | 
|     @ResponseBody | 
|     public AjaxResult getAdPage(@RequestBody  ShopAdvertisType shopAdvertisType) { | 
|         QueryUtil.setQueryLimitCom(shopAdvertisType); | 
|         List<ShopAdvertisType> shopAdvertisTypes = shopAdvertisTypeDao.selectByModel(shopAdvertisType); | 
|         for (ShopAdvertisType advertisType : shopAdvertisTypes) { | 
|             if (ShopAdNodeType.NODE_TYPE_SWIPER.getCode() == advertisType.getNodeType() | 
|                     || ShopAdNodeType.NODE_TYPE_HUKUAI.getCode() == advertisType.getNodeType() | 
|             ) { | 
|                 //轮播图类型 | 
|                 List<ShopAdvertis> advertisingList = shopAdvertisDao.selectListByTypeId(advertisType.getAdtId(), advertisType.getAdtCapacity()); | 
|                 advertisType.setAds(advertisingList); | 
|             } | 
|             //样式转换 | 
|             String nodeStyle = advertisType.getNodeStyle(); | 
|             if (StringUtils.isNotBlank(nodeStyle)) { | 
|                 nodeStyle = nodeStyle.replaceAll(";", ","); | 
|                 nodeStyle = nodeStyle.replaceAll("rpx", "px").trim(); | 
|                 if (nodeStyle.endsWith(",")) { | 
|                     nodeStyle = nodeStyle.substring(0, nodeStyle.length() - 1); | 
|                 } | 
|                 LogUtil.debug(nodeStyle); | 
|                 JSONObject jsonObject = new JSONObject(); | 
|                 String[] obj = nodeStyle.split(","); | 
|                 for (String node : obj) { | 
|                     String[] nodes = node.split(":"); | 
|                     String value = nodes[1]; | 
|                     jsonObject.put(nodes[0], value); | 
|                 } | 
|                 advertisType.setNodeStyle(jsonObject.toJSONString()); | 
|   | 
|             } | 
|   | 
|         } | 
|         return AjaxResult.buildSuccessInstance(shopAdvertisTypes); | 
|     } | 
|   | 
|   | 
| } |