package com.sif.action.service.impl;
import com.sif.action.SSL.TencentSmsSender;
import com.sif.action.constants.Constants;
import com.sif.action.pojo.GoodDetail;
import com.sif.action.entity.*;
import com.sif.action.mapper.*;
import com.sif.action.pojo.History;
import com.sif.action.pojo.OtherDetail;
import com.sif.action.repository.BiddingTbRepository;
import com.sif.action.repository.CommodityTbRepository;
import com.sif.action.result.*;
import com.sif.action.service.CommodityTbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sif.action.service.WebSocketService;
import com.sif.common.util.EntityUtils;
import com.sif.common.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <p>
* 服务实现类
* </p>
*
* @author Sif
* @since 2019-10-26
*/
@Service
public class CommodityTbServiceImpl extends ServiceImpl<CommodityTbMapper, CommodityTb> implements CommodityTbService {
@Autowired
private IdWorker idWorker;
@Autowired
private UserCommodityTbMapper userCommodityTbMapper;
@Autowired
private CommodityTbMapper commodityTbMapper;
@Autowired
private UserTbMapper userTbMapper;
@Autowired
private BiddingTbMapper biddingTbMapper;
@Autowired
private UserCreditTbMapper userCreditTbMapper;
@Autowired
CommodityTbRepository commodityTbRepository;
@Autowired
BiddingTbRepository biddingTbRepository;
@Autowired
private FavoriteCommodityTbMapper favoriteCommodityTbMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private LogisticsTbMapper logisticsTbMapper;
@Autowired
private WebSocketService webSocketService;
@Autowired
private OrderTbMapper orderTbMapper;
@Autowired
private PackageBillsTbMapper packageBillsTbMapper;
@Autowired
private CommodityCommentTbMapper commodityCommentTbMapper;
/**
* @Description: 发布商品
* @Param: [shopForm, uid]
* @return: void
* @Author: shenyini
* @Date: 2020/1/1
*/
@Transactional
@Override
public void addCommodity(ShopForm shopForm, String uid) {
System.err.println(shopForm.getPart_time());
String beginDate = ((List<String>) shopForm.getPart_time()).get(0).replace("Z", " UTC");
String endDate = ((List<String>) shopForm.getPart_time()).get(1).replace("Z", " UTC");
SimpleDateFormat df_1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
SimpleDateFormat df_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("beginDate:" + beginDate);
System.out.println("endDate" + endDate);
try {
String cid = idWorker.nextId()+"";
CommodityTb commodityTb = new CommodityTb();
commodityTb.setCid(cid);
commodityTb.setCname(shopForm.getCname());
commodityTb.setPrice(shopForm.getPrice());
commodityTb.setAddprice(shopForm.getAddprice());
commodityTb.setNowprice(shopForm.getPrice());
commodityTb.setImage(shopForm.getImage());
commodityTb.setImages(shopForm.getImages());
commodityTb.setDes(shopForm.getDesc());
commodityTb.setTypeId(shopForm.getType_id());
commodityTb.setBrandName(shopForm.getBrand_name());
commodityTb.setCreateTime(new Date());
commodityTb.setUpdateTime(new Date());
commodityTb.setBeginTime(df_2.parse(df_2.format(df_1.parse(beginDate))));
commodityTb.setEndTime(df_2.parse(df_2.format(df_1.parse(endDate))));
// commodityTb.setStatu(0);
baseMapper.insert(commodityTb);
//user_commodity_tb 关系表
UserCommodityTb userCommodityTb = new UserCommodityTb();
userCommodityTb.setCid(cid);
userCommodityTb.setUid(uid);
userCommodityTbMapper.insert(userCommodityTb);
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* @Description: 获取商品详情
* @Param: [cid]
* @return: com.sif.action.result.GoodDetailResult
* @Author: shenyini
* @Date: 2020/1/1
*/
@Override
public GoodDetailResult getGoodDetail(String cid) {
CommodityTb commodityTb = commodityTbMapper.selectById(cid);
System.out.println(commodityTb);
GoodDetailResult goodDetailResult = new GoodDetailResult();
/*cid*/
goodDetailResult.setCid(commodityTb.getCid());
/*price*/
goodDetailResult.setPrice(commodityTb.getPrice());
/*addprice*/
goodDetailResult.setAddprice(commodityTb.getAddprice());
/*nowprice*/
goodDetailResult.setNowprice(commodityTb.getNowprice());
/*nowprice 如果有竞购,取最近一次竞购的价格,否则取原价*/
// List<com.sif.action.pojo.BiddingTb> biddingList = biddingTbRepository.findBiddingRecord(cid);
// if(biddingList.size() == 0){
// System.err.println("未拍卖过");
// System.err.println(commodityTb.getPrice());
// goodDetailResult.setNowprice(commodityTb.getPrice());
// }else{
// System.err.println(biddingList.get(0).getBidprice());
// goodDetailResult.setNowprice(biddingList.get(0).getBidprice());
// }
/*images*/
String[] images = commodityTb.getImages().split("_");
for(int i= 0; i < images.length; i++){
if(images[i] != "" && !"http".equals(images[i].substring(0,4)))
images[i] = Constants.IMAGE_PATH +images[i];
}
goodDetailResult.setImages(images);
/*cname*/
goodDetailResult.setCname(commodityTb.getCname());
/*typeid*/
String[] typeid = commodityTb.getTypeId().split("_");
goodDetailResult.setTypeid(typeid);
/*seller*/
UserTb user = userTbMapper.selectById(this.selectSeller(cid));
goodDetailResult.setSeller(user.getName());
/*count*/
Map map = new HashMap();
map.put("cid",cid);
List<BiddingTb> list = biddingTbMapper.selectByMap(map);
goodDetailResult.setCount(list.size());
/*score*/
Map map2 = new HashMap();
map2.put("uid", user.getUid());
UserCreditTb userCreditTb = (UserCreditTb) userCreditTbMapper.selectByMap(map2).get(0);
goodDetailResult.setScore(userCreditTb.getScore());
// History history = new History();
// List<History> histories = EntityUtils.castEntity(commodityTbRepository.findGoodList(cid) , History.class, history);
// goodDetailResult.setHistory(histories);
/*des*/
goodDetailResult.setDes(commodityTb.getDes());
/*param1*/
List<OtherDetail> param1 = new ArrayList<>();
OtherDetail o1 = new OtherDetail("商品件数","1");
OtherDetail o2 = new OtherDetail("邮费","买家承担");
OtherDetail o3 = new OtherDetail("可否退货","不可以");
param1.add(o1);
param1.add(o2);
param1.add(o3);
goodDetailResult.setParam1(param1);
/*param2*/
List<OtherDetail> param2 = new ArrayList<>();
// 2019-11-24 16:35:35
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
OtherDetail o4 = new OtherDetail("开始时间",dateFormat.format(commodityTb.getBeginTime()));
OtherDetail o5 = new OtherDetail("结束时间",dateFormat.format(commodityTb.getEndTime()));
OtherDetail o6