wzy
2021-11-30 0e5aaeba97021c289485a652ec6af0bb37f92af7
gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java
@@ -2,6 +2,8 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -24,6 +26,7 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
@Service
@@ -61,17 +64,55 @@
    private IdUtils idUtils;
    public Map<String, Object> queryOrderList(QueryOrderListDto model) {
        String name = model.getName();
        String accountName = model.getName();
        String name = StrUtil.isEmpty(model.getName())?model.getName():StringUtils.encode(model.getName());
        String orderNo = model.getOrderNo();
        Integer status = model.getStatus() == null ? 0 : model.getStatus();
        List<Integer> status = new ArrayList<>();
        if(CollUtil.isNotEmpty(model.getStatus())){
            status = model.getStatus();
        }
        if(CollUtil.isEmpty(model.getStatus())){
            status.add(1);
            status.add(2);
            status.add(3);
            status.add(4);
            status.add(5);
            status.add(6);
        }
        Date createdTimeStart = model.getCreatedTimeStart();
        Date createdTimeEnd = model.getCreatedTimeEnd();
        PageHelper.startPage(model.getPage(), model.getLimit());
        List<QueryOrderListVo> maps = scoreOrderMapper.queryOrderList(name,orderNo,status,createdTimeStart,createdTimeEnd);
        Integer namePx = model.getNamePx() == null ? 2 : model.getNamePx();
        Integer typePx = model.getTypePx() == null ? 2 : model.getTypePx();
        List<QueryOrderListVo> maps = scoreOrderMapper.queryOrderList(name,accountName,orderNo,status,createdTimeStart,createdTimeEnd,namePx,typePx);
        if(CollUtil.isNotEmpty(maps)){
            for(QueryOrderListVo queryOrderListVo : maps){
                String decode = StringUtils.decode(queryOrderListVo.getName());
                queryOrderListVo.setName(decode);
                BigDecimal totalPrice = new BigDecimal(StrUtil.isEmpty(queryOrderListVo.getTotalPrice()) ? "0" : queryOrderListVo.getTotalPrice())
                        .setScale( 2, BigDecimal.ROUND_DOWN );
                queryOrderListVo.setTotalPrice(totalPrice.toString());
                Long id = queryOrderListVo.getId();
                Example example = new Example(ScoreOrderDetails.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("orderId",id);
                List<ScoreOrderDetails> scoreOrderDetails = scoreOrderDetailsMapper.selectByExample(example);
                if(CollUtil.isNotEmpty(scoreOrderDetails)){
                    queryOrderListVo.setOrderDetails(scoreOrderDetails);
//                    StringBuffer stringBuffer = new StringBuffer();
//                    for(ScoreOrderDetails scoreOrderDetail : scoreOrderDetails){
//                        String goodsName = scoreOrderDetail.getGoodsName();
//                        BigDecimal unitPrice = scoreOrderDetail.getUnitPrice();
//                        String sku = scoreOrderDetail.getSku();
//                        Integer cnt = scoreOrderDetail.getCnt();
//                        BigDecimal totalPriceOrderDetail = scoreOrderDetail.getTotalPrice();
//                        stringBuffer = stringBuffer.append(goodsName+"(")
//                                .append("规格:"+sku+","+unitPrice.setScale( 2, BigDecimal.ROUND_DOWN ).toString()+"*"+cnt.toString()+",")
//                                .append("总价:"+totalPriceOrderDetail.setScale( 2, BigDecimal.ROUND_DOWN ).toString()+";)");
//                    }
//                    queryOrderListVo.setOrderDetails(stringBuffer.toString());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(maps);
@@ -150,9 +191,16 @@
    }
    public Long insureOrder(InsureOrderDto model) {
        long id = model.getId();
        ScoreOrder scoreOrderBefore = scoreOrderMapper.selectByPrimaryKey(id);
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setId(model.getId());
        scoreOrder.setStatus(ScoreOrder.STATUS_DONE);
        if(ScoreOrder.STATUS_DOING == scoreOrderBefore.getStatus()){
            scoreOrder.setStatus(ScoreOrder.STATUS_DONE);
        }
//        scoreOrder.setStatus(ScoreOrder.STATUS_DONE);
        scoreOrder.setIsFinish(ScoreOrder.ISFINISH_YES);
        scoreOrder.setIsFinishTime(new Date());
        scoreOrder.setVoucherImg(CollUtil.join(model.getVoucherImgs(),","));
        scoreOrderMapper.updateByPrimaryKeySelective(scoreOrder);
        return scoreOrder.getId();
@@ -166,23 +214,64 @@
            throw new RestException(-3, "商品不存在或已下架");
        }
        if (ScoreGoods.ISQG_YES.equals(goods.getIsQg())) {
            if (new Date().before(goods.getQgStartTime())) {
                throw new RestException(-3, "抢购未开始");
            }
//        if (ScoreGoods.ISQG_YES.equals(goods.getIsQg())) {
//            if (new Date().before(goods.getQgStartTime())) {
//                throw new RestException(-3, "抢购未开始");
//            }
//
//            if (new Date().after(goods.getQgEndTime())) {
//                throw new RestException(-3, "抢购已结束");
//            }
//        }
            if (new Date().after(goods.getQgEndTime())) {
                throw new RestException(-3, "抢购已结束");
            }
        if (addGoodsOrderDto.getCnt() > sku.getStock()) {
            throw new RestException(-3, "库存不足");
        }
        AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(addGoodsOrderDto.getUserId());
        if (accountInfo.getCollectScore() == null) {
            throw new RestException(-3, "积分不足");
        }
        BigDecimal totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
        BigDecimal totalPrice = BigDecimal.ZERO;
        BigDecimal unitPrice = BigDecimal.ZERO;
        if (ScoreGoods.ISQG_YES.equals(goods.getIsQg())) {
            //限购数量
            Integer quotaTime = goods.getQuotaTime() == null ? 0:goods.getQuotaTime();
            //不限购
            if(quotaTime == 0){
                totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
                unitPrice = sku.getPresentPrice();
            }else{
                //抢购时间内,用户购买该商品的数量
                Integer sumCnt = scoreOrderMapper.selectGoodsQuotaTime(addGoodsOrderDto.getUserId(),goods.getId(),goods.getQgStartTime(),goods.getQgEndTime());
                //剩余限购数量
                quotaTime = quotaTime-sumCnt;
                if(goods.getQgStartTime().before(new Date()) && goods.getQgEndTime().after(new Date())){
                    if(addGoodsOrderDto.getCnt() <= quotaTime){
                        totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
                        unitPrice = sku.getPresentPrice();
                    }else{
                        if(quotaTime > 0){
                            totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(quotaTime)).add(
                                    sku.getOriginalPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()-quotaTime)));
                            unitPrice = sku.getOriginalPrice();
                        }else{
                            totalPrice = sku.getOriginalPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
                            unitPrice = sku.getOriginalPrice();
                        }
                    }
                }else{
                    //开启抢购,不再时间内,直接原价* 数量
                    totalPrice = sku.getOriginalPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
                    unitPrice = sku.getOriginalPrice();
                }
            }
        }else{
            //没有开启抢购,直接现价* 数量
            totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
            unitPrice = sku.getPresentPrice();
        }
        BigDecimal score = new BigDecimal(accountInfo.getCollectScore());
        if (score.compareTo(totalPrice) < 0) {
            throw new RestException(-3, "积分不足");
@@ -206,8 +295,10 @@
        orderDetails.setOrderId(order.getId());
        orderDetails.setCnt(addGoodsOrderDto.getCnt());
        orderDetails.setTotalPrice(totalPrice);
        orderDetails.setUnitPrice(sku.getPresentPrice());
//        orderDetails.setUnitPrice(sku.getPresentPrice());
        orderDetails.setUnitPrice(unitPrice);
        orderDetails.setGoodsId(goods.getId());
        orderDetails.setSkuId(sku.getId());
        orderDetails.setThumb(goods.getThumb());
        scoreOrderDetailsMapper.insert(orderDetails);
@@ -223,8 +314,8 @@
        if (StrUtil.isNotBlank(addressInfo.getTagName())) {
            address.append(StrUtil.isNotBlank(addressInfo.getHouseName()) ? addressInfo.getHouseName() : "");
            if (Constants.ADDRESS_TYPE_HOME.equals(addressInfo.getTagName())) {
                address.append(StrUtil.isNotBlank(addressInfo.getHouseNumber()) ? addressInfo.getHouseNumber() : "");
                address.append(StrUtil.isNotBlank(addressInfo.getUnitName()) ? addressInfo.getUnitName() : "");
                address.append(StrUtil.isNotBlank(addressInfo.getHouseNumber()) ? addressInfo.getHouseNumber() : "");
            }
        }
        expressInfo.setAddress(address.toString());
@@ -241,12 +332,12 @@
        scoreDetails.setType(ScoreDetails.SCORE_TYPE_SHOPPING);
        scoreDetails.setOriginalScore(score);
        scoreDetails.setCurrentScore(remianScore);
        scoreDetails.setChangeScore(totalPrice);
        scoreDetails.setChangeScore(totalPrice.negate());
        scoreDetails.setCreatedTime(new Date());
        scoreDetailsMapper.insert(scoreDetails);
        sku.setStock(sku.getStock() - addGoodsOrderDto.getCnt());
        sku.setQuantity(sku.getQuantity() - addGoodsOrderDto.getCnt());
        sku.setQuantity(sku.getQuantity() + addGoodsOrderDto.getCnt());
        scoreGoodsSkuMapper.updateByPrimaryKey(sku);
    }
@@ -324,7 +415,12 @@
            throw new RestException(-3, "暂不能确认收货");
        }
        scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_DOING, userId);
        if (ScoreOrder.ISFINISH_YES.equals(order.getIsFinish())) {
            scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_DONE, userId);
        } else {
            scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_DOING, userId);
        }
    }
    public void cancelOrder(Long id, String userId) {
@@ -378,4 +474,43 @@
            }
        }
    }
    public StatisticsVo orderStatistics(String userId) {
        BigDecimal score = scoreDetailsMapper.selectTotalScoreByUserId(userId, ScoreDetails.SCORE_TYPE_EXCHANGE);
        Map<String, Object> userData = scoreOrderMapper.selectOrderStastics(userId);
        Map<String, Object> allData = scoreOrderMapper.selectOrderStastics(null);
        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setScore(score);
        BigDecimal userPrice = (BigDecimal) userData.get("totalPrice");
        statisticsVo.setReduceCarbon(userPrice.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_DOWN));
        statisticsVo.setOrderCnt(Integer.parseInt(userData.get("totalOrder").toString()));
        BigDecimal totalPrice = (BigDecimal) allData.get("totalPrice");
        statisticsVo.setTotalReduceCarbon(totalPrice.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_DOWN));
        statisticsVo.setTotalOrderCnt(Integer.parseInt(allData.get("totalOrder").toString()));
        statisticsVo.setTotalBuyCnt(Integer.parseInt(allData.get("totalBuy").toString()));
        return statisticsVo;
    }
    public void autoConfirmReceipt() {
        //获取七天之前后台点击确认提货的订单
        DateTime dateTime = DateUtil.offsetDay(new Date(), -7);
        //
        List<ScoreOrder> orders = scoreOrderMapper.selectOrderByIsFinish(dateTime);
        if(CollUtil.isNotEmpty(orders)){
            for(ScoreOrder order : orders){
                scoreOrderMapper.updateOrderStatusToDone(order.getId());
            }
        }
    }
//    public static void main(String[] args) {
//        DateTime dateTime = DateUtil.offsetDay(new Date(), -7);
//        System.out.println(dateTime);
//    }
}