mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
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
package com.sinata.shop.modular.mall.controller;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sinata.common.enums.mall.EnumMallGoodsGroupType;
import com.sinata.common.enums.mall.EnumMallGoodsState;
import com.sinata.core.base.controller.BaseController;
import com.sinata.core.util.Convert;
import com.sinata.core.util.DateUtils2;
import com.sinata.core.util.ExcelExportUtil;
import com.sinata.shop.core.common.constant.factory.PageFactory;
import com.sinata.shop.core.log.LogObjectHolder;
import com.sinata.shop.core.shiro.ShiroKit;
import com.sinata.shop.modular.mall.model.*;
import com.sinata.shop.modular.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 商品信息控制器
 *
 * @author goku
 */
@Controller
@RequestMapping("/mallGoods")
public class MallGoodsController extends BaseController {
 
    private String PREFIX = "/mall/mallGoods/";
 
    @Autowired
    private IMallGoodsService mallGoodsService;
 
    @Autowired
    private IMallGoodsSkuService mallGoodsSkuService;
 
    @Autowired
    private IMallClassifyOneService mallClassifyOneService;
 
    @Autowired
    private IMallGoodsSpecService mallGoodsSpecService;
 
    @Autowired
    private IMallGoodsDetailService mallGoodsDetailService;
 
    @Resource
    private IMallTagService mallTagService;
 
 
    /**
     * 跳转到商品信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mallGoods.html";
    }
 
    /**
     * 跳转到添加商品信息
     */
    @RequestMapping("/mallGoods_add")
    public String mallGoodsAdd(Model model) {
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
        model.addAttribute("mallTags", mallTags);
        return PREFIX + "mallGoods_add.html";
    }
 
    /**
     * 跳转到修改商品信息
     */
    @RequestMapping("/mallGoods_update/{mallGoodsId}")
    public String mallGoodsUpdate(@PathVariable Integer mallGoodsId, Model model) {
        MallGoods mallGoods = mallGoodsService.selectById(mallGoodsId);
        model.addAttribute("item", mallGoods);
 
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
        model.addAttribute("mallTags", mallTags);
 
        LogObjectHolder.me().set(mallGoods);
        return PREFIX + "mallGoods_edit.html";
    }
 
    /**
     * 获取商品信息列表
     *
     * @param id          商品id
     * @param state       商品状态
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(String beginTime, String endTime, String goodsName, String id, Integer state, String goodsNo) {
        Page<Map<String, Object>> page = new PageFactory().defaultPage();
 
        page.setRecords(
                wrapperList(page, beginTime, endTime, goodsName, id, state, goodsNo)
        );
 
        return super.packForBT(page);
    }
    public List<Map<String, Object>> wrapperList(Page<Map<String, Object>> page, String beginTime, String endTime, String goodsName, String id, Integer state, String goodsNo) {
        Integer merchantId = ShiroKit.getUserId();
//        merchantId = null;
        List<MallGoodsSku> goodsByMerchantList = mallGoodsSkuService.selectList(
                new EntityWrapper<MallGoodsSku>()
                        .setSqlSelect("goods_id")
                        .eq(merchantId != null, "merchant_id", merchantId)
                        .groupBy("goods_id")
        );
        List<Integer> goodsIds = Arrays.asList(0);
        if (goodsByMerchantList != null && goodsByMerchantList.size() > 0) {
            goodsIds = goodsByMerchantList.stream().map(MallGoodsSku::getGoodsId).collect(Collectors.toList());
        }
 
        Wrapper wrapper = new EntityWrapper<MallGoods>()
                .in("id", goodsIds)
                .eq("group_type", EnumMallGoodsGroupType.GOODS.index)
                .eq("is_delete", 0)
                .orderBy("id", false);
 
        // 时间搜索
        if (!StringUtils.isEmpty(beginTime)) {
            wrapper.ge("create_time", beginTime + " 00:00:00");
        }
        if (!StringUtils.isEmpty(endTime)) {
            wrapper.le("create_time", endTime + " 23:59:59");
        }
 
        if (!StringUtils.isEmpty(goodsName)) {
            wrapper.like("goods_name", goodsName);
        }
        if (!StringUtils.isEmpty(id)) {
            if (id.indexOf("sp") != -1) {
                String idd = id.substring(id.indexOf("sp") + 2);
                wrapper.eq("id", Integer.parseInt(idd));
            } else {
                wrapper.eq("id", Integer.parseInt(id));
            }
        }
        if (state != null) {
            wrapper.eq("state", state);
        }
        if (StrUtil.isNotBlank(goodsNo)) {
            wrapper.like("goods_no", goodsNo);
        }
 
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
 
        // 查询数据列表
        List<Map<String, Object>> list;
        if (page != null) {
            list = mallGoodsService.selectMapsPage(page, wrapper).getRecords();
        } else {
            list = mallGoodsService.selectMaps(wrapper);
        }
 
        // 规格列表
        List<MallGoodsSpec> selectList = mallGoodsSpecService.selectList(new EntityWrapper<MallGoodsSpec>().setSqlSelect("id, spec_name"));
 
        return list.stream().peek(m -> {
            Object classObj = m.get("classifyIdOne");
            if (classObj != null) {
                m.put("classifyName", mallClassifyOneService.selectById(Convert.toInt(classObj)).getClassifyName());
            }
 
            // 封装规格名
            List<String> specNameList = new ArrayList<>();
            List<MallGoodsSku> skuList = mallGoodsSkuService.selectList(new EntityWrapper<MallGoodsSku>().eq("goods_id", m.get("id")).eq("merchant_id", 0));
            for (MallGoodsSku sku : skuList) {
                String[] spIds = sku.getSpecGrep().split(",");
                for (String spId : spIds) {
                    for (MallGoodsSpec spec : selectList) {
                        if (spec.getId().toString().equals(spId)) {
                            specNameList.add(spec.getSpecName());
                        }
                    }
                }
            }
            m.put("specNames", specNameList.stream().distinct().collect(Collectors.joining(",")));
 
            m.put("merchantCount", mallGoodsSkuService.selectCount(
                    new EntityWrapper<MallGoodsSku>()
                            .eq("goods_id", m.get("id"))
                            .ne("merchant_id", 0)
                            .groupBy("goods_id")
            ));
 
            Object tagIds = m.get("tagIds");
            if (tagIds != null) {
                m.put("tagIds", mallTagService.getTagListByGoodsId(mallTags, tagIds.toString()).stream().collect(Collectors.joining("、")));
            }
        }).collect(Collectors.toList());
    }
 
    @ResponseBody
    @RequestMapping(value = "/export")
    public void export(String beginTime, String endTime, String goodsName, String id, Integer state, String goodsNo, HttpServletResponse response) {
        List<Map<String, Object>> list = wrapperList(null, beginTime, endTime, goodsName, id, state, goodsNo);
 
        // 表格数据【封装】
        List<List<Object>> dataList = new ArrayList<>();
 
        // 头部列【封装】
        List<Object> shellList = new ArrayList<>();
        shellList.add("创建时间");
        shellList.add("商品编号");
        shellList.add("商品分类");
        shellList.add("商品名称");
        shellList.add("商品副标题");
        shellList.add("商品标签");
        shellList.add("规格");
        shellList.add("销售量");
        shellList.add("商品库存");
        shellList.add("售卖门店数");
        shellList.add("销售价");
        shellList.add("会员价");
        shellList.add("商品状态");
        dataList.add(shellList);
 
        // 详细数据列【封装】
        for (Map<String, Object> map : list) {
            shellList = new ArrayList<>();
            shellList.add(map.get("createTime"));
            shellList.add(map.get("goodsNo"));
            shellList.add(map.get("classifyName"));
            shellList.add(map.get("goodsName"));
            shellList.add(map.get("goodsTitle"));
            shellList.add(map.get("tagIds"));
            shellList.add(map.get("specNames"));
            shellList.add(map.get("buyCount"));
            shellList.add(map.get("stock"));
            shellList.add(map.get("merchantCount"));
            shellList.add(map.get("priceSale"));
            shellList.add(map.get("priceMember"));
            shellList.add(EnumMallGoodsState.getMarkByIndex((Integer) map.get("state")));
            dataList.add(shellList);
        }
        try {
            String title = "商品信息";
            // 调用工具类进行导出
            ExcelExportUtil.easySheet(title + DateUtils2.formatDate(new Date(), "YYYYMMddHHmmSS"), title, dataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 跳转商品信息详情
     */
    @RequestMapping(value = "/detail/{mallGoodsId}")
    public Object detail(@PathVariable("mallGoodsId") Integer mallGoodsId, Model model) {
        MallGoods mallGoods = mallGoodsService.selectById(mallGoodsId);
        mallGoods.setMallClassifyOne(this.mallClassifyOneService.selectById(mallGoods.getClassifyIdOne()));
 
        MallGoodsDetail goodsDetail = this.mallGoodsDetailService.selectById(mallGoods.getId());
        if (goodsDetail == null) {
            goodsDetail = new MallGoodsDetail();
        }
 
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
        mallGoods.setTagIds(mallTagService.getTagListByGoodsId(mallTags, mallGoods.getTagIds()).stream().collect(Collectors.joining("、")));
 
        model.addAttribute("item", mallGoods);
        model.addAttribute("mallGoodsState", EnumMallGoodsState.getMarkByIndex(mallGoods.getState()));
        model.addAttribute("mallGoodsDetail", goodsDetail);
        model.addAttribute("introImageList", goodsDetail.getIntroImage().split(","));
 
        return PREFIX + "mallGoods_detail.html";
    }
 
    @ResponseBody
    @GetMapping(value = "/getGoodsSpecData")
    public List<MallGoodsSku> getGoodsSpecData(Integer goodsId) {
        List<MallGoodsSku> skuList = mallGoodsSkuService.selectList(
                new EntityWrapper<MallGoodsSku>()
                        .setSqlSelect("spec_ids id, grep_name, stock")
                        .eq("goods_id", goodsId)
                        .eq("merchant_id", 0)
        );
 
        MallGoods mallGoods = mallGoodsService.selectById(goodsId);
 
        String classifyName = "";
        MallClassifyOne classifyOne = mallClassifyOneService.selectById(mallGoods.getClassifyIdOne());
        if (classifyOne != null) {
            classifyName = classifyOne.getClassifyName();
        }
        for (MallGoodsSku sku : skuList) {
            sku.setClassifyName(classifyName);
        }
        return skuList;
    }
}