yujian
2020-04-24 f276a5a1fc38e735499aa95089ff8105bd2ba196
fanli/src/main/java/com/yeshi/fanli/controller/admin/BrandShopAdminController.java
@@ -20,12 +20,15 @@
import com.yeshi.fanli.dto.taobao.TaoBaoShopInfoDTO;
import com.yeshi.fanli.entity.brand.BrandClass;
import com.yeshi.fanli.entity.brand.BrandClassShop;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo;
import com.yeshi.fanli.exception.brand.BrandClassException;
import com.yeshi.fanli.exception.brand.BrandClassShopException;
import com.yeshi.fanli.exception.brand.BrandInfoException;
import com.yeshi.fanli.service.inter.brand.BrandClassService;
import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
@@ -43,6 +46,9 @@
   @Resource
   private BrandClassShopService brandClassShopService;
   @Resource
   private BrandInfoService brandInfoService;
   /**
    * 保存信息
@@ -97,7 +103,6 @@
   @RequestMapping(value = "query")
   public void query(String callback, Integer pageIndex, Integer pageSize, String key, Integer state,
         PrintWriter out) {
      if (pageIndex == null || pageIndex < 1) {
         pageIndex = 1;
      }
@@ -108,7 +113,6 @@
      try {
         List<BrandClass> list = brandClassService.listQuery((pageIndex - 1) * pageSize, pageSize, key, state);
         if (list == null || list.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
            return;
@@ -126,11 +130,28 @@
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("result_list", gson.toJson(list));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 修改状态
    * @param callback
    * @param id
    * @param out
    */
   @RequestMapping(value = "switchStateClass")
   public void switchStateClass(String callback, Long id, PrintWriter out) {
      try {
         brandClassService.switchState(id);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("操作成功"));
      } catch (BrandClassException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作失败"));
         e.printStackTrace();
      }
   }
@@ -144,7 +165,6 @@
    */
   @RequestMapping(value = "delete")
   public void delete(String callback, String idArray, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(idArray)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择操作的数据"));
@@ -429,4 +449,171 @@
         e.printStackTrace();
      }
   }
   /**
    * 品牌信息保存
    * @param callback
    * @param brandInfo
    * @param out
    */
   @RequestMapping(value = "saveBrand")
   public void saveBrand(String callback, BrandInfo brandInfo, HttpServletRequest request, PrintWriter out) {
      try {
         // 1. 先判断httpRequest 是否含有文件类型
         if (request instanceof MultipartHttpServletRequest) {
              MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
              brandInfoService.saveObject(fileRequest.getFile("file"), brandInfo);
           }else{
              brandInfoService.saveObject(null, brandInfo);
           }
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("保存成功"));
      } catch (BrandInfoException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("保存失败"));
         e.printStackTrace();
      }
   }
   /**
    * 修改状态
    * @param callback
    * @param id
    * @param out
    */
   @RequestMapping(value = "switchStateBrand")
   public void switchStateBrand(String callback, Long id, PrintWriter out) {
      try {
         brandInfoService.switchState(id);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("操作成功"));
      } catch (BrandInfoException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作失败"));
         e.printStackTrace();
      }
   }
   /**
    * 删除
    *
    * @param callback
    * @param idArray
    * @param out
    */
   @RequestMapping(value = "deleteBrand")
   public void deleteBrand(String callback, String idArray, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(idArray)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择操作的数据"));
            return;
         }
         Gson gson = new Gson();
         List<Long> list = gson.fromJson(idArray, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (list == null || list.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未检测到删除的数据"));
            return;
         }
         int count = brandInfoService.deleteBatchByPrimaryKey(list);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("成功删除[" + count + "]条数据"));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("删除失败"));
         e.printStackTrace();
      }
   }
   /**
    * 修改排序
    *
    * @param callback
    * @param goodsClass
    * @param out
    */
   @RequestMapping(value = "updateOrderBrand")
   public void updateOrderBrand(String callback, Long id, Integer moveType, PrintWriter out) {
      try {
         brandInfoService.updateOrder(id, moveType);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("操作成功"));
      } catch (BrandInfoException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作失败"));
         e.printStackTrace();
      }
   }
   /**
    * 品牌信息查询
    * @param callback
    * @param pageIndex
    * @param pageSize
    * @param key
    * @param cid
    * @param state
    * @param out
    */
   @RequestMapping(value = "queryBrand")
   public void queryBrand(String callback, Integer pageIndex, Integer pageSize, String key, Long cid, Integer state,
         Integer showState, PrintWriter out) {
      if (pageIndex == null || pageIndex < 1) {
         pageIndex = 1;
      }
      if (pageSize == null || pageSize < 1) {
         pageSize = Constant.PAGE_SIZE;
      }
      List<String> keys = null;
      if (!StringUtil.isNullOrEmpty(key)) {
         String[] array = key.split("\\s+");
         if (array != null && array.length > 0) {
            keys = new ArrayList<String>();
            for (int i= 0; i < array.length; i++) {
               keys.add(array[i].toString());
            }
         }
      }
      try {
         List<BrandInfo> list = brandInfoService.listQuery((pageIndex - 1) * pageSize, pageSize, keys, cid, state, showState);
         if (list == null || list.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
            return;
         }
         for (BrandInfo brandInfo: list) {
            BrandClass brandClass = brandInfo.getBrandClass();
            if (brandClass != null && StringUtil.isNullOrEmpty(brandClass.getName())) {
               BrandClass bclass = brandClassService.selectByPrimaryKey(brandClass.getId());
               if (bclass != null) {
                  brandInfo.setBrandClass(bclass);
               }
            }
         }
         long count = brandInfoService.countQuery(keys, cid, state, showState);
         int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls();
         Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("result_list", gson.toJson(list));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
}