package com.xxfc.platform.order.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.util.OrderUtil;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.xxfc.platform.order.biz.BaseOrderBiz;
import com.xxfc.platform.order.contant.enumerate.OrderStatusEnum;
import com.xxfc.platform.order.contant.enumerate.OrderTypeEnum;
import com.xxfc.platform.order.entity.BaseOrder;
import com.xxfc.platform.order.entity.inter.OrderDetail;
import com.xxfc.platform.order.mqhandler.RabbitProduct;
import com.xxfc.platform.order.pojo.price.OrderPriceVO;
import com.xxfc.platform.universal.entity.Dictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

@Transactional
public abstract class AbstractOrderHandle<Biz extends BaseBiz, Detail extends OrderDetail>{

    public static final int VERSION_INITIAL = 0;

    @Autowired
    protected Biz detailBiz;

    protected Map<String, Dictionary> dictionaryMap;

    @Autowired
    RabbitProduct rabbitProduct;

    @Autowired
    protected BaseOrderBiz baseOrderBiz;

    protected OrderTypeEnum orderTypeEnum;

    protected Integer channel;

    /**
     * 创建基础订单
     * @return
     */
    public BaseOrder createBaseOrder(Integer orderOrigin) {
        BaseOrder baseOrder = new BaseOrder();

        //设置下单来源
        baseOrder.setOrderOrigin(orderOrigin);

        //设置订单号
        baseOrder.setNo(OrderUtil.GetOrderNumber("", OrderUtil.APP_MID));
        //设置订单类型
        if(null == orderTypeEnum)
            throw new BaseException(ResultCode.NOTEXIST_CODE);
        else {
            baseOrder.setType(orderTypeEnum.getCode());
        }
        //设置订单状态
        baseOrder.setStatus(OrderStatusEnum.ORDER_CRT.getCode());

        //设置初始版本号
        baseOrder.setVersion(VERSION_INITIAL);

        //设置用户id
        baseOrder.setUserId(Integer.valueOf(BaseContextHandler.getUserID()));
        return baseOrder;
    }

    public void initDetail(Detail detail) {
        BaseOrder order = createBaseOrder(detail.getOrderOrigin());
        detail.setOrder(order);
    }

    /**
     * 创建订单及其子项
     * @param detail
     */
    public void createOrder(Detail detail) {
        //初始化
        initDetail(detail);
        //计算价格
        handleCalculate(detail);
        //插入baseOrder
        baseOrderBiz.insertSelective(detail.getOrder());
        //处理detail
        handleDetail(detail);
    }

    /**
     * 处理订单详情
     * @param detail
     */
    public void handleDetail(Detail detail){
        detail.setOrderId(detail.getOrder().getId());
        detailBiz.insertSelective(detail);
        detail.getOrder().setDetailId(detail.getId());
        int updateResult =baseOrderBiz.updateSelectiveByIdRe(detail.getOrder());
        if(updateResult > 0) {
            detail.getOrder().setVersion(detail.getOrder().getVersion() + 1);
        }
    }

    /**
     * 计算价格
     * @param detail
     */
    public OrderPriceVO calculatePrice(Detail detail) {
        OrderPriceVO priceVO = new OrderPriceVO();
        priceVO.setGoodsAmount(BigDecimal.ZERO);
        priceVO.setOrderAmount(BigDecimal.ZERO);
        priceVO.setRealAmount(BigDecimal.ZERO);
        return priceVO;
    }

    /**
     * 计算价格
     * @param detail
     */
    public void handleCalculate(Detail detail) {
        OrderPriceVO opv = calculatePrice(detail);
        BeanUtil.copyProperties(opv, detail.getOrder());
        BeanUtil.copyProperties(opv, detail);
    }

}
