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
package com.sinata.modular.mall.controller;
 
import cn.hutool.core.util.StrUtil;
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.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.modular.mall.model.*;
import com.sinata.modular.mall.service.*;
import org.apache.commons.collections.CollectionUtils;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.*;
 
/**
 * 规格属性控制器
 *
 * @author goku
 */
@Controller
@RequestMapping("/mallGoodsSpec")
public class MallGoodsSpecController extends BaseController {
 
    private String PREFIX = "/mall/mallGoodsSpec/";
 
    @Autowired
    private IMallGoodsSpecService mallGoodsSpecService;
 
    @Autowired
    private IMallClassifyOneService iMallClassifyOneService;
 
    @Autowired
    private IMallClassifyTwoService iMallClassifyTwoService;
 
    @Autowired
    private IMallGoodsSpecValueService mallGoodsSpecValueService;
 
    @Autowired
    private IMallGoodsSkuService mallGoodsSkuService;
 
    /**
     * 跳转到规格属性首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mallGoodsSpec.html";
    }
 
    /**
     * 跳转到添加规格属性
     */
    @RequestMapping("/mallGoodsSpec_add")
    public String mallGoodsSpecAdd(Model model) {
        List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
        List<String> oneIds = Lists.newArrayList();
        for (MallClassifyOne classifyOne : mallClassifyOneList) {
            oneIds.add("" + classifyOne.getId());
        }
 
        model.addAttribute("mallClassifyOneList", mallClassifyOneList);
        model.addAttribute("oneIds", String.join(",", oneIds));
        return PREFIX + "mallGoodsSpec_add.html";
    }
 
    /**
     * 跳转到修改规格属性
     */
    @RequestMapping("/mallGoodsSpec_update/{mallGoodsSpecId}")
    public String mallGoodsSpecUpdate(@PathVariable Integer mallGoodsSpecId, Model model) {
        MallGoodsSpec mallGoodsSpec = mallGoodsSpecService.selectById(mallGoodsSpecId);
        model.addAttribute("item", mallGoodsSpec);
        List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
        model.addAttribute("mallClassifyOneList", mallClassifyOneList);
        model.addAttribute("mallSpecValues", this.mallGoodsSpecValueService
                .selectList(new EntityWrapper<MallGoodsSpecValue>().eq("spec_id", mallGoodsSpecId)
                        .eq("is_delete", "0")));
        LogObjectHolder.me().set(mallGoodsSpec);
        return PREFIX + "mallGoodsSpec_edit.html";
    }
 
    /**
     * 获取规格属性列表
     *
     * @param specName 规格属性名称
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(String beginTime, String endTime, String specName) {
        Page<Map<String, Object>> page = new PageFactory().defaultPage();
        Wrapper wrapper = new EntityWrapper<MallGoodsSpec>()
                .ne("is_delete", 1).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(specName)) {
            wrapper.like("spec_name", specName);
        }
        Map<String, String> classMap = getClassIdAndName();
        // 查询数据列表
        List<Map<String, Object>> list = mallGoodsSpecService.selectMapsPage(page, wrapper).getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, Object> map : list) {
                StringBuffer classNames = new StringBuffer("");
                String oneIds = (String) map.get("classifyIdOne");
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(oneIds)) {
                    String[] ids = oneIds.split(",");
                    for (String id : ids) {
                        String className = classMap.get(id);
                        if (org.apache.commons.lang3.StringUtils.isNotEmpty(className)) {
                            classNames.append(className + ",");
                        }
                    }
                }
 
                map.put("classNames", classNames.toString());
            }
        }
 
        page.setRecords(list);
        return super.packForBT(page);
    }
 
    /**
     * @Title: getClassIdAndName
     * @Description: 获取类型对应类型和名字map
     * @author guohongjin
     * @date 2023/3/20
     */
    @RequestMapping("/getClassIdAndName")
    @ResponseBody
    public Map<String, String> getClassIdAndName() {
        Map<String, String> classMap = new HashMap<String, String>();
        List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
        if (CollectionUtils.isNotEmpty(mallClassifyOneList)) {
            for (MallClassifyOne mallClassifyOne : mallClassifyOneList) {
                classMap.put(mallClassifyOne.getId() + "", mallClassifyOne.getClassifyName());
            }
        }
        return classMap;
    }
 
    /**
     * 新增规格属性
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "新增规格属性")
    @RequestMapping(value = "/add")
    public Object add(MallGoodsSpec mallGoodsSpec) {
        mallGoodsSpecService.insert(mallGoodsSpec);
//        if (org.apache.commons.lang3.StringUtils.isNotEmpty(mallGoodsSpec.getSpecValuesJson())){
//            List<MallGoodsSpecValue> mallGoodsSpecValueList = JSONObject.parseArray(mallGoodsSpec.getSpecValuesJson(),MallGoodsSpecValue.class);
//            mallGoodsSpecValueList.forEach(mallGoodsSpecValue -> {
//                mallGoodsSpecValue.setSpecId(mallGoodsSpec.getId());
//            });
//            this.mallGoodsSpecValueService.insertBatch(mallGoodsSpecValueList);
//        }
        return SUCCESS_TIP;
    }
 
    /**
     * 删除/批量删除
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "删除/批量删除规格属性")
    @RequestMapping(value = "/delete")
    public Object delete(@RequestParam String ids) {
        String[] idd = ids.split(",");
        if (idd.length == 1) {
            int count1 = this.mallGoodsSkuService.selectCount(new EntityWrapper<MallGoodsSku>().where("FIND_IN_SET({0},spec_ids)", idd[0]));
            if (count1 == 0) {
                // 逻辑删除
                mallGoodsSpecService.updateForSet("is_delete = 1", new EntityWrapper<MallGoodsSpec>().in("id", ids.split(",")));
                return SUCCESS_TIP;
            } else {
                ErrorTip errorTip = new ErrorTip(500, "该规格已被使用,不能删除");
                return errorTip;
            }
        } else {
            boolean state = false;
            for (String id : idd) {
                int count1 = this.mallGoodsSkuService.selectCount(new EntityWrapper<MallGoodsSku>().where("FIND_IN_SET({0},spec_ids)", id));
                if (count1 == 0) {
                    // 逻辑删除
                    mallGoodsSpecService.updateForSet("is_delete = 1", new EntityWrapper<MallGoodsSpec>().in("id", ids.split(",")));
                } else {
                    state = true;
                }
            }
            if (state) {
                ErrorTip errorTip = new ErrorTip(500, "已经被使用的分类删除失败。请解除关联关系后再删除");
                return errorTip;
            } else {
                return SUCCESS_TIP;
            }
        }
    }
 
    /**
     * 修改规格属性
     *
     * @param specValueIds 规格值ids  需要删除的没有删除的为null
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "修改规格属性")
    @RequestMapping(value = "/update")
    public Object update(MallGoodsSpec mallGoodsSpec, String specValueIds, String deleteSpecValueIds) {
        mallGoodsSpecService.updateById(mallGoodsSpec);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改规格属性状态
     */
    @ResponseBody
    @BussinessLog(value = "修改规格属性状态")
    @RequestMapping(value = "/updateState")
    public Object updateState(Integer mallGoodsSpecId, Integer state) {
        mallGoodsSpecService.updateForSet("is_lock = " + state, new EntityWrapper<MallGoodsSpec>().eq("id", mallGoodsSpecId));
        return SUCCESS_TIP;
    }
 
    /**
     * 跳转规格属性详情
     */
    @RequestMapping(value = "/detail/{mallGoodsSpecId}")
    public Object detail(@PathVariable("mallGoodsSpecId") Integer mallGoodsSpecId, Model model) {
        MallGoodsSpec mallGoodsSpec = mallGoodsSpecService.selectById(mallGoodsSpecId);
        model.addAttribute("item", mallGoodsSpec);
        return PREFIX + "mallGoodsSpec_detail.html";
    }
}