/
SkuModule.java
265 lines (219 loc) · 9.34 KB
/
SkuModule.java
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
package band.wukong.mz.g.sku.module;
import band.wukong.mz.base.exception.AppRuntimeException;
import band.wukong.mz.g.AppConf;
import band.wukong.mz.g.category.SimpleCateConst;
import band.wukong.mz.g.category.bean.Category;
import band.wukong.mz.g.sku.SkuMoreHelper;
import band.wukong.mz.g.sku.bean.Goods;
import band.wukong.mz.g.sku.bean.Sku;
import band.wukong.mz.g.sku.bean.SkuPropType;
import band.wukong.mz.g.sku.service.GoodsService;
import band.wukong.mz.g.sku.service.SkuMoreViewService;
import band.wukong.mz.g.sku.service.SkuPropTypeService;
import band.wukong.mz.g.sku.service.SkuService;
import band.wukong.practice.barcode.zxing.BarcodeTool;
import com.google.zxing.BarcodeFormat;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.mvc.upload.UploadAdaptor;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;
/**
* As you see...
*
* @author wukong(wukonggg@139.com)
*/
@IocBean
@At("/stock/sku")
@Filters(@By(type = CheckSession.class, args = {"me", "/entry.io"}))
public class SkuModule {
private static final Log log = Logs.get();
private static final String GIMG_RELATIVE_PATH = "/gimg/sku/";
public static final int SKU_ADD_S1_PAGE_SIZE = 4;
@Inject
private SkuService skuService;
@Inject("refer:skuMoreViewService")
private SkuMoreViewService smvService;
@Inject("refer:goodsService")
private GoodsService gService;
@Inject("refer:skuPropTypeService")
private SkuPropTypeService sptService;
@Inject("app_conf")
private AppConf appConf;
@At("/list")
@Ok("jsp:view.sku.sku_list")
public Object list(@Param("cateCode") String cateCode, @Param("qcond") String qcond, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize) {
log.debug("Input params - cateCode: " + cateCode);
log.debug("Input params - qcond: " + qcond);
log.debug("Input params - pageNum: " + pageNum);
log.debug("Input params - pageSize: " + pageSize);
if (Strings.isBlank(qcond)) {
qcond = "";
}
if (Strings.isBlank(cateCode)) {
cateCode = Category.CATE_CODE_TYPE_SIMPLE;
}
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("qcond", qcond);
retMap.put("cateCode", cateCode);
retMap.put("result", skuService.list(cateCode, qcond, pageNum, pageSize));
return retMap;
}
@At("/add_s1")
@Ok("jsp:view.sku.sku_add_s1")
public Object add_s1(@Param("qcond") String qcond, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize) {
log.debug("Input params - qcond: " + qcond);
log.debug("Input params - pageNum: " + pageNum);
log.debug("Input params - pageSize: " + pageSize);
if (null == qcond) {
qcond = "";
}
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("qcond", qcond);
retMap.put("result", gService.list(null, qcond, pageNum, SKU_ADD_S1_PAGE_SIZE));
return retMap;
}
@At("/add/s2")
@Ok("jsp:view.sku.sku_add_s2")
public Object add_s2(@Param("..") Sku sc) {
log.debug("Input params - Sku: \n" + sc);
Goods g = gService.find(sc.getGoodsId());
List<SkuPropType> sptList = sptService.listDistinctNameByCateCode(g.getCateCode());
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("g", g);
retMap.put("sptList", sptList);
return retMap;
}
@At("/save")
@Ok("json")
@Fail("json")
@AdaptBy(type = UploadAdaptor.class, args = {"${app.root}/WEB-INF/tmp"})
public Object save(@Param("..") Sku sku, @Param("inputFile") File img,
@Param("more") String more, HttpServletRequest req) throws IOException {
log.debug("Input params - sku: \n" + sku);
log.debug("Input params - image file is uploaded: " + Boolean.valueOf(null != img));
log.debug("Input params - more: " + more);
NutMap re = new NutMap();
sku.setMoreList(SkuMoreHelper.convert2ListWhenSave(more));
sku.setGimg(img);
String path = req.getSession().getServletContext().getRealPath(GIMG_RELATIVE_PATH);
try {
skuService.saveWithMore(sku, path);
} catch (AppRuntimeException e) {
return re.setv("ok", false).setv("msg", e.getMessage());
}
// return for test
// return re.setv("ok", false).setv("msg", "lalalalld");
return re.setv("ok", true);
}
@At("/printBarcode")
@Ok("raw:txt")
public Object printBarcode(@Param("barcode") String barcode) {
log.debug("Input params - barcode: \n" + barcode);
int width = appConf.getInt(AppConf.BARCODE_IMAGE_WIDTH);
int height = appConf.getInt(AppConf.BARCODE_IMAGE_HEIGHT);
String format = appConf.get(AppConf.BARCODE_IMAGE_FORMAT);
String way = appConf.get(AppConf.BARCODE_IMAGE_PATH);
way = way.replace("$name", UUID.randomUUID().toString());
FileSystem fileSystem = FileSystems.getDefault();
Path path = fileSystem.getPath(way);
BarcodeTool.encode(barcode, BarcodeFormat.CODE_128, width, height, path, format);
return Files.findFile(way);
}
@At("/mod")
@Ok("jsp:view.sku.sku_mod")
public Object mod(@Param("id") Long id) {
log.debug("Input params - id: \n" + id);
Sku sc = skuService.findWithLinks(id);
List<SkuPropType> sptList = sptService.listDistinctNameByCateCode(sc.getGoods().getCateCode());
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("sc", sc);
retMap.put("sptList", sptList);
return retMap;
}
@At("/upd")
@Ok("json")
@Fail("json")
@AdaptBy(type = UploadAdaptor.class, args = {"${app.root}/WEB-INF/tmp"})
public Object upd(@Param("..") Sku sc, @Param("inputFile") File img,
@Param("more") String more, HttpServletRequest req) throws IOException {
log.debug("Input params - sc: \n" + sc);
log.debug("Input params - image file is uploaded: " + Boolean.valueOf(null != img));
log.debug("Input params - more: " + more);
NutMap re = new NutMap();
if (sc.getId() <= 0) {
throw new AppRuntimeException("木有找到你要更新的产品。。。");
}
sc.setGimg(img);
sc.setMoreList(SkuMoreHelper.convert2ListWhenUpdate(more));
String path = req.getSession().getServletContext().getRealPath(GIMG_RELATIVE_PATH);
try {
skuService.updateWithMore(sc, path);
} catch (AppRuntimeException e) {
return re.setv("ok", false).setv("msg", e.getMessage());
}
// return for test
// return re.setv("ok", false).setv("msg", "lalalalld");
return re.setv("ok", true);
}
@At("/rm")
@Ok("json")
@Fail("json")
// @Ok("redirect:/stock/sku/list.io")
public Object rm(@Param("skuId") Long id) {
log.debug("Input params - skuId:" + id);
if (null == id || id <= 0) {
throw new AppRuntimeException("木有找到你要删除的产品SKU。。。");
}
NutMap re = new NutMap();
try {
skuService.rm(id);
} catch (Exception e) {
if (e instanceof AppRuntimeException) {
return re.setv("ok", false).setv("msg", e.getMessage());
} else {
return re.setv("ok", false).setv("msg", "你从火星来的吗???");
}
}
return re.setv("ok", true);
}
@At("/rest")
@Ok("jsp:view.sku.sku_rest")
public Object rest(@Param("cateCodes") String[] cateCodes, @Param("counts") int[] counts
,@Param("pageNum") int pageNum, @Param("pageSize") int pageSize) {
if(null == cateCodes || cateCodes.length == 0) {
cateCodes = loadSimpleCateCodes4rest();
}
if(null == counts || counts.length == 0) {
counts = new int[] {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
}
log.debug("Input params - cateCodes:" + Arrays.toString(cateCodes));
log.debug("Input params - counts:" + Arrays.toString(counts));
QueryResult qr = smvService.listSkuRest(cateCodes, counts, pageNum, pageSize);
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("cateCodes", Arrays.toString(cateCodes).replaceAll("\\[|\\]", ""));
retMap.put("counts", Arrays.toString(counts).replaceAll("\\[|\\]", ""));
retMap.put("result", qr);
return retMap;
}
private String[] loadSimpleCateCodes4rest() {
return new String[] {SimpleCateConst.CATE_CODE_A_SYTZ, SimpleCateConst.CATE_CODE_B_KZ
, SimpleCateConst.CATE_CODE_C_NY, SimpleCateConst.CATE_CODE_D_XZ, SimpleCateConst.CATE_CODE_E_RH
, SimpleCateConst.CATE_CODE_F_LSBJ, SimpleCateConst.CATE_CODE_G_NF, SimpleCateConst.CATE_CODE_H_WJ
, SimpleCateConst.CATE_CODE_I_NBS, SimpleCateConst.CATE_CODE_J_WJ
};
}
}