package com.yeshi.fanli.controller.client.v2;
|
|
import java.io.PrintWriter;
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Set;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
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.accept.AcceptData;
|
import com.yeshi.fanli.entity.bus.user.UserGoodsStorage;
|
import com.yeshi.fanli.entity.goods.CommonGoods;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.exception.share.UserShareGoodsRecordException;
|
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
|
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
|
import com.yeshi.fanli.exception.user.UserGoodsStorageException;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
|
import com.yeshi.fanli.service.inter.user.UserGoodsStorageService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
|
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
/**
|
* 用户商品分享库
|
*
|
* @author Administrator
|
*
|
*/
|
@Controller
|
@RequestMapping("api/v2/shraeStorage")
|
public class ShareStorageControllerV2 {
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private HongBaoManageService hongBaoManageService;
|
|
@Resource
|
private UserGoodsStorageService userGoodsStorageService;
|
|
|
|
/**
|
* 单个商品加入选品库
|
*
|
* @param callback
|
* @param storageIds
|
* id数组
|
* @param out
|
*/
|
@RequestMapping(value = "accordtorage", method = RequestMethod.POST)
|
public void accordtorage(AcceptData acceptData, Long uid, Long id, Integer goodsType, PrintWriter out) {
|
|
if (id == null) {
|
out.print(JsonUtil.loadFalseResult("未选择商品"));
|
return;
|
}
|
|
if (uid == null) {
|
out.print(JsonUtil.loadFalseResult("用户未登录"));
|
return;
|
}
|
|
if (goodsType == null) {
|
goodsType = Constant.SOURCE_TYPE_TAOBAO;
|
}
|
|
try {
|
UserGoodsStorage userGoodsStorage = userGoodsStorageService.getByUidAndAuctionId(uid, id, goodsType);
|
boolean storageState = false;
|
if (userGoodsStorage != null) {
|
// 取消加入选品库
|
userGoodsStorageService.deleteByPrimaryKey(userGoodsStorage.getId());
|
} else {
|
// 加入选品库
|
Set<Long> set = new HashSet<Long>();
|
set.add(id);
|
userGoodsStorageService.save(uid, set, goodsType);
|
storageState = true;
|
}
|
|
JSONObject data = new JSONObject();
|
data.put("storageState", storageState);
|
out.print(JsonUtil.loadTrueResult(data));
|
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseResult("操作失败"));
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 批量添加选品库
|
*
|
* @param acceptData
|
* @param uid 用户id
|
* @param ids 简版商品id
|
* @param out
|
*/
|
@RequestMapping(value = "addStorage", method = RequestMethod.POST)
|
public void addStorage(AcceptData acceptData, Long uid, String ids, PrintWriter out) {
|
try {
|
Gson gson = new Gson();
|
Set<Long> set = gson.fromJson(ids, new TypeToken<HashSet<Long>>() {
|
}.getType());
|
if (set == null || set.size() == 0) {
|
out.print(JsonUtil.loadFalseResult("未选择商品"));
|
return;
|
}
|
|
userGoodsStorageService.addCommonGoods(uid, set);
|
out.print(JsonUtil.loadTrueResult("添加成功"));
|
} catch (UserGoodsStorageException e) {
|
out.print(JsonUtil.loadFalseResult(e.getMsg()));
|
}
|
}
|
|
/**
|
* 查询用户选品库数据
|
*
|
* @param acceptData
|
* @param page 页码 初始值 1
|
* @param uid 用户id
|
* @param out
|
*/
|
@RequestMapping(value = "getlist", method = RequestMethod.POST)
|
public void getlist(AcceptData acceptData, Integer page, Long uid, Integer goodsType, PrintWriter out) {
|
|
if (uid == null) {
|
out.print(JsonUtil.loadFalseResult("用户未登录"));
|
return;
|
}
|
|
if (goodsType == null) {
|
out.print(JsonUtil.loadFalseResult("平台类型不能为空"));
|
return;
|
}
|
|
if (page == null || page < 1) {
|
page = 1;
|
}
|
|
int pageSize = Constant.PAGE_SIZE;
|
JSONArray array = new JSONArray();
|
JSONObject data = new JSONObject();
|
|
if (goodsType == Constant.SOURCE_TYPE_JD) {
|
String open = configService.get("share_jd_open");
|
if (!"1".equals(open.trim())) {
|
data.put("count", 0);
|
data.put("result_list", array);
|
out.print(JsonUtil.loadTrueResult(data));
|
return;
|
}
|
}
|
|
List<UserGoodsStorage> listStorage = userGoodsStorageService.listQueryByUid((page - 1) * pageSize, pageSize,
|
uid, goodsType);
|
if (listStorage == null || listStorage.size() == 0) {
|
data.put("count", 0);
|
data.put("result_list", array);
|
out.print(JsonUtil.loadTrueResult(data));
|
return;
|
}
|
|
// API网络接口验证是否在售
|
List<TaoBaoGoodsBrief> listTaoKeGoods = null;
|
if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
|
List<Long> listGid = new ArrayList<Long>();
|
for (UserGoodsStorage userGoodsStorage : listStorage) {
|
CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
|
if (commonGoods == null) {
|
continue;
|
}
|
listGid.add(commonGoods.getGoodsId());
|
}
|
|
try {
|
listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
|
} catch (TaoKeApiException e) {
|
e.printStackTrace();
|
} catch (TaobaoGoodsDownException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
|
.excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
|
|
BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
|
BigDecimal shareRate = hongBaoManageService.getShareRate();
|
|
for (UserGoodsStorage userGoodsStorage : listStorage) {
|
CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
|
if (commonGoods == null) {
|
continue;
|
}
|
|
// 淘宝商品验证在售
|
if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
|
if (listTaoKeGoods != null && listTaoKeGoods.size() > 0) {
|
int state = 1; // 默认停售
|
Long goodsId = commonGoods.getGoodsId();
|
for (TaoBaoGoodsBrief taoKeGoods : listTaoKeGoods) {
|
Long auctionId = taoKeGoods.getAuctionId();
|
if (goodsId == auctionId || goodsId.equals(auctionId)) {
|
state = 0; // 在售
|
break;
|
}
|
}
|
commonGoods.setState(state);
|
}
|
}
|
|
// 判断是否已分享, 已分享显示已下架
|
Integer storageState = userGoodsStorage.getState();
|
if (storageState != null && storageState == UserGoodsStorage.STATE_SHARED) {
|
Integer goodsState = commonGoods.getState();
|
if (goodsState != null && goodsState != 1) {
|
commonGoods.setState(2);
|
}
|
}
|
|
GoodsDetailVO detailVO = GoodsDetailVOFactory.convertCommonGoods(commonGoods, null, fanLiRate, shareRate);
|
detailVO.setId(commonGoods.getId());
|
|
JSONObject dataObject = new JSONObject();
|
dataObject.put("storageId", userGoodsStorage.getId());
|
dataObject.put("goods", gson.toJson(detailVO));
|
array.add(dataObject);
|
}
|
|
long count = userGoodsStorageService.countQueryByUid(uid, goodsType);
|
data.put("count", count);
|
data.put("result_list", array);
|
out.print(JsonUtil.loadTrueResult(data));
|
}
|
|
|
/**
|
* 分享商品返回二维码图片
|
*
|
* @param callback
|
* @param storageIds
|
* id数组
|
* @param out
|
*/
|
@RequestMapping(value = "createShare", method = RequestMethod.POST)
|
public void createShare(AcceptData acceptData, Long uid, String storageIds, PrintWriter out) {
|
|
if (StringUtil.isNullOrEmpty(storageIds)) {
|
out.print(JsonUtil.loadFalseResult("分享商品不能为空"));
|
return;
|
}
|
|
if (uid == null) {
|
out.print(JsonUtil.loadFalseResult("用户未登录"));
|
return;
|
}
|
|
try {
|
Gson gson = new Gson();
|
List<Long> listStorageID = gson.fromJson(storageIds, new TypeToken<ArrayList<Long>>() {}.getType());
|
if (listStorageID == null || listStorageID.size() < 9) {
|
out.print(JsonUtil.loadFalseResult("分享商品数量不足"));
|
return;
|
}
|
|
if (listStorageID.size() != 9) {
|
out.print(JsonUtil.loadFalseResult("分享商品数量只能是9个"));
|
return;
|
}
|
|
JSONObject data = userGoodsStorageService.createShareV2(uid, listStorageID);
|
out.print(JsonUtil.loadTrueResult(data));
|
} catch (UserGoodsStorageException e) {
|
out.print(JsonUtil.loadFalseResult("分享失败"));
|
e.printStackTrace();
|
} catch (UserShareGoodsRecordException e) {
|
out.print(JsonUtil.loadFalseResult("分享失败"));
|
e.printStackTrace();
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseResult("分享失败"));
|
e.printStackTrace();
|
}
|
}
|
}
|