package com.yeshi.fanli.controller.admin; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabel; import com.yeshi.fanli.exception.goods.GoodsClassException; import com.yeshi.fanli.service.inter.clazz.GoodsSubClassLabelService; import com.yeshi.fanli.tag.PageEntity; import com.yeshi.fanli.util.StringUtil; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/v1/goodsSubClassLabel") public class GoodsSubClassLabelAdminController { @Resource private GoodsSubClassLabelService goodsSubClassLabelService; /** * 标签列表 * * @param callback * @param classId * @param pageIndex * @param pageSize * @param request * @param out */ @RequestMapping(value = "labelList") public void labelList(String callback, Long classId, int pageIndex, int pageSize, HttpServletRequest request, PrintWriter out) { if (classId != null && classId == 0) classId = null; List list = goodsSubClassLabelService.listLabelByClassId(classId); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); // 重点 Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create(); JSONObject data = new JSONObject(); data.put("resultList", gson.toJson(list)); PageEntity pe = new PageEntity(pageIndex, pageSize, list.size(), 1); data.put("pe", pe); out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data))); } /** * 保存信息 * * @param callback * @param special * @param out */ @RequestMapping(value = "saveLabel") public void save(String callback, GoodsSubClassLabel label, HttpServletRequest request, PrintWriter out) { if (StringUtil.isNullOrEmpty(label.getName()) || label.getGoodsClass() == null || label.getOrderBy() == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("数据不完整")); return; } if (label.getId() == null)// 新增 { if (label.getGoodsClass().getId() == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("分类不能为空")); return; } label.setCreateTime(new Date()); try { String[] names = label.getName().replace(",", ",").split(","); int orderBy = label.getOrderBy(); for (String name : names) { label.setId(null); label.setOrderBy(orderBy++); label.setName(name); goodsSubClassLabelService.addSubClassLabel(label); } } catch (GoodsClassException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMessage())); } JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("添加成功")); } else {// 修改 goodsSubClassLabelService.updateSubClassLabel(label); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("修改成功")); } } /** * 批量删除 * * @param callback * @param ids * @param out */ @RequestMapping(value = "deleteLabelBatch") public void deleteBatch(String callback, String ids, PrintWriter out) { Gson gson = new Gson(); try { List recordIds = gson.fromJson(ids, new TypeToken>() { }.getType()); if (recordIds == null || recordIds.size() == 0) { out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择需删除数据"))); } else { for (String id : recordIds) goodsSubClassLabelService.deleteLabel(Long.parseLong(id)); out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功"))); } } catch (Exception e) { out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常"))); e.printStackTrace(); } } }