package cn.zqh.brand.service.impl;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.*;
import cn.zqh.brand.client.CommodityClient;
import cn.zqh.brand.client.StoreClient;
import cn.zqh.brand.client.UserClient;
import cn.zqh.brand.config.BSException;
import cn.zqh.brand.holder.LoginUserHolder;
import cn.zqh.brand.mapper.BrandMapper;
import cn.zqh.brand.mapper.OrderMapper;
import cn.zqh.brand.mapper.ShopCarMapper;
import cn.zqh.brand.service.OrderService;
import cn.zqh.uuid.utils.SnowflakeId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author zqh
* @since 2023-03-01
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Dydj> implements OrderService {
@Resource
private OrderMapper orderMapper;
@Autowired
private CommodityClient commodityClient;
@Autowired
private UserClient userClient;
@Autowired
private LoginUserHolder loginUserHolder;
@Resource
private BrandMapper brandMapper;
@Resource
private ShopCarMapper shopCarMapper;
@Autowired
private StoreClient storeClient;
/**
* @Author: zqh
* @Date:
* @Description: 商品购买(添加订单)
*/
@Override
public CommonResult buyACommodity(String commodityId, Integer number) {
UserDTO currentUser = loginUserHolder.getCurrentUser();
if (currentUser == null) {
throw new BSException(ResultCode.UNAUTHORIZED.getCode(),ResultCode.UNAUTHORIZED.getMessage());
}
//查询购买商品信息
Commodity commodity = commodityClient.gCommodity(commodityId);
//查询购买者信息
User user = userClient.getUserById(currentUser.getId());
//看用户钱是否足够
if (user.getHavaMoney() < commodity.getPrice() * number){
return CommonResult.failed("余额不足");
}
//看购买量是否大于库存量
if (number > commodity.getNumber()){
return CommonResult.failed(" 库存不足");
}
//购买者钱减少
AdminChangeUserInfo adminChangeUserInfo = new AdminChangeUserInfo();
adminChangeUserInfo.setId(user.getUserId())
.setEmail(user.getQqEmail())
.setName(user.getNickName())
.setSex(user.getSex())
.setAvatar(user.getAvatar())
.setIntro(user.getIntro())
.setAddress(user.getAddress())
.setIsStore(user.getIsStore())
.setMoney(user.getHavaMoney() - (commodity.getPrice() * number))
.setIsDelete(user.getIsDelete());
String objectCommonResult = userClient.adminChangeInfo(adminChangeUserInfo);
if (!objectCommonResult.equals("SUCCESS")) {
return CommonResult.failed();
}
//商品库存量减少
Brand brand = brandMapper.selectOne(new QueryWrapper<Brand>().eq("Brand_id", commodity.getBrandId()));
AdminChangeCommodity adminChangeCommodity = new AdminChangeCommodity();
adminChangeCommodity.setCommodityId(commodity.getCommodityId())
.setCommodityName(commodity.getCommodityName())
.setAvatar(commodity.getAvatar())
.setIntro(commodity.getIntro())
.setStyle(commodity.getStyle())
.setPrice(commodity.getPrice())
.setNumber(commodity.getNumber() - number)
.setSalesVolume(commodity.getSalesVolume())
.setBrandId(brand.getBrandId());
String commonResult1 = commodityClient.ModifyTheProductInformationBasedOnTheAdministrator(adminChangeCommodity);
System.out.println("555555555555555555555555555");
if (!"SUCCESS".equals(commonResult1)) {
return CommonResult.failed();
}
//增加一个订单
Commodity commodity1 = commodityClient.gCommodity(commodityId);
Dydj order = new Dydj();
order.setOrderId("order"+ SnowflakeId.nextId())
.setUserId(user.getUserId())
.setStoreId(commodity1.getStoreId())
.setCommodityId(commodityId)
.setNumber(number)
.setPrice(number * commodity.getPrice())
.setAddress(user.getAddress())
.setIsDelete(0)
.setStyle(1);
int i = orderMapper.insert(order);
if (i != 1){
return CommonResult.failed();
}
System.out.println("666666666666666");
//店主钱增加
Store store = storeClient.getStoreInformationByStoreId(commodity1.getStoreId());
User user1 = userClient.getUserById(store.getUserId());
AdminChangeUserInfo adminChangeUserInfo1 = new AdminChangeUserInfo();
adminChangeUserInfo1.setId(user1.getUserId())
.setEmail(user1.getQqEmail())
.setName(user1.getNickName())
.setSex(user1.getSex())
.setAvatar(user1.getAvatar())
.setIntro(user1.getIntro())
.setAddress(user1.getAddress())
.setIsStore(user1.getIsStore())
.setMoney(user1.getHavaMoney() + (commodity.getPrice() * number))
.setIsDelete(user1.getIsDelete());
String commonResult2 = userClient.adminChangeInfo(adminChangeUserInfo1);
if (!"SUCCESS".equals(commonResult2)) {
return CommonResult.failed();
}
return CommonResult.success(ResultCode.SUCCESS);
}
/**
* @Author: zqh
* @Date:
* @Description: 把购物车里的商品提交订单
*/
@Override
public CommonResult submitTheorderForCart(String shopCarId) {
UserDTO currentUser = loginUserHolder.getCurrentUser();
User user = userClient.getUserById(currentUser.getId());
//删除购物车中信息
ShopCar shopCar = shopCarMapper.selectById(shopCarId);
EsCommodity commodity = commodityClient.getCommodityById(shopCar.getCommodityId());
Commodity commodity1 = commodityClient.gCommodity(shopCar.getCommodityId());
Dydj order = new Dydj();
order.setOrderId("order"+SnowflakeId.nextId())
.setUserId(user.getUserId())
.setStoreId(commodity1.getStoreId())
.setCommodityId(commodity.getCommodityId())
.setAddress(user.getAddress())
.setNumber(shopCar.getNumber())
.setPrice(shopCar.getMoney())
.setIsDelete(0)
.setStyle(1);
int i = orderMapper.insert(order);
if (i != 1){
return CommonResult.failed();
}
//店主钱增加
Store store = storeClient.getStoreInformationByStoreId(commodity1.getStoreId());
User user1 = userClient.getUserById(store.getUserId());
AdminChangeUserInfo adminChangeUserInfo1 = new AdminChangeUserInfo();
adminChangeUserInfo1.setId(user1.getUserId())
.setEmail(user1.getQqEmail())
.setName(user1.getNickName())
.setSex(user1.getSex())
.setAvatar(user1.getAvatar())
.setIntro(user1.getIntro())
.setAddress(user1.getAddress())
.setIsStore(user1.getIsStore())
.setMoney(user1.getHavaMoney() + shopCar.getMoney())
.setIsDelete(user1.getIsDelete());
String commonResult2 = userClient.ad