jyy
2021-09-03 3868b706ee3ee115a400d77abc3b69b83ea448c7
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
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());
            newShopAdvertisType.setCompanyId(user.getCompanyId());
            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.setCompanyId(user.getCompanyId());
                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);
    }
 
 
}