package com.yeshi.fanli.controller.admin;
|
|
import java.io.PrintWriter;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
|
import net.sf.json.JSONObject;
|
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
import org.yeshi.utils.JsonUtil;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.fanli.entity.admin.GoodsClassAdmin;
|
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
|
import com.yeshi.fanli.entity.system.System;
|
import com.yeshi.fanli.service.inter.config.SystemService;
|
import com.yeshi.fanli.service.inter.goods.ClassRecommendGoodsService;
|
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
|
import com.yeshi.fanli.service.inter.goods.GoodsSecondClassService;
|
import com.yeshi.fanli.service.inter.goods.GoodsSubClassService;
|
import com.yeshi.fanli.service.inter.goods.SuperGoodsClassService;
|
import com.yeshi.fanli.service.inter.lable.LabelClassService;
|
import com.yeshi.fanli.tag.PageEntity;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.Utils;
|
|
@Controller
|
@RequestMapping("admin/new/api/v1/goodsclass")
|
public class GoodsClassAdminCotroller {
|
|
@Resource
|
private GoodsClassService goodsClassService;
|
|
@Resource
|
private SuperGoodsClassService superGoodsClassService;
|
|
@Resource
|
private ClassRecommendGoodsService classRecommendGoodsService;
|
|
@Resource
|
private GoodsSecondClassService goodsSecondClassService;
|
|
@Resource
|
private GoodsSubClassService goodsSubClassService;
|
|
@Resource
|
private LabelClassService labelClassService;
|
|
@Resource
|
private SystemService systemService;
|
|
// private static final String IMGPATH="/upload/class";
|
|
@RequestMapping(value = "getGoodsClassList"/* , method = RequestMethod.POST */)
|
public void getGoodsClassList(String callback, Integer pageIndex, String platform, String packages, String key, PrintWriter out) {
|
|
List<GoodsClassAdmin> goodsClassList = goodsClassService.getGoodsClassAdmins(pageIndex - 1, platform, packages, key);
|
|
int count = goodsClassService.getCount(platform, packages, key);
|
int totalPage = count % Constant.PAGE_SIZE == 0 ? count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;
|
PageEntity pe = new PageEntity(pageIndex, Constant.PAGE_SIZE, count, totalPage);
|
Map<String, String> map = new HashMap<String, String>();
|
map.put("platform", platform);
|
map.put("packages", packages);
|
map.put("key", key);
|
pe.setParams(map);
|
JSONObject data = new JSONObject();
|
data.put("pe", pe);
|
List<System> systemList = systemService.getSystems();
|
|
data.put("systemList", systemList);
|
data.put("goodsClassList", goodsClassList);
|
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
|
|
// out.print(JsonUtil.loadTrueResult(data));
|
|
return;
|
|
}
|
|
@RequestMapping(value = "queryAll")
|
public void queryAll(String callback, String platform, String packages, PrintWriter out) {
|
|
try {
|
List<GoodsClass> goodsClassList = goodsClassService.queryAll(platform, packages);
|
|
if (goodsClassList == null || goodsClassList.size() == 0) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
|
return;
|
}
|
|
Map<String, String> map = new HashMap<String, String>();
|
map.put("platform", platform);
|
map.put("packages", packages);
|
JSONObject data = new JSONObject();
|
|
// List<System> systemList = systemService.getSystems();
|
// data.put("systemList", systemList);
|
|
data.put("goodsClassList", goodsClassList);
|
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
|
}
|
|
}
|
|
@RequestMapping(value = "getGoodsClasAll"/* , method = RequestMethod.POST */)
|
public void getGoodsClassAll(String callback, PrintWriter out) {
|
List<GoodsClass> goodsClassList = goodsClassService.getGoodsClassAll();
|
|
if (goodsClassList == null || goodsClassList.size() == 0) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
|
} else {
|
|
JSONObject data = new JSONObject();
|
data.put("goodsClassList", goodsClassList);
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
|
}
|
}
|
|
@RequestMapping(value = "addGoodsClass", method = RequestMethod.POST)
|
public void addGoodsClass(GoodsClass goodsClass, PrintWriter out) {
|
if (goodsClass == null) {
|
out.print(JsonUtil.loadFalseResult("goodsClass不能为空"));
|
return;
|
}
|
Integer res = goodsClassService.addGoodsClass(goodsClass);
|
if (res == null) {
|
out.print(JsonUtil.loadTrueResult("添加分类成功"));
|
} else {
|
out.print(JsonUtil.loadFalseResult("添加失败"));
|
}
|
return;
|
}
|
|
// @RequestMapping(value="uploadImg",method=RequestMethod.POST)
|
// public void uploadImg(@RequestParam("file") CommonsMultipartFile
|
// file,HttpServletRequest request,PrintWriter out){
|
// ServletContext servletContext = request.getSession().getServletContext();
|
// JSONObject json=new JSONObject();
|
// if(file != null){
|
// String root=servletContext.getRealPath(IMGPATH);
|
// if (!new File(root).exists()){
|
// new File(root).mkdirs();
|
// }
|
// File newFile=new File(root+"/"+file.getOriginalFilename());
|
// //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
|
// try {
|
// file.transferTo(newFile);
|
// json.put("code", "0");
|
// json.put("path", IMGPATH+"/"+newFile.getName());
|
// } catch (IllegalStateException e) {
|
// e.printStackTrace();
|
// json.put("code", "1");
|
// } catch (IOException e) {
|
// e.printStackTrace();
|
// json.put("code", "1");
|
// }finally{
|
// out.print(json);
|
// }
|
// }else{
|
// json.put("code", "1");
|
// out.print(json);
|
// }
|
// }
|
|
@RequestMapping(value = "setGoodsClassSystem", method = RequestMethod.POST)
|
public void setGoodsClassSystem(String type, long gcid, String platform, String packageName, PrintWriter out) {
|
platform = Utils.getMap().get(platform);
|
if (Constant.DEL.equals(type)) {
|
Integer integer = superGoodsClassService.deleteSuperGoodsClass(gcid, platform, packageName);
|
if (integer > 0) {
|
out.print(JsonUtil.loadTrueResult("删除成功"));
|
} else {
|
out.print(JsonUtil.loadTrueResult("删除失败"));
|
}
|
} else {
|
superGoodsClassService.addSuperGoodsClass(gcid, platform, packageName);
|
out.print(JsonUtil.loadTrueResult("添加成功"));
|
}
|
}
|
|
@RequestMapping(value = "getGoodsClass", method = RequestMethod.POST)
|
public void getGoodsClass(long id, PrintWriter out) {
|
GoodsClass rb = goodsClassService.getGoodsClass(id);
|
if (rb != null) {
|
out.print(JsonUtil.loadTrueResult(rb));
|
return;
|
}
|
out.print(JsonUtil.loadFalseResult("不存在该GoodsClass"));
|
}
|
|
@RequestMapping(value = "deleteGoodsClass", method = RequestMethod.POST)
|
public void deleteGoodsClass(long[] gcids, PrintWriter out) {
|
goodsClassService.deleteGoodsClasss(gcids);
|
out.print(JsonUtil.loadTrueResult("删除成功"));
|
}
|
|
@RequestMapping(value = "updateGoodsClass", method = RequestMethod.POST)
|
public void updateGoodsClass(GoodsClass goodsClass, PrintWriter out) {
|
goodsClassService.updateGoodsClass(goodsClass);
|
out.print(JsonUtil.loadTrueResult("修改成功"));
|
}
|
|
/**
|
* 添加类别
|
*
|
* @param callback
|
* @param goodsClass
|
* @param request
|
* @param out
|
*/
|
@RequestMapping(value = "saveAdd", method = RequestMethod.POST)
|
public void saveAdd(String callback, GoodsClass goodsClass, HttpServletRequest request, PrintWriter out) {
|
try {
|
|
String name = goodsClass.getName();
|
if (StringUtil.isNullOrEmpty(name)) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("类别名称不能为空"));
|
return;
|
}
|
|
if (request instanceof MultipartHttpServletRequest) {
|
|
long result;
|
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
|
|
goodsClass.setAndroidClick(0L);
|
goodsClass.setIosClick(0L);
|
// 搜索条件:有券、在售价20-200、牛皮癣轻微
|
goodsClass.setSearchParam("{\"quan\":1,\"endPrice\":220,\"includeGoodRate\":true,\"baoYou\":true,\"ip\":\"218.72.111.105\"}");
|
|
if (files != null && files.size() > 0) {
|
// 图片文件上传
|
result = goodsClassService.saveAdd(goodsClass, files.get(0));
|
} else {
|
// 无图保存
|
result = goodsClassService.saveAdd(goodsClass, null);
|
}
|
|
if (result > 0) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("保存成功"));
|
} else {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("保存失败"));
|
}
|
|
} else {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请传递正确的参数"));
|
}
|
|
} catch (Exception e) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
|
e.printStackTrace();
|
}
|
|
}
|
|
/**
|
* 保存修改信息
|
*
|
* @param callback
|
* @param goodsClass
|
* @param out
|
*/
|
@RequestMapping(value = "saveModify")
|
public void saveModify(String callback, GoodsClass goodsClass, PrintWriter out) {
|
|
try {
|
|
Long id = goodsClass.getId();
|
|
GoodsClass resultObj = goodsClassService.getGoodsClass(id);
|
|
if (resultObj == null) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("该类别不存在或已被删除"));
|
} else {
|
String name = goodsClass.getName();
|
if (!StringUtil.isNullOrEmpty(name)) {
|
resultObj.setName(name);
|
}
|
|
String key = goodsClass.getKey();
|
if (!StringUtil.isNullOrEmpty(key)) {
|
resultObj.setKey(key);
|
}
|
|
goodsClassService.updateGoodsClass(resultObj);
|
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("修改成功"));
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
|
}
|
|
}
|
|
/**
|
* 保存修改信息
|
*
|
* @param callback
|
* @param goodsClass
|
* @param out
|
*/
|
@RequestMapping(value = "saveOrderby")
|
public void saveOrderby(String callback, Long id, int orderby, PrintWriter out) {
|
|
try {
|
|
GoodsClass resultObj = goodsClassService.getGoodsClass(id);
|
|
if (resultObj == null) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("该类别不存在或已被删除"));
|
} else {
|
|
List<GoodsClass> glist = goodsClassService.getByorderby(orderby);
|
if (glist != null && glist.size() > 0) {
|
GoodsClass changeObj = glist.get(0);
|
changeObj.setOrderby(resultObj.getOrderby());
|
goodsClassService.updateGoodsClass(changeObj);
|
}
|
|
resultObj.setOrderby(orderby);
|
goodsClassService.updateGoodsClass(resultObj);
|
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("修改成功"));
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
|
}
|
|
}
|
|
/**
|
* 批量删除
|
*
|
* @param callback
|
* @param ids
|
* @param out
|
*/
|
@RequestMapping(value = "deleteBatch")
|
public void deleteBatch(String callback, String ids, PrintWriter out) {
|
|
Gson gson = new Gson();
|
|
try {
|
List<String> recordIds = gson.fromJson(ids, new TypeToken<ArrayList<String>>() {
|
}.getType());
|
|
if (recordIds == null || recordIds.size() == 0) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择需删除数据"));
|
} else {
|
|
for (String id : recordIds) {
|
Long recordId = Long.parseLong(id);
|
|
superGoodsClassService.deleteSuperGoodsClass(recordId);
|
classRecommendGoodsService.deleteClassGoodsByGC(recordId);
|
|
// goodsSecondClassService.deleteSecondClassByGC(recordId);
|
|
// 删除子类分类
|
goodsSubClassService.deleteByRootId(recordId);
|
// 删除类别关联标签
|
labelClassService.deleteByClassId(recordId);
|
|
goodsClassService.deleteGoodsClass(recordId);
|
}
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("删除成功"));
|
}
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
|
e.printStackTrace();
|
}
|
|
}
|
|
//
|
/**
|
* 上传/修改 图片
|
*
|
* @param callback
|
* @param file
|
* @param request
|
* @param out
|
* @param response
|
*/
|
@RequestMapping(value = "uploadPicture")
|
public void uploadPicture(Long id, @RequestParam("file") CommonsMultipartFile file, PrintWriter out) {
|
|
try {
|
GoodsClass goodsClass = goodsClassService.getGoodsClass(id);
|
|
if (goodsClass == null ) {
|
out.print(JsonUtil.loadFalseResult("该类别不存在或已被删除"));
|
return;
|
}
|
|
if (file == null) {
|
out.print(JsonUtil.loadFalseResult("图片文件不能为空"));
|
return;
|
}
|
|
goodsClassService.uploadPicture(goodsClass, file);
|
out.print(JsonUtil.loadTrueResult("上传成功"));
|
} catch (Exception e) {
|
e.printStackTrace();
|
out.print(JsonUtil.loadFalseResult("操作异常"));
|
}
|
}
|
|
/**
|
* 删除图片
|
*
|
* @param callback
|
* @param file
|
* @param out
|
* @param response
|
*/
|
@RequestMapping(value = "removePicture")
|
public void removePicture(String callback, Long id, PrintWriter out) {
|
|
try {
|
GoodsClass goodsClass = goodsClassService.getGoodsClass(id);
|
|
if (goodsClass == null) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该类别不存在或已被删除")));
|
return;
|
}
|
|
goodsClassService.removePicture(goodsClass);
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("图片删除成功")));
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
|
}
|
|
}
|
|
@RequestMapping(value = "setSuperSystem")
|
public void setSuperSystem(String callback,String type, Long gcid, String platform, String packageName, PrintWriter out) {
|
platform = Utils.getMap().get(platform);
|
if (Constant.DEL.equals(type)) {
|
Integer integer = superGoodsClassService.deleteSuperGoodsClass(gcid, platform, packageName);
|
if (integer > 0) {
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("删除成功"));
|
} else {
|
JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("删除失败"));
|
}
|
} else {
|
superGoodsClassService.addSuperGoodsClass(gcid, platform, packageName);
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("添加成功"));
|
}
|
}
|
|
|
@RequestMapping(value = "getClassOption")
|
public void getClassOption(String callback, PrintWriter out) {
|
|
List<GoodsClass> goodsClassList = goodsClassService.getGoodsClassAll();
|
|
JSONObject data = new JSONObject();
|
|
data.put("result_list", goodsClassList);
|
|
JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
|
|
}
|
}
|