admin
2021-05-14 ae2294be876ac4595d7b31b36c0057726d12354f
fanli/src/main/java/com/yeshi/fanli/controller/admin/QualityFactoryAdminController.java
@@ -1,974 +1,937 @@
package com.yeshi.fanli.controller.admin;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.lable.BoutiqueAutoRule;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.QualityFactoryException;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.LabelGoodsService;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.quality.QualityFactoryVO;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("admin/new/api/v1/qualityFactory")
public class QualityFactoryAdminController {
   @Resource
   private RedisManager redisManager;
   @Resource
   private LabelService labelService;
   @Resource
   private LabelGoodsService labelGoodsService;
   @Resource
   private QualityFactoryService qualityFactoryService;
   @Resource
   private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
   @Resource
   private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
   public static final int SALES_ASC = 1;
   public static final int SALES_DESC = 2;
   public static final int PRICE_ASC = 3;
   public static final int PRICE_DESC = 4;
   public static final int TKRATE_ASC = 5;
   public static final int TKRATE_DESC = 6;
   public static final int PROPOR_ASC = 7;
   public static final int PROPOR_DESC = 8;
   /**
    * 批量添加标签 --- 淘宝
    *
    * @param callback
    * @param label
    * @param request
    * @param out
    */
   @RequestMapping(value = "addBatch")
   public void addBatch(String callback, String auctionIds, String lableNames, HttpServletRequest request, PrintWriter out) {
      try {
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         if (admin == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("当前账户失效,请重新登陆。")));
            return;
         }
         if (StringUtil.isNullOrEmpty(auctionIds)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择正确的商品入库")));
            return;
         }
         Gson gson = new Gson();
         List<Long> auctionIdList = gson.fromJson(auctionIds, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (auctionIdList == null || auctionIdList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择正确的商品入库")));
            return;
         }
         qualityFactoryService.addBatch(auctionIdList,lableNames, admin);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("添加成功")));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 保存修改精品库商品关联信息
    *
    * @param callback
    * @param label
    * @param request
    * @param out
    */
   @RequestMapping(value = "saveModify")
   public void saveModify(String callback, QualityFactory selectionGoods, HttpServletRequest request, PrintWriter out) {
      try {
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         if (admin == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("当前账户已失效,请重新登陆")));
         } else {
            Long id = selectionGoods.getId();
            QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
            if (resultObj == null) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            } else {
               Date nowTime = new Date();
               selectionGoods.setUpdatetime(nowTime);
               selectionGoods.setUpdateUser(admin);
               // 选择性更新数据
               int result = qualityFactoryService.updateByPrimaryKeySelective(selectionGoods);
               if (result > 0) {
                  out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
               } else {
                  out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
               }
            }
         }
      } catch (QualityFactoryException e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 统计商品数量
    *
    * @param callback
    * @param out
    */
   @RequestMapping(value = "getCountAll")
   public void getCountAll(String callback, PrintWriter out) {
      try {
         Map<String, Object> countMap = qualityFactoryService.getCountAll();
         JSONObject data = new JSONObject();
         data.put("countData", countMap);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
      }
   }
   /**
    * 批量移除商品至精品库外-不删除商品
    *
    * @param callback
    * @param ids
    *            商品id
    * @param out
    */
   @RequestMapping(value = "removeGoods")
   public void removeGoods(String callback, String ids, PrintWriter out) {
      try {
         Gson gson = new Gson();
         List<String> goodsIds = gson.fromJson(ids, new TypeToken<ArrayList<String>>() {}.getType());
         if (goodsIds == null || goodsIds.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检测到移除商品数据")));
         } else {
            // 商品移除精品库
            qualityFactoryService.deleteByGoodsId(goodsIds);
            // 清理商品所有标签
            labelGoodsService.deleteByGoodsId(goodsIds);
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("移除成功")));
         }
      } catch (Exception e) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作失败")));
         e.printStackTrace();
      }
   }
   /**
    * 查询
    * @param out
    */
   @RequestMapping(value = "query")
   public void query(String callback, Integer pageIndex,Integer pageSize,  Integer sort,
         QualityFactoryVO qualityFactoryVO, PrintWriter out) {
      try {
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         if (pageIndex == null)
            pageIndex = 1;
         Integer price = null;
         Integer tkRate = null;
         Integer totalSales = null;
         Integer propor = null;
         if (sort != null) {
            switch (sort) {
            case SALES_ASC:
               totalSales = 1;
               break;
            case SALES_DESC:
               totalSales = 2;
               break;
            case PRICE_ASC:
               price = 1;
               break;
            case PRICE_DESC:
               price = 2;
               break;
            case TKRATE_ASC:
               tkRate = 1;
               break;
            case TKRATE_DESC:
               tkRate = 2;
               break;
            case PROPOR_ASC:
               propor = 1;
               break;
            case PROPOR_DESC:
               propor = 2;
               break;
            default:
               break;
            }
         }
         qualityFactoryVO.setPrice(price);
         qualityFactoryVO.setTkRate(tkRate);
         qualityFactoryVO.setTotalSales(totalSales);
         qualityFactoryVO.setPropor(propor);
         qualityFactoryVO.setStart((pageIndex - 1) * pageSize);
         qualityFactoryVO.setCount(pageSize);
         List<QualityFactory> selectionList = qualityFactoryService.query(qualityFactoryVO);
         /* 暂无数据显示 */
         if (selectionList == null || selectionList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
            return;
         }
         /* 有数据:处理返回格式 */
         long count = qualityFactoryService.queryCount(qualityFactoryVO);
         int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
         /*  验证网络: 获取商品链接 图片链接   */
         List<TaoBaoGoodsBrief> goodsBriefList = new ArrayList<TaoBaoGoodsBrief>();
         StringBuffer idBuf = new StringBuffer();
         StringBuffer auctionIdBuf = new StringBuffer();
         int j = 0;
         for (int i = 0; i < selectionList.size(); i++ ) {
            QualityFactory selectionGoods = selectionList.get(i);
            TaoBaoGoodsBrief taoBaoGoodsBrief = selectionGoods.getTaoBaoGoodsBrief();
            if (taoBaoGoodsBrief == null)
               continue; // 为空结束本次循环
            Long id = taoBaoGoodsBrief.getId();
            idBuf.append(id + ",");
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            if (auctionId != null) {
               j++;
               auctionIdBuf.append(auctionId + ",");
            }
            if (selectionList.size() <= 40) {
               // 小于40个参数
               if (i == selectionList.size()-1 ){
                  if (auctionIdBuf != null && auctionIdBuf.length() > 0) {
                     String auctionIds = auctionIdBuf.toString();
                     String ids = auctionIds.substring(0, auctionIds.length() -1);
                     List<TaoBaoGoodsBrief> list = null;
                     try {
                        list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                     } catch (Exception e) {
                        e.printStackTrace();
                     }
                     if (list != null && list.size() > 0) {
                        goodsBriefList.addAll(list);
                     }
                     auctionIdBuf = new StringBuffer();
                  }
               }
            } else {
               if (j == 40) {
                  // 大于40参数
                  String auctionIds = auctionIdBuf.toString();
                  String ids = auctionIds.substring(0, auctionIds.length() -1);
                  List<TaoBaoGoodsBrief> list = null;
                  try {
                     list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
                  if (list != null && list.size() > 0) {
                     goodsBriefList.addAll(list);
                  }
                  j= 0;
                  auctionIdBuf = new StringBuffer();
               } else if (i == selectionList.size()-1 ){
                  // 最后一个
                  String auctionIds = auctionIdBuf.toString();
                  String ids = auctionIds.substring(0, auctionIds.length() -1);
                  List<TaoBaoGoodsBrief> list = null;
                  try {
                     list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
                  if (list != null && list.size() > 0) {
                     goodsBriefList.addAll(list);
                  }
                  auctionIdBuf = new StringBuffer();
               }
            }
         }
         for (QualityFactory selectionGoods : selectionList) {
            TaoBaoGoodsBrief taoBaoGoodsBrief = selectionGoods.getTaoBaoGoodsBrief();
            if (taoBaoGoodsBrief == null)
               continue; // 为空结束本次循环
            Map<String, Object> map = new HashMap<String, Object>();
            BoutiqueAutoRule boutiqueAutoRule = selectionGoods.getBoutiqueAutoRule();
            if (boutiqueAutoRule == null) {
               boutiqueAutoRule = new BoutiqueAutoRule();
               selectionGoods.setBoutiqueAutoRule(boutiqueAutoRule);
            }
            String reslutSource = "";
            Integer gsSource = selectionGoods.getGoodsSource();
            if (QualityFactory.SOURCE_TAOBAO == gsSource ) {
               reslutSource = "淘宝";
            } else if (QualityFactory.SOURCE_TAOBAO_MATERIAL == gsSource) {
               reslutSource = "淘宝推荐";
            } else if (QualityFactory.SOURCE_TAOBAO_DATAOKE == gsSource) {
               reslutSource = "大淘客";
            } else if (QualityFactory.SOURCE_JINGDONG == gsSource) {
               reslutSource = "京东";
            } else if (QualityFactory.SOURCE_PINDUODUO == gsSource) {
               reslutSource = "拼多多";
            } else if (QualityFactory.SOURCE_WEIPINHUI == gsSource) {
               reslutSource = "唯品会";
            } else if (QualityFactory.SOURCE_OTHER == gsSource) {
               reslutSource = "商务合作";
            }
            map.put("goodsSource", reslutSource);
            map.put("selectionGoods", selectionGoods);
            /* 商品销售状态: 0 在售 1停售 */
            int saleStae = 1;
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            if (auctionId != null) {
               if (goodsBriefList != null && goodsBriefList.size() > 0) {
                  for (TaoBaoGoodsBrief tb :goodsBriefList) {
                     Long auctionIdTB = tb.getAuctionId();
                     if (auctionId.equals(auctionIdTB)) {
                        saleStae = 0;
                     }
                  }
               }
            }
            map.put("saleStae", saleStae);
            /* 1 定向计划 2 营销返利 、高佣 3 普佣 */
            Integer includeDxjh = taoBaoGoodsBrief.getIncludeDxjh();
            String tkMktStatus = taoBaoGoodsBrief.getTkMktStatus();
            boolean include = false;
            if (includeDxjh != null) {
               if (includeDxjh == 1) {
                  // 定向计划
                  map.put("yongjinType", 1);
                  include = true;
               }
            }
            if (!include) {
               if ("1".equals(tkMktStatus)) {
                  // 营销返利 、高佣
                  map.put("yongjinType", 2);
               } else {
                  // 普佣
                  map.put("yongjinType", 3);
               }
            }
             /* 券后价--计算 */
            BigDecimal couponPrice = TaoBaoUtil.getAfterUseCouplePrice(taoBaoGoodsBrief);
            map.put("couponPrice", couponPrice);
            /* 预计收益: 公司、用户 */
            BigDecimal tkRateBrief = taoBaoGoodsBrief.getTkRate();
            BigDecimal zkPrice = taoBaoGoodsBrief.getZkPrice();
            BigDecimal profit = MoneyBigDecimalUtil.mul(tkRateBrief, zkPrice);
            // 计算结果
            BigDecimal estimateProfit = MoneyBigDecimalUtil.div(profit, new BigDecimal("100"));
            // 预计收益
            map.put("estimateProfit", estimateProfit);
            BigDecimal couponAmount = taoBaoGoodsBrief.getCouponAmount();
            if (couponAmount != null && zkPrice != null) {
               BigDecimal propors = couponAmount.divide(zkPrice ,10 ,BigDecimal.ROUND_HALF_DOWN);
               BigDecimal resultPropor = MoneyBigDecimalUtil.mul(propors, new BigDecimal("100"));
               map.put("propor", resultPropor);
            } else {
               map.put("propor", 0);
            }
            /* 商品标签列表 */
            Long id = taoBaoGoodsBrief.getId();
            int totalLabels = labelGoodsService.getCountQueryByGoodsId(id);
            //List<LabelGoods> labelList = labelService.getByGoodsId(id);
            map.put("totalLabels", totalLabels);
            listmap.add(map);
         }
         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("listGoods", gson.toJson(listmap));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 获取商品信息 以及对应标签列表;此商品标签候选项
    *
    * @param callback
    * @param id 精选id
    * @param request
    * @param out
    */
   @RequestMapping(value = "getGoodsInfo")
   public void getGoodsInfo(String callback, Long id, HttpServletRequest request, PrintWriter out) {
      try {
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            return;
         }
         TaoBaoGoodsBrief taobao = resultObj.getTaoBaoGoodsBrief();
         if (taobao == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在")));
            return;
         }
         // 商品图片
         List<String> imgList = TaoBaoUtil.getTbImg(taobao.getAuctionId() + "");
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls(); // 重点
         Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         // data.put("taobao", gson.toJson(taobao));
         data.put("imgList", gson.toJson(imgList));
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 单个商品贴上标签
    *
    * @param callback
    * @param id
    * @param labId
    * @param request
    * @param out
    */
   @RequestMapping(value = "addGoodsLable")
   public void addGoodsLable(String callback, Long id, Long labId, HttpServletRequest request, PrintWriter out) {
      try {
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
//         if (admin == null) {
//            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("当前账户失效,请重新登陆。")));
//            return;
//         }
         if (id == null || labId == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确的参数")));
            return;
         }
         TaoBaoGoodsBrief taoBaoGoodsBrief = taoBaoGoodsBriefService.selectByPrimaryKey(id);
         if (taoBaoGoodsBrief == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不存在")));
            return;
         }
         Label label = labelService.selectByPrimaryKey(labId);
         if (label == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签已不存在")));
            return;
         }
         /* 贴上标签 : 已贴不处理则 */
         Long existence = labelGoodsService.isExistence(id, labId);
         // 该商品不存在此标签则添加, 反之不做处理
         if (existence == null || existence == 0l) {
            Date nowTime = new Date();
            // 贴上新标签
            LabelGoods lg = new LabelGoods();
            lg.setLabel(label);
            lg.setCreateUser(admin);
            lg.setCreatetime(nowTime);
            lg.setTaoBaoGoodsBrief(taoBaoGoodsBrief);
            int insert = labelGoodsService.insertSelective(lg);
            if (insert > 0) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("添加成功")));
            } else {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("添加失败")));
            }
         } else {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("此商品已存在该标签")));
         }
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 删除商品关联的标签
    *
    * @param callback
    * @param rid
    * @param request
    * @param out
    */
   @RequestMapping(value = "removeSingle")
   public void removeSingle(String callback, Long rid, HttpServletRequest request, PrintWriter out) {
      try {
         int deleteId = labelGoodsService.deleteByPrimaryKey(rid);
         if (deleteId > 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功")));
         } else {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("删除失败")));
         }
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 设置商品权重、等信息
    *
    * @param callback
    * @param label
    * @param request
    * @param out
    */
   @RequestMapping(value = "saveInfo")
   public void saveInfo(String callback, QualityFactory selectionGoods, HttpServletRequest request, PrintWriter out) {
      try {
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
//
//         if (admin == null) {
//            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("当前账户已失效,请重新登陆")));
//            return;
//         }
         Long id = selectionGoods.getId();
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
         } else {
            Date nowTime = new Date();
            selectionGoods.setUpdatetime(nowTime);
            selectionGoods.setUpdateUser(admin);
            // 选择性更新数据
            int result = qualityFactoryService.updateByPrimaryKeySelective(selectionGoods);
            if (result > 0) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
            } else {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
            }
         }
      } catch (QualityFactoryException e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   @RequestMapping(value = "queryMyLabels")
   public void queryMyLabels(String callback, Integer pageIndex, Integer pageSize,
         String ids, HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(ids)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:101")));
            return;
         }
         Gson gson = new Gson();
         List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {}.getType());
         if (idList == null || idList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:102")));
            return;
         }
         if (idList.size() > 1) {
            // 只处理单个商品
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:103")));
            return;
         }
         Long id = idList.get(0);
         if (id == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:104")));
            return;
         }
         if (pageIndex == null)
            pageIndex = 1;
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         // 商品标签列表
         List<LabelGoods> labelList = labelGoodsService.queryByGoodsId((pageIndex - 1) * pageSize,
               pageSize, id);
         if (labelList == null || labelList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无标签"));
            return;
         }
         int count = labelGoodsService.getCountQueryByGoodsId(id);
         int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls();
         Gson rgson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("labelList", rgson.toJson(labelList));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 查询商品添加标签候选项
    *
    * @param callback
    * @param pageIndex
    * @param pageSize
    * @param key
    * @param id
    * @param request
    * @param out
    */
   @RequestMapping(value = "queryLabel")
   public void queryLabel(String callback, Integer pageIndex, Integer pageSize, String key,
         String ids, HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(ids)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:101")));
            return;
         }
         Gson gson = new Gson();
         List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {}.getType());
         if (idList == null || idList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:102")));
            return;
         }
         int count = 0;
         List<Label> labelList = null;
         if (pageIndex == null)
            pageIndex = 1;
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         if (idList.size() > 1) {
            // 多个商品 查询所有标签
             labelList = labelService.query((pageIndex - 1) * pageSize, pageSize, key, null, null,null);
             count = labelService.getQueryCount(key, null, null);
         } else {
            // 单个商品
            Long id = idList.get(0);
            if (id == null) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:104")));
               return;
            }
            labelList = labelService.queryGoodsCandidate((pageIndex - 1) * pageSize, pageSize, key, id);
            count = labelService.getCountQueryGoodsCandidate(key, id);
         }
         if (labelList == null || labelList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无标签"));
            return;
         }
         int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls(); // 重点
         Gson rgson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("labelList", rgson.toJson(labelList));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 单个商品批量添加标签-关联lableId
    *
    * @param callback
    * @param id
    * @param type
    * @param request
    * @param out
    */
   @RequestMapping(value = "addBatchcGoodsLabel")
   public void addBatchcGoodsLabel(String callback, String labIds, Long id, HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(labIds)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未选择标签"));
            return;
         }
         if (id == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未选择商品"));
            return;
         }
         Gson gson = new Gson();
         List<String> labIdList = gson.fromJson(labIds, new TypeToken<ArrayList<String>>() {
         }.getType());
         if (labIdList == null || labIdList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择标签")));
            return;
         }
         TaoBaoGoodsBrief taoBaoGoodsBrief = taoBaoGoodsBriefService.selectByPrimaryKey(id);
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         labelGoodsService.addBatchByLabId(taoBaoGoodsBrief, labIdList, admin);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("操作成功"));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   @RequestMapping(value="updateGoodsBrief")
   public void updateGoodsBrief(String callback, TaoBaoGoodsBrief tb, PrintWriter out){
      try {
         // 实际未精选ID
         Long id = tb.getId();
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            return;
         }
         TaoBaoGoodsBrief taobao = resultObj.getTaoBaoGoodsBrief();
         if (taobao == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在")));
            return;
         }
         String title = tb.getTitle();
         String pictUrl = tb.getPictUrl();
         if (!StringUtil.isNullOrEmpty(title))
            taobao.setTitle(title);
         if (!StringUtil.isNullOrEmpty(pictUrl))
            taobao.setPictUrl(pictUrl);
         // 更新商品
         taoBaoGoodsUpdateService.getUpdateTaoBaoGoodsBrief(taobao);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
      }
   }
   /**
    * 批量添加标签 --- 精品库(随机权重)
    * @param callback
    * @param request
    * @param out @param         权重范围 大值
    * @param         佣金范围 小值
    */
   @RequestMapping(value = "setWeightBatch")
   public void setWeightBatch(String callback,String ids, Integer weight,Integer weightSmall,
         Integer weightLarge, HttpServletRequest request, PrintWriter out) {
      try {
          if (StringUtil.isNullOrEmpty(ids)) {
             out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未选择需操作的商品")));
             return;
          }
          Gson gson = new Gson();
          List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {}.getType());
          if (idList == null || idList.size() == 0) {
             out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择需操作的商品")));
             return;
          }
            // 获取当前操作用户
          AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
          qualityFactoryService.setWeightBatch(idList, admin, weight, weightSmall, weightLarge);
          out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("设置成功")));
      } catch ( Exception e) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
         e.printStackTrace();
      }
   }
}
package com.yeshi.fanli.controller.admin;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.yeshi.fanli.entity.accept.AdminAcceptData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.bus.lable.BoutiqueAutoRule;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.quality.QualityFactoryException;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.LabelGoodsService;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.common.entity.PageEntity;
import com.yeshi.fanli.util.Constant;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.quality.QualityFactoryVO;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("admin/new/api/v1/qualityFactory")
public class QualityFactoryAdminController {
   @Resource
   private RedisManager redisManager;
   @Resource
   private LabelService labelService;
   @Resource
   private LabelGoodsService labelGoodsService;
   @Resource
   private QualityFactoryService qualityFactoryService;
   @Resource
   private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
   @Resource
   private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
   public static final int SALES_ASC = 1;
   public static final int SALES_DESC = 2;
   public static final int PRICE_ASC = 3;
   public static final int PRICE_DESC = 4;
   public static final int TKRATE_ASC = 5;
   public static final int TKRATE_DESC = 6;
   public static final int PROPOR_ASC = 7;
   public static final int PROPOR_DESC = 8;
   /**
    * 保存修改精品库商品关联信息
    *
    * @param callback
    * @param label
    * @param request
    * @param out
    */
   @RequestMapping(value = "saveModify")
   public void saveModify(AdminAcceptData acceptData, String callback, QualityFactory selectionGoods, HttpServletRequest request,
                     PrintWriter out) {
      try {
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         if (admin == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("当前账户已失效,请重新登陆")));
         } else {
            Long id = selectionGoods.getId();
            QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
            if (resultObj == null) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            } else {
               Date nowTime = new Date();
               selectionGoods.setUpdatetime(nowTime);
               selectionGoods.setUpdateUser(admin);
               // 选择性更新数据
               int result = qualityFactoryService.updateByPrimaryKeySelective(selectionGoods);
               if (result > 0) {
                  out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
               } else {
                  out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
               }
            }
         }
      } catch (QualityFactoryException e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 统计商品数量
    *
    * @param callback
    * @param out
    */
   @RequestMapping(value = "getCountAll")
   public void getCountAll(AdminAcceptData acceptData,String callback, PrintWriter out) {
      try {
         Map<String, Object> countMap = qualityFactoryService.getCountAll();
         JSONObject data = new JSONObject();
         data.put("countData", countMap);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
      }
   }
   /**
    * 批量移除商品至精品库外-不删除商品
    *
    * @param callback
    * @param ids
    *            商品id
    * @param out
    */
   @RequestMapping(value = "removeGoods")
   public void removeGoods(AdminAcceptData acceptData,String callback, String ids, PrintWriter out) {
      try {
         Gson gson = new Gson();
         List<Long> goodsIds = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (goodsIds == null || goodsIds.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检测到移除商品数据")));
            return;
         }
         qualityFactoryService.deleteBatchByTaoBaoGoodsId(goodsIds);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("移除成功")));
      } catch (Exception e) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作失败")));
         e.printStackTrace();
      }
   }
   /**
    * 查询
    *
    * @param out
    */
   @RequestMapping(value = "query")
   public void query(AdminAcceptData acceptData,String callback, Integer pageIndex, Integer pageSize, Integer sort,
         QualityFactoryVO qualityFactoryVO, PrintWriter out) {
      try {
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         if (pageIndex == null)
            pageIndex = 1;
         Integer price = null;
         Integer tkRate = null;
         Integer totalSales = null;
         Integer propor = null;
         if (sort != null) {
            switch (sort) {
            case SALES_ASC:
               totalSales = 1;
               break;
            case SALES_DESC:
               totalSales = 2;
               break;
            case PRICE_ASC:
               price = 1;
               break;
            case PRICE_DESC:
               price = 2;
               break;
            case TKRATE_ASC:
               tkRate = 1;
               break;
            case TKRATE_DESC:
               tkRate = 2;
               break;
            case PROPOR_ASC:
               propor = 1;
               break;
            case PROPOR_DESC:
               propor = 2;
               break;
            default:
               break;
            }
         }
         qualityFactoryVO.setPrice(price);
         qualityFactoryVO.setTkRate(tkRate);
         qualityFactoryVO.setTotalSales(totalSales);
         qualityFactoryVO.setPropor(propor);
         qualityFactoryVO.setStart((pageIndex - 1) * pageSize);
         qualityFactoryVO.setCount(pageSize);
         List<QualityFactory> selectionList = qualityFactoryService.query(qualityFactoryVO);
         /* 暂无数据显示 */
         if (selectionList == null || selectionList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
            return;
         }
         /* 有数据:处理返回格式 */
         long count = qualityFactoryService.queryCount(qualityFactoryVO);
         int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
         /* 验证网络: 获取商品链接 图片链接 */
         List<TaoBaoGoodsBrief> goodsBriefList = new ArrayList<TaoBaoGoodsBrief>();
         StringBuffer idBuf = new StringBuffer();
         StringBuffer auctionIdBuf = new StringBuffer();
         int j = 0;
         for (int i = 0; i < selectionList.size(); i++) {
            QualityFactory selectionGoods = selectionList.get(i);
            TaoBaoGoodsBrief taoBaoGoodsBrief = selectionGoods.getTaoBaoGoodsBrief();
            if (taoBaoGoodsBrief == null)
               continue; // 为空结束本次循环
            Long id = taoBaoGoodsBrief.getId();
            idBuf.append(id + ",");
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            if (auctionId != null) {
               j++;
               auctionIdBuf.append(auctionId + ",");
            }
            if (selectionList.size() <= 40) {
               // 小于40个参数
               if (i == selectionList.size() - 1) {
                  if (auctionIdBuf != null && auctionIdBuf.length() > 0) {
                     String auctionIds = auctionIdBuf.toString();
                     String ids = auctionIds.substring(0, auctionIds.length() - 1);
                     List<TaoBaoGoodsBrief> list = null;
                     try {
                        list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                     } catch (Exception e) {
                        e.printStackTrace();
                     }
                     if (list != null && list.size() > 0) {
                        goodsBriefList.addAll(list);
                     }
                     auctionIdBuf = new StringBuffer();
                  }
               }
            } else {
               if (j == 40) {
                  // 大于40参数
                  String auctionIds = auctionIdBuf.toString();
                  String ids = auctionIds.substring(0, auctionIds.length() - 1);
                  List<TaoBaoGoodsBrief> list = null;
                  try {
                     list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
                  if (list != null && list.size() > 0) {
                     goodsBriefList.addAll(list);
                  }
                  j = 0;
                  auctionIdBuf = new StringBuffer();
               } else if (i == selectionList.size() - 1) {
                  // 最后一个
                  String auctionIds = auctionIdBuf.toString();
                  String ids = auctionIds.substring(0, auctionIds.length() - 1);
                  List<TaoBaoGoodsBrief> list = null;
                  try {
                     list = TaoKeApiUtil.getBatchGoodsInfos(ids);
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
                  if (list != null && list.size() > 0) {
                     goodsBriefList.addAll(list);
                  }
                  auctionIdBuf = new StringBuffer();
               }
            }
         }
         for (QualityFactory selectionGoods : selectionList) {
            TaoBaoGoodsBrief taoBaoGoodsBrief = selectionGoods.getTaoBaoGoodsBrief();
            if (taoBaoGoodsBrief == null)
               continue; // 为空结束本次循环
            // 设置图片大小
            taoBaoGoodsBrief.setPictUrl(TbImgUtil.getTBSize220Img(taoBaoGoodsBrief.getPictUrl()));
            String auctionUrl = taoBaoGoodsBrief.getAuctionUrl();
            if (auctionUrl != null && auctionUrl.trim().length() > 0 && auctionUrl.contains("id=null")) {
               auctionUrl = auctionUrl.replace("id=null", "id=" + taoBaoGoodsBrief.getAuctionId());
               taoBaoGoodsBrief.setAuctionUrl(auctionUrl);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            BoutiqueAutoRule boutiqueAutoRule = selectionGoods.getBoutiqueAutoRule();
            if (boutiqueAutoRule == null) {
               boutiqueAutoRule = new BoutiqueAutoRule();
               selectionGoods.setBoutiqueAutoRule(boutiqueAutoRule);
            }
            String reslutSource = "";
            Integer gsSource = selectionGoods.getGoodsSource();
            if (QualityFactory.SOURCE_TAOBAO == gsSource) {
               reslutSource = "淘宝";
            } else if (QualityFactory.SOURCE_TAOBAO_MATERIAL == gsSource) {
               reslutSource = "淘宝推荐";
            } else if (QualityFactory.SOURCE_TAOBAO_DATAOKE == gsSource) {
               reslutSource = "大淘客";
            } else if (QualityFactory.SOURCE_JINGDONG == gsSource) {
               reslutSource = "京东";
            } else if (QualityFactory.SOURCE_PINDUODUO == gsSource) {
               reslutSource = "拼多多";
            } else if (QualityFactory.SOURCE_WEIPINHUI == gsSource) {
               reslutSource = "唯品会";
            } else if (QualityFactory.SOURCE_OTHER == gsSource) {
               reslutSource = "商务合作";
            }
            map.put("goodsSource", reslutSource);
            map.put("selectionGoods", selectionGoods);
            /* 商品销售状态: 0 在售 1停售 */
            int saleStae = 1;
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            if (auctionId != null) {
               if (goodsBriefList != null && goodsBriefList.size() > 0) {
                  for (TaoBaoGoodsBrief tb : goodsBriefList) {
                     Long auctionIdTB = tb.getAuctionId();
                     if (auctionId.equals(auctionIdTB)) {
                        saleStae = 0;
                     }
                  }
               }
            }
            map.put("saleStae", saleStae);
            /* 1 定向计划 2 营销返利 、高佣 3 普佣 */
            Integer includeDxjh = taoBaoGoodsBrief.getIncludeDxjh();
            String tkMktStatus = taoBaoGoodsBrief.getTkMktStatus();
            boolean include = false;
            if (includeDxjh != null) {
               if (includeDxjh == 1) {
                  // 定向计划
                  map.put("yongjinType", 1);
                  include = true;
               }
            }
            if (!include) {
               if ("1".equals(tkMktStatus)) {
                  // 营销返利 、高佣
                  map.put("yongjinType", 2);
               } else {
                  // 普佣
                  map.put("yongjinType", 3);
               }
            }
            /* 券后价--计算 */
            BigDecimal couponPrice = TaoBaoUtil.getCouponPrice(taoBaoGoodsBrief);
            map.put("couponPrice", couponPrice);
            /* 预计收益: 公司、用户 */
            BigDecimal tkRateBrief = taoBaoGoodsBrief.getTkRate();
            BigDecimal zkPrice = taoBaoGoodsBrief.getZkPrice();
            BigDecimal profit = MoneyBigDecimalUtil.mul(tkRateBrief, zkPrice);
            // 计算结果
            BigDecimal estimateProfit = MoneyBigDecimalUtil.div(profit, new BigDecimal("100"));
            // 预计收益
            map.put("estimateProfit", estimateProfit);
            BigDecimal couponAmount = taoBaoGoodsBrief.getCouponAmount();
            if (couponAmount != null && zkPrice != null) {
               BigDecimal propors = couponAmount.divide(zkPrice, 10, BigDecimal.ROUND_HALF_DOWN);
               BigDecimal resultPropor = MoneyBigDecimalUtil.mul(propors, new BigDecimal("100"));
               map.put("propor", resultPropor);
            } else {
               map.put("propor", 0);
            }
            /* 商品标签列表 */
            Long id = taoBaoGoodsBrief.getId();
            int totalLabels = labelGoodsService.getCountQueryByGoodsId(id);
            // List<LabelGoods> labelList = labelService.getByGoodsId(id);
            map.put("totalLabels", totalLabels);
            listmap.add(map);
         }
         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("listGoods", gson.toJson(listmap));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 获取商品信息 以及对应标签列表;此商品标签候选项
    *
    * @param callback
    * @param id
    *            精选id
    * @param request
    * @param out
    */
   @RequestMapping(value = "getGoodsInfo")
   public void getGoodsInfo(AdminAcceptData acceptData,String callback, Long id, HttpServletRequest request, PrintWriter out) {
      try {
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            return;
         }
         TaoBaoGoodsBrief taobao = resultObj.getTaoBaoGoodsBrief();
         if (taobao == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在")));
            return;
         }
         // 商品图片
         List<String> imgList = TaoBaoUtil.getTbImg(taobao.getAuctionId() + "");
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls(); // 重点
         Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         // data.put("taobao", gson.toJson(taobao));
         data.put("imgList", gson.toJson(imgList));
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 单个商品贴上标签
    *
    * @param callback
    * @param id
    * @param labId
    * @param request
    * @param out
    */
   @RequestMapping(value = "addGoodsLable")
   public void addGoodsLable(AdminAcceptData acceptData,String callback, Long id, Long labId, HttpServletRequest request, PrintWriter out) {
      try {
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         // if (admin == null) {
         // out.print(JsonUtil.loadJSONP(callback,
         // JsonUtil.loadFalseResult("当前账户失效,请重新登陆。")));
         // return;
         // }
         if (id == null || labId == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确的参数")));
            return;
         }
         TaoBaoGoodsBrief taoBaoGoodsBrief = taoBaoGoodsBriefService.selectByPrimaryKey(id);
         if (taoBaoGoodsBrief == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不存在")));
            return;
         }
         Label label = labelService.selectByPrimaryKey(labId);
         if (label == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签已不存在")));
            return;
         }
         /* 贴上标签 : 已贴不处理则 */
         Long existence = labelGoodsService.isExistence(id, labId);
         // 该商品不存在此标签则添加, 反之不做处理
         if (existence == null || existence == 0l) {
            Date nowTime = new Date();
            // 贴上新标签
            LabelGoods lg = new LabelGoods();
            lg.setLabel(label);
            lg.setCreateUser(admin);
            lg.setCreatetime(nowTime);
            lg.setTaoBaoGoodsBrief(taoBaoGoodsBrief);
            int insert = labelGoodsService.insertSelective(lg);
            if (insert > 0) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("添加成功")));
            } else {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("添加失败")));
            }
         } else {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("此商品已存在该标签")));
         }
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 删除商品关联的标签
    *
    * @param callback
    * @param rid
    * @param request
    * @param out
    */
   @RequestMapping(value = "removeSingle")
   public void removeSingle(AdminAcceptData acceptData,String callback, Long rid, HttpServletRequest request, PrintWriter out) {
      try {
         int deleteId = labelGoodsService.deleteByPrimaryKey(rid);
         if (deleteId > 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功")));
         } else {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("删除失败")));
         }
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   /**
    * 设置商品权重、等信息
    *
    * @param callback
    * @param label
    * @param request
    * @param out
    */
   @RequestMapping(value = "saveInfo")
   public void saveInfo(AdminAcceptData acceptData,String callback, QualityFactory selectionGoods, HttpServletRequest request, PrintWriter out) {
      try {
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         //
         // if (admin == null) {
         // out.print(JsonUtil.loadJSONP(callback,
         // JsonUtil.loadFalseResult("当前账户已失效,请重新登陆")));
         // return;
         // }
         Long id = selectionGoods.getId();
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
         } else {
            Date nowTime = new Date();
            selectionGoods.setUpdatetime(nowTime);
            selectionGoods.setUpdateUser(admin);
            // 选择性更新数据
            int result = qualityFactoryService.updateByPrimaryKeySelective(selectionGoods);
            if (result > 0) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
            } else {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
            }
         }
      } catch (QualityFactoryException e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
      }
   }
   @RequestMapping(value = "queryMyLabels")
   public void queryMyLabels(AdminAcceptData acceptData,String callback, Integer pageIndex, Integer pageSize, String ids,
         HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(ids)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:101")));
            return;
         }
         Gson gson = new Gson();
         List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (idList == null || idList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:102")));
            return;
         }
         if (idList.size() > 1) {
            // 只处理单个商品
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:103")));
            return;
         }
         Long id = idList.get(0);
         if (id == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:104")));
            return;
         }
         if (pageIndex == null)
            pageIndex = 1;
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         // 商品标签列表
         List<LabelGoods> labelList = labelGoodsService.queryByGoodsId((pageIndex - 1) * pageSize, pageSize, id);
         if (labelList == null || labelList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无标签"));
            return;
         }
         int count = labelGoodsService.getCountQueryByGoodsId(id);
         int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls();
         Gson rgson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("labelList", rgson.toJson(labelList));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 查询商品添加标签候选项
    *
    * @param callback
    * @param pageIndex
    * @param pageSize
    * @param key
    * @param id
    * @param request
    * @param out
    */
   @RequestMapping(value = "queryLabel")
   public void queryLabel(AdminAcceptData acceptData,String callback, Integer pageIndex, Integer pageSize, String key, String ids,
         HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(ids)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:101")));
            return;
         }
         Gson gson = new Gson();
         List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (idList == null || idList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:102")));
            return;
         }
         int count = 0;
         List<Label> labelList = null;
         if (pageIndex == null)
            pageIndex = 1;
         if (pageSize == null)
            pageSize = Constant.PAGE_SIZE;
         if (idList.size() > 1) {
            // 多个商品 查询所有标签
            labelList = labelService.query((pageIndex - 1) * pageSize, pageSize, key, null, null, null);
            count = labelService.getQueryCount(key, null, null);
         } else {
            // 单个商品
            Long id = idList.get(0);
            if (id == null) {
               out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请传递正确参数:104")));
               return;
            }
            labelList = labelService.queryGoodsCandidate((pageIndex - 1) * pageSize, pageSize, key, id);
            count = labelService.getCountQueryGoodsCandidate(key, id);
         }
         if (labelList == null || labelList.size() == 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无标签"));
            return;
         }
         int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
         PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
         GsonBuilder gsonBuilder = new GsonBuilder();
         gsonBuilder.serializeNulls(); // 重点
         Gson rgson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
         JSONObject data = new JSONObject();
         data.put("pe", pe);
         data.put("labelList", rgson.toJson(labelList));
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   /**
    * 单个商品批量添加标签-关联lableId
    *
    * @param callback
    * @param id
    * @param type
    * @param request
    * @param out
    */
   @RequestMapping(value = "addBatchcGoodsLabel")
   public void addBatchcGoodsLabel(AdminAcceptData acceptData,String callback, String labIds, Long id, HttpServletRequest request,
         PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(labIds)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未选择标签"));
            return;
         }
         if (id == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未选择商品"));
            return;
         }
         Gson gson = new Gson();
         List<String> labIdList = gson.fromJson(labIds, new TypeToken<ArrayList<String>>() {
         }.getType());
         if (labIdList == null || labIdList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择标签")));
            return;
         }
         TaoBaoGoodsBrief taoBaoGoodsBrief = taoBaoGoodsBriefService.selectByPrimaryKey(id);
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         labelGoodsService.addBatchByLabId(taoBaoGoodsBrief, labIdList, admin);
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("操作成功"));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
         e.printStackTrace();
      }
   }
   @RequestMapping(value = "updateGoodsBrief")
   public void updateGoodsBrief(AdminAcceptData acceptData,String callback, TaoBaoGoodsBrief tb, PrintWriter out) {
      try {
         // 实际未精选ID
         Long id = tb.getId();
         QualityFactory resultObj = qualityFactoryService.selectByPrimaryKey(id);
         if (resultObj == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在精品库中")));
            return;
         }
         TaoBaoGoodsBrief taobao = resultObj.getTaoBaoGoodsBrief();
         if (taobao == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该商品已不在")));
            return;
         }
         String title = tb.getTitle();
         String pictUrl = tb.getPictUrl();
         if (!StringUtil.isNullOrEmpty(title))
            taobao.setTitle(title);
         if (!StringUtil.isNullOrEmpty(pictUrl))
            taobao.setPictUrl(pictUrl);
         if (!StringUtil.isNullOrEmpty(tb.getPictUrlWhite()))
            taobao.setPictUrlWhite(tb.getPictUrlWhite());
         // 更新商品
         taoBaoGoodsUpdateService.getUpdateTaoBaoGoodsBrief(taobao);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
      }
   }
   /**
    * 批量添加标签 --- 精品库(随机权重)
    *
    * @param callback
    * @param request
    * @param out
    * @param 权重范围
    *            大值
    * @param 佣金范围
    *            小值
    */
   @RequestMapping(value = "setWeightBatch")
   public void setWeightBatch(AdminAcceptData acceptData,String callback, String ids, Integer weight, Integer weightSmall, Integer weightLarge,
         HttpServletRequest request, PrintWriter out) {
      try {
         if (StringUtil.isNullOrEmpty(ids)) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未选择需操作的商品")));
            return;
         }
         Gson gson = new Gson();
         List<Long> idList = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {
         }.getType());
         if (idList == null || idList.size() == 0) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择需操作的商品")));
            return;
         }
         // 获取当前操作用户
         AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
         qualityFactoryService.setWeightBatch(idList, admin, weight, weightSmall, weightLarge);
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("设置成功")));
      } catch (Exception e) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
         e.printStackTrace();
      }
   }
}