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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.sinata.modular.member.controller;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.sinata.common.enums.EnumIsDelete;
import com.sinata.core.base.controller.BaseController;
import com.sinata.core.base.tips.ErrorTip;
import com.sinata.core.common.annotion.BussinessLog;
import com.sinata.core.common.annotion.Permission;
import com.sinata.core.common.constant.factory.PageFactory;
import com.sinata.core.log.LogObjectHolder;
import com.sinata.core.shiro.ShiroKit;
import com.sinata.core.util.DateUtils2;
import com.sinata.core.util.ExcelExportUtil;
import com.sinata.core.util.ToolUtil;
import com.sinata.modular.mall.model.MallGoodsSku;
import com.sinata.modular.mall.service.IMallGoodsSkuService;
import com.sinata.modular.member.model.MemMerchant;
import com.sinata.modular.member.model.MerchantSysUser;
import com.sinata.modular.member.service.IMemMerchantService;
import com.sinata.modular.member.service.IMemUserService;
import com.sinata.modular.member.service.IMerchantSysUserService;
import com.sinata.modular.system.model.AreaCity;
import com.sinata.modular.system.model.StoreService;
import com.sinata.modular.system.service.IAreaCityService;
import com.sinata.modular.system.service.IStoreServiceService;
import java.util.HashMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 商家信息控制器
 *
 * @author goku
 */
@Controller
@RequestMapping("/memMerchant")
public class MemMerchantController extends BaseController {
 
    private String PREFIX = "/member/memMerchant/";
 
    @Autowired
    private IMemUserService memUserService;
 
    @Autowired
    private IMemMerchantService memMerchantService;
    @Resource
    private IStoreServiceService storeServiceService;
    @Resource
    private IAreaCityService areaCityService;
 
    @Resource
    private IMallGoodsSkuService mallGoodsSkuService;
 
    @Resource
    private IMerchantSysUserService sysUserService;
 
 
    /**
     * 跳转到商家信息首页
     */
    @RequestMapping("")
    public String index(Model model) {
        Wrapper wrapper = new EntityWrapper<AreaCity>().orderBy("id", false);
        wrapper.eq("is_open", 1);
        wrapper.eq("is_delete", EnumIsDelete.EXISTED.index);
        //model.addAttribute("cityAreaList", areaCityService.selectList(wrapper));
        wrapper.groupBy("province_code");
        model.addAttribute("provinceList", areaCityService.selectList(wrapper));
        return PREFIX + "memMerchant.html";
    }
 
    /**
     * 跳转到商家审核信息首页
     */
    @RequestMapping("/auditeIndex")
    public String auditeIndex() {
        return PREFIX + "memMerchant-audite.html";
    }
 
    /**
     * 跳转到添加商家信息
     */
    @RequestMapping("/memMerchant_add")
    public String memMerchantAdd(Model model) {
        Wrapper wrapper = new EntityWrapper<StoreService>().orderBy("id", false);
        wrapper.eq("status", 1);
        wrapper.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("serviceList", storeServiceService.selectList(wrapper));
        Wrapper wrapper2 = new EntityWrapper<AreaCity>().orderBy("id", false);
        wrapper2.eq("is_open", 1);
        wrapper2.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("cityAreaList", areaCityService.selectList(wrapper2));
        return PREFIX + "memMerchant_add.html";
    }
 
    /**
     * 跳转到修改商家信息
     */
    @RequestMapping("/memMerchant_update/{memMerchantId}")
    public String memMerchantUpdate(@PathVariable Integer memMerchantId, Model model) {
        Wrapper wrapper = new EntityWrapper<StoreService>().orderBy("id", false);
        wrapper.eq("status", 1);
        wrapper.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("serviceList", storeServiceService.selectList(wrapper));
        MemMerchant memMerchant = memMerchantService.selectById(memMerchantId);
        model.addAttribute("item", memMerchant);
        Wrapper wrapper2 = new EntityWrapper<AreaCity>().orderBy("id", false);
        wrapper2.eq("is_open", 1);
        wrapper2.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("cityAreaList", areaCityService.selectList(wrapper2));
        List<HashMap<String, Object>> list = mallGoodsSkuService.queryMerchantGoodsList(
                memMerchant.getId());
        model.addAttribute("merchantGoodsList", list);
        LogObjectHolder.me().set(memMerchant);
        return PREFIX + "memMerchant_edit.html";
    }
 
    /**
     * 跳转商家信息详情
     */
    @RequestMapping(value = "/detail/{memMerchantId}")
    public Object detail(@PathVariable("memMerchantId") Integer memMerchantId, Model model) {
        Wrapper wrapper = new EntityWrapper<StoreService>().orderBy("id", false);
        wrapper.eq("status", 1);
        wrapper.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("serviceList", storeServiceService.selectList(wrapper));
        MemMerchant memMerchant = memMerchantService.selectById(memMerchantId);
        model.addAttribute("item", memMerchant);
        Wrapper wrapper2 = new EntityWrapper<AreaCity>().orderBy("id", false);
        wrapper2.eq("is_open", 1);
        wrapper2.eq("is_delete", EnumIsDelete.EXISTED.index);
        model.addAttribute("cityAreaList", areaCityService.selectList(wrapper2));
        model.addAttribute("merchantGoodsList", mallGoodsSkuService.queryMerchantGoodsList(memMerchant.getId()));
        LogObjectHolder.me().set(memMerchant);
        return PREFIX + "memMerchant_detail.html";
    }
 
    /**
     * 跳转到修改商家信息
     */
    @RequestMapping("/memMerchantAudite/{memMerchantId}")
    public String memMerchantAudite(@PathVariable Integer memMerchantId, Model model) {
        MemMerchant memMerchant = memMerchantService.selectById(memMerchantId);
        model.addAttribute("item", memMerchant);
        LogObjectHolder.me().set(memMerchant);
        return PREFIX + "memMerchant_edit.html";
    }
 
    /**
     * 新增商家信息
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "新增商家信息")
    @RequestMapping(value = "/add")
    @Transactional(rollbackFor = {Exception.class})
    public Object add(MemMerchant memMerchant) {
        try {
            verifMerchantPhone(memMerchant.getImAccount(),null);
        }catch (Exception e){
            return new ErrorTip(400,e.getMessage());
        }
        memMerchantService.insert(memMerchant);
        addMerchantGoods(memMerchant.getProductId(), memMerchant.getId());
        MerchantSysUser sysUser = new MerchantSysUser();
        sysUser.setId(memMerchant.getId());
        sysUser.setAccount(memMerchant.getImAccount());
        sysUser.setPhone(memMerchant.getImAccount());
        sysUser.setSalt(ShiroKit.getRandomSalt(5));
        sysUser.setPassword(ShiroKit.md5(memMerchant.getImPassword(), sysUser.getSalt()));
        sysUser.setName(memMerchant.getMerchantName());
        sysUserService.insertOrUpdate(sysUser);
        return SUCCESS_TIP;
    }
    private void verifMerchantPhone(String phone,Integer userId){
        Wrapper<MemMerchant> wrapper = new EntityWrapper<>();
        wrapper.eq("im_account",phone);
        wrapper.eq("is_delete",0);
        if(Objects.nonNull(userId)){
            wrapper.ne("id",userId);
        }
        List<MemMerchant> merchantList = memMerchantService.selectList(wrapper);
        if(merchantList.size() > 0){
            throw new IllegalArgumentException("该账号已经绑定商户");
        }
    }
 
    private void addMerchantGoods(String productId, Integer merchantId) {
        if (StrUtil.isNotEmpty(productId)) {
            JSONArray jsonArray = JSONArray.parseArray(productId);
            List<MallGoodsSku> goodsList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Wrapper wrapper = new EntityWrapper<MallGoodsSku>();
                wrapper.eq("goods_id", jsonObject.getInteger("goodsId"));
                if (Objects.nonNull(jsonObject.get("goodsSkuId"))) {
                    MallGoodsSku sku = new MallGoodsSku();
                    sku.setPriceMerchant(jsonObject.getBigDecimal("price"));
                    sku.setState(jsonObject.getInteger("state"));
                    wrapper.eq("merchant_id", merchantId)
                            .eq("id", jsonObject.getInteger("goodsSkuId"));
                    mallGoodsSkuService.update(sku, wrapper);
                } else {
                    //查询门店sku,防止重复添加
                    wrapper.eq("merchant_id", merchantId).
                    eq("spec_ids", jsonObject.getInteger("specIds"));
                    MallGoodsSku specSku = mallGoodsSkuService.selectOne(wrapper);
                    if (Objects.nonNull(specSku)) {
                        specSku.setPriceMerchant(jsonObject.getBigDecimal("price"));
                        specSku.setState(jsonObject.getInteger("state"));
                        mallGoodsSkuService.updateById(specSku);
                    } else {
                        wrapper = new EntityWrapper<MallGoodsSku>();
                        wrapper.eq("spec_ids", jsonObject.getInteger("specIds"))
                                .eq("merchant_id", 0)
                                .eq("goods_id", jsonObject.getInteger("goodsId"));
                        MallGoodsSku sku = mallGoodsSkuService.selectOne(wrapper);
                        MallGoodsSku goodsSku = new MallGoodsSku();
                        BeanUtils.copyProperties(sku, goodsSku);
                        goodsSku.setPriceMerchant(jsonObject.getBigDecimal("price"));
                        goodsSku.setPrice(sku.getPrice());
                        goodsSku.setPriceMember(sku.getPriceMember());
                        goodsSku.setPriceSale(sku.getPriceSale());
                        goodsSku.setId(null);
                        goodsSku.setState(jsonObject.getInteger("state"));
                        goodsSku.setSkuId(sku.getId());
                        goodsSku.setMerchantId(merchantId);
                        goodsList.addAll(Lists.newArrayList(goodsSku));
                    }
                }
            }
            if (goodsList.size() > 0) {
                mallGoodsSkuService.insertBatch(goodsList);
            }
        }
    }
 
    /**
     * 删除/批量删除
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "删除/批量删除商家信息")
    @RequestMapping(value = "/delete")
    public Object delete(@RequestParam String ids) {
        // 逻辑删除
        memMerchantService.updateForSet("is_delete = 1", new EntityWrapper<MemMerchant>().in("id", ids.split(",")));
        sysUserService.updateForSet("status = 3", new EntityWrapper<MerchantSysUser>().in("id", ids.split(",")));
        return SUCCESS_TIP;
    }
 
    /**
     * 修改商家信息
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "修改商家信息")
    @RequestMapping(value = "/update")
    @Transactional(rollbackFor = {Exception.class})
    public Object update(MemMerchant memMerchant) {
        try {
            verifMerchantPhone(memMerchant.getImAccount(),memMerchant.getId());
        }catch (Exception e){
            return new ErrorTip(400,e.getMessage());
        }
        memMerchantService.updateById(memMerchant);
        addMerchantGoods(memMerchant.getProductId(), memMerchant.getId());
        MerchantSysUser sysUser = new MerchantSysUser();
        sysUser.setId(memMerchant.getId());
        sysUser.setAccount(memMerchant.getImAccount());
        sysUser.setPhone(memMerchant.getImAccount());
        sysUser.setSalt(ShiroKit.getRandomSalt(5));
        sysUser.setPassword(ShiroKit.md5(memMerchant.getImPassword(), sysUser.getSalt()));
        sysUser.setName(memMerchant.getMerchantName());
        sysUserService.insertOrUpdate(sysUser);
        return SUCCESS_TIP;
    }
 
 
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(String beginTime, String endTime, String merchantName, Integer cityCode1, Integer cityCode2, Integer cityCode3) {
        Page<MemMerchant> page = new PageFactory().defaultPage();
        // 时间搜索
        if (ToolUtil.isNotEmpty(beginTime)) {
            beginTime = beginTime + " 00:00:00";
        }
        if (ToolUtil.isNotEmpty(endTime)) {
            endTime = endTime + " 23:59:59";
        }
 
        // 查询数据列表
        List<MemMerchant> list = memMerchantService.queryMerchantList(page, beginTime, endTime, merchantName, cityCode1, cityCode2, cityCode3);
 
        page.setRecords(list);
        return super.packForBT(page);
    }
 
    /**
     * 导出用户信息列表
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(String beginTime, String endTime, String merchantName, Integer cityCode1, Integer cityCode2, Integer cityCode3, HttpServletResponse response) {
        Page<MemMerchant> page = new PageFactory().defaultPage();
 
        // 时间搜索
        if (StrUtil.isNotBlank(beginTime)) {
            beginTime = beginTime + " 00:00:00";
        }
        if (StrUtil.isNotBlank(endTime)) {
            endTime = endTime + " 23:59:59";
        }
 
        // 查询数据列表
        List<MemMerchant> list = memMerchantService.queryMerchantList(page, beginTime, endTime, merchantName, cityCode1, cityCode2, cityCode3);
 
        // 表格数据【封装】
        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("门店服务");
        dataList.add(shellList);
 
        // 详细数据列【封装】
        for (MemMerchant map : list) {
            shellList = new ArrayList<>();
            shellList.add(DateUtils2.getTime(map.getCreateTime()));
            shellList.add(map.getStartTime());
            shellList.add(map.getMerchantName());
            shellList.add(map.getCityName());
            shellList.add(map.getAddress());
            shellList.add(map.getLinkman());
            shellList.add(map.getLinkPhone());
            shellList.add(getDate(map.getWorkStartDate()) + "至" + getDate(map.getWorkEndDate()) + " "
                    + map.getWorkStartTime() + "-" + map.getWorkEndTime());
            shellList.add(map.getNumber());
            shellList.add(map.getServiceName());
            dataList.add(shellList);
        }
        try {
            // 调用工具类进行导出
            ExcelExportUtil.easySheet("导出数据" + DateUtils2.formatDate(new Date(), "YYYYMMddHHmmSS"), "导出数据", dataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private String getDate(String key) {
        String value = "周一";
        switch (key) {
            case "2":
                value = "周二";
                break;
            case "3":
                value = "周三";
                break;
            case "4":
                value = "周四";
                break;
            case "5":
                value = "周五";
                break;
            case "6":
                value = "周六";
                break;
            case "7":
                value = "周日";
                break;
            default:
                break;
        }
        return value;
    }
 
 
}