package com.fzm.mall.server.front.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.asset.model.Tran;
import com.fzm.mall.server.front.asset.model.TranOrder;
import com.fzm.mall.server.front.asset.mapper.TranOrderMapper;
import com.fzm.mall.server.front.asset.model.TranPay;
import com.fzm.mall.server.front.asset.model.po.CreateTranOrderPo;
import com.fzm.mall.server.front.asset.service.ITranOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.asset.service.ITranPayService;
import com.fzm.mall.server.front.asset.service.ITranService;
import com.fzm.mall.server.front.goods.model.vo.PageVoWithOne;
import com.fzm.mall.server.front.goods.model.vo.SkuVo;
import com.fzm.mall.server.front.goods.service.IDistributionTemplateService;
import com.fzm.mall.server.front.goods.service.IGoodSkuService;
import com.fzm.mall.server.front.user.model.Header;
import com.fzm.mall.server.front.util.OrderUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-09-07
 */
@Service
public class TranOrderServiceImpl extends ServiceImpl<TranOrderMapper, TranOrder> implements ITranOrderService {

    @Autowired
    private ITranService tranService;

    @Autowired
    private IDistributionTemplateService distributionTemplateService;

    @Autowired
    private IGoodSkuService goodSkuService;

    @Autowired
    private ITranPayService tranPayService;

    @Autowired
    private TranOrderMapper tranOrderMapper;

    @Override
    @Transactional
    public TranPay createOrder(String uid, Integer id, CreateTranOrderPo createTranOrderPo) {
        Tran tran = tranService.getByTranIdForUpdate(id);
        int i = 0;
        BigDecimal amount = BigDecimal.ZERO;
        if(tran.getType() == 1){
            i = tranService.delStock(id,createTranOrderPo.getNumber());
            amount = tran.getPrice().multiply(new BigDecimal(createTranOrderPo.getNumber()));
        }else{
            i = tranService.delStock(id,tran.getNumber());
            createTranOrderPo.setNumber(tran.getNumber());
            amount = tran.getPrice();
        }
        if(i != 1){
            throw  new RuntimeException("stock_err");
        }
        SkuVo skuVo = goodSkuService.getSkuVoBySkuId(tran.getSkuId());
        BigDecimal PostFee = BigDecimal.ZERO;
        if (createTranOrderPo.getDelivery() == 0) {

        } else {
            PostFee = distributionTemplateService.getPrice(skuVo.getTemplateId(), createTranOrderPo.getProvince(), skuVo.getOriginalPrice().multiply(new BigDecimal(createTranOrderPo.getNumber())), createTranOrderPo.getNumber(), skuVo.getWeight().multiply(new BigDecimal(createTranOrderPo.getNumber())));
        }
        if (PostFee.compareTo(new BigDecimal("-1")) == 0) {
            throw new RuntimeException("当前地区不支持快递");
        }
        //支付单
        TranPay tranPay = new TranPay();
        tranPay.setPayId(OrderUtil.getPayId());
        tranPay.setAmount(amount.add(PostFee));
        tranPay.setStatus(0);

        //订单
        String oid = OrderUtil.getOid();
        TranOrder tranOrder = new TranOrder();
        tranOrder.setTranId(tran.getTranId());
        tranOrder.setUid(uid);
        tranOrder.setSeller(tran.getUid());
        tranOrder.setTranId(tran.getTranId());
        tranOrder.setOid(oid);
        tranOrder.setSkuId(tran.getSkuId());
        tranOrder.setGoodsId(tran.getGoodsId());
        tranOrder.setNumber(createTranOrderPo.getNumber());
        tranOrder.setPrice(tran.getPrice());
        tranOrder.setCoin(tran.getCoin());
        if(createTranOrderPo.getDelivery() == 0){

        }else{
            tranOrder.setUname(createTranOrderPo.getUname());
            tranOrder.setUphone(createTranOrderPo.getUphone());
            tranOrder.setUaddress(createTranOrderPo.getUaddress());
            tranOrder.setPostFee(PostFee);
        }
        tranOrder.setStatus(1);
        save(tranOrder);
        tranPay.setOid(oid);
        tranPayService.save(tranPay);
        return tranPay;
    }

    @Override
    public TranOrder getByOid(String oid) {
        QueryWrapper<TranOrder> q = new QueryWrapper<>();
        q.eq("oid",oid);
        return getOne(q);
    }

    @Override
    public List<TranOrder> getListAll(String uid, PageVoWithOne pageVoWithOne) {
        int start = (pageVoWithOne.getPage()-1)*pageVoWithOne.getSize();
        return tranOrderMapper.getListAll(uid,start,pageVoWithOne.getSize());
    }

    @Override
    public List<TranOrder> getListOut(String uid, PageVoWithOne pageVoWithOne) {
        int start = (pageVoWithOne.getPage()-1)*pageVoWithOne.getSize();
        return tranOrderMapper.getListOut(uid,start,pageVoWithOne.getSize());
    }

    @Override
    public List<TranOrder> getListIn(String uid, PageVoWithOne pageVoWithOne) {
        int start = (pageVoWithOne.getPage()-1)*pageVoWithOne.getSize();
        return tranOrderMapper.getListIn(uid,start,pageVoWithOne.getSize());
    }

    @Override
    public int getAll(String uid) {
        return tranOrderMapper.getAll(uid);
    }

    @Override
    public int getOut(String uid) {
        return tranOrderMapper.getOut(uid);
    }

    @Override
    public int getIn(String uid) {
        return tranOrderMapper.getIn(uid);
    }

    @Override
    public List<TranOrder> getByUid(String uid, String status, String seller, String skuId) {
        QueryWrapper<TranOrder> q = new QueryWrapper<>();
        if(!StringUtil.isNullOrEmpty(uid)){
            q.eq("uid",uid);
        }
        if(!StringUtil.isNullOrEmpty(status)){
            q.eq("status",status);
        }
        if(!StringUtil.isNullOrEmpty(seller)){
            q.eq("seller",seller);
        }
        if(!StringUtil.isNullOrEmpty(skuId)){
            q.eq("sku_id",skuId);
        }

        return list(q);
    }
}
