package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountRecord;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountOrder;
import com.bohuikeji.delivery.backstage.mapper.TDiscountOrderMapper;
import com.bohuikeji.delivery.backstage.service.ITDiscountOrderService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.ITDiscountRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 客户下单使用优惠券
 * </p>
 *
 * @author zhouliwu
 * @since 2021-03-29
 */
@Slf4j
@Service
public class TDiscountOrderServiceImpl extends ServiceImpl<TDiscountOrderMapper, TDiscountOrder> implements ITDiscountOrderService {
    @Autowired
    ITDiscountRecordService discountRecordService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ITDiscountOrderService discountOrderService;

    /**
     * 下单使用优惠信息从新计算预估金额
     * 历史存入明细
     * @param order
     * @return
     */
    /*@Override
    public BigDecimal useDiscount(TOrder order) {
        log.info("!!!!!!!!!!!!!!!!!:[{}]",order.getOrderExpectPrice());
        BigDecimal bignum = new BigDecimal("0.00").setScale(1, BigDecimal.ROUND_HALF_UP);//优惠的金额
        BigDecimal bignum1 = new BigDecimal("10.0").setScale(1, BigDecimal.ROUND_HALF_UP);;
        BigDecimal bignum3 = null;
        TDiscountRecord discountRecord = discountRecordService.getOne(new QueryWrapper<TDiscountRecord>().in("usage_status","未使用","使用中").eq("release_id",order.getReleaseId()).eq("customer_id",order.getPlayerId()));
        if(discountRecord != null){
            if("满减卷".equals(discountRecord.getDisType()) && order.getOrderExpectPrice().compareTo(discountRecord.getStartAmount()) < 0){//订单金额小于等于起始金额时不可用“满减卷”
                return bignum;
            }
            if("折扣卷".equals(discountRecord.getDisType()) && order.getOrderExpectPrice().compareTo(discountRecord.getStartAmount()) < 0){//订单金额小于等于起始金额时不可用“折扣卷”
                return bignum;
            }
            if("立减卷".equals(discountRecord.getDisType()) && order.getOrderExpectPrice().compareTo(discountRecord.getStartAmount()) < 0){//订单金额小于等于起始金额时不可用“折扣卷”
                return bignum;
            }
            if("折扣卷".equals(discountRecord.getDisType()) && order.getOrderExpectPrice().compareTo(discountRecord.getStartAmount()) > -1 || "尊享卡".equals(discountRecord.getDisType()) && order.getOrderExpectPrice().compareTo(discountRecord.getStartAmount()) > -1){//订单金额大于等于起始金额  && order.getOrderExpectPrice().compareTo(receiveRecord.getStartAmount()) > -1
                BigDecimal prenum =bignum1.subtract(discountRecord.getDisValue());
                bignum3 = prenum.divide(bignum1,1,BigDecimal.ROUND_HALF_UP);
                bignum = order.getOrderExpectPrice().multiply(bignum3);//计算打折后的优惠金额
            }else {
                bignum = discountRecord.getDisValue();//获取优惠金额
            }
            log.info("折扣优惠卷的金额111111111:[{}]",bignum);
        }
        return bignum;
    }*/

    /**
     * 下单时保存订单使用优惠记录
     * @param order
     * @return
     */
    @Override
    public boolean saveDiscount(TOrder order,TDiscountRecord discountRecord) {
        TDiscountOrder discountOrder = new TDiscountOrder();
        discountOrder.setDisId(discountRecord.getDisId());
        discountOrder.setCustomerId(order.getPlayerId());
        discountOrder.setOrderNum(order.getOrderNum());
        discountOrder.setFranId(order.getFranId());
        discountOrder.setReleaseId(discountRecord.getReleaseId());
        discountOrder.setDisType(discountRecord.getDisType());
        discountOrder.setDisRemark(discountRecord.getDisRemark());
        discountOrder.setOrderTotalAmount(order.getTotalOrderAmount());
        discountOrder.setDisAmount(order.getPrefAmount());
        discountOrder.setEstimateAmount(order.getTotalOrderAmount().subtract(order.getPrefAmount()));
        discountOrder.setDisState("已使用");
        discountOrder.setCreateTime(LocalDateTime.now());
        save(discountOrder);
        order.setDisId(discountOrder.getUseId());//订单与优惠使用记录绑定
        order.setPrefAmount(order.getPrefAmount());
        orderService.updateById(order);
        return true;
    }

    @Override
    public boolean wxDiscount(TOrder order, JSONObject jsonObject) {
        JSONObject js=null;
        for (int i = 0; i < jsonObject.getJSONArray("promotion_detail").size(); i++) {
            js = jsonObject.getJSONArray("promotion_detail").getJSONObject(i);
        }
        TDiscountOrder orderDiscount = new TDiscountOrder();
        orderDiscount.setCustomerId(order.getPlayerId());
        orderDiscount.setOrderNum(order.getOrderNum());
        orderDiscount.setFranId(order.getFranId());
        orderDiscount.setDisType("代金券");
        orderDiscount.setDisRemark(js.getString("coupon_id"));
        orderDiscount.setOrderTotalAmount(order.getTotalOrderAmount());
        orderDiscount.setDisAmount(new BigDecimal(js.getString("amount")).divide(new BigDecimal("100")));
        orderDiscount.setEstimateAmount(order.getTotalOrderAmount().subtract(order.getPrefAmount()));
        orderDiscount.setDisState("已使用");
        orderDiscount.setCreateTime(LocalDateTime.now());
        orderDiscount.setDisId(296);
        save(orderDiscount);
        order.setPrefAmount(order.getPrefAmount().add(orderDiscount.getDisAmount()));
        orderDiscount.setDisId(296);
        orderService.updateById(order);
        return true;
    }

    /**
     * 取消订单后优惠卷退回
     * @param order
     * @return
     */
    @Override
    public TOrder cancelOrderDisByVoucher(TOrder order) {
       TDiscountOrder discountOrder = discountOrderService.getById(order.getDisId());//根据订单绑定的使用记录id查询订单使用记录
       if(discountOrder != null){
           discountOrderService.updateById(discountOrder.setDisState("已取消"));
           discountRecordService.update(new UpdateWrapper<TDiscountRecord>().set("usage_time",null).set("usage_status","未使用").eq("usage_status","已使用").eq("release_id",discountOrder.getReleaseId()));
       }
        return order;
    }

}
