package com.fdkankan.platform.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.NumberUtils;
import com.fdkankan.platform.goods.common.SkuCache;
import com.fdkankan.platform.goods.service.IGoodsSkuService;
import com.fdkankan.platform.goods.vo.ResponseGoodsSku;
import com.fdkankan.platform.order.entity.IncrementOrder;
import com.fdkankan.platform.order.entity.Invoice;
import com.fdkankan.platform.order.mapper.IIncrementOrderMapper;
import com.fdkankan.platform.order.service.IIncrementOrderService;
import com.fdkankan.platform.order.service.IInvoiceService;
import com.fdkankan.platform.user.request.RequestIncrementOrder;
import com.fdkankan.platform.user.vo.ResponseIncrementOrder;
import com.fdkankan.platform.user.vo.ResponseInvoice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 增值权益订单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class IncrementOrderServiceImpl extends ServiceImpl<IIncrementOrderMapper, IncrementOrder> implements IIncrementOrderService {

    @Autowired
    private IGoodsSkuService goodsSkuService;
    @Autowired
    private IInvoiceService invoiceService;

    @Override
    public ResponseIncrementOrder saveByOrderParam(Long userId, RequestIncrementOrder order, BigDecimal price) {
        BigDecimal total = price.multiply(new BigDecimal(order.getCount()));

        IncrementOrder incrementOrderEntity = new IncrementOrder();
        incrementOrderEntity.setOrderSn(NumberUtils.getOrderSn());
        incrementOrderEntity.setAmount(total);
        incrementOrderEntity.setPayType(order.getPayType());
        incrementOrderEntity.setPayStatus(-1);
        incrementOrderEntity.setUserId(userId);
        incrementOrderEntity.setCount(order.getCount());
        incrementOrderEntity.setAbroad(order.getAbroad());
        incrementOrderEntity.setStatus(order.getIncrementId() == null ? 1 : 2);
        incrementOrderEntity.setIncrementId(order.getIncrementId());
        if(!this.save(incrementOrderEntity)){
            throw  new BusinessException(ErrorCode.ERROR_MSG);
        }
        ResponseIncrementOrder incrementOrder = new ResponseIncrementOrder();
        BeanUtils.copyProperties(incrementOrderEntity, incrementOrder);
        return incrementOrder;
    }

    @Override
    public Page<ResponseIncrementOrder> getPageByParam(Long userId, RequestIncrementOrder param) {
        QueryWrapper<IncrementOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(IncrementOrder::getRecStatus,"A");
        queryWrapper.lambda().eq(IncrementOrder::getUserId,userId);
        queryWrapper.lambda().eq(IncrementOrder::getPayStatus,1);
        queryWrapper.lambda().orderByDesc(IncrementOrder::getTradeTime);
        Page<IncrementOrder> entityPage = new Page<>(param.getPageNum(),param.getPageSize());
        Page<IncrementOrder> page = this.page(entityPage, queryWrapper);

        ResponseGoodsSku sku = goodsSkuService.getVoBySkuSn(SkuCache.skusMap.get("5"));

        List<ResponseIncrementOrder> responseList = this.convert(page.getRecords(), sku.getDescription());
        Page<ResponseIncrementOrder> voPage = new Page<ResponseIncrementOrder>(param.getPageNum(), param.getPageSize());
        voPage.setTotal(page.getTotal());
        voPage.setRecords(responseList);

        ResponseInvoice responseInvoice = null;
        QueryWrapper<Invoice> invoiceQueryWrapper= null;
        for (ResponseIncrementOrder responseIncrementOrder : voPage.getRecords()) {
            invoiceQueryWrapper = new QueryWrapper<>();
            invoiceQueryWrapper.lambda().eq(Invoice::getIncrementOrderId,responseIncrementOrder.getId());
            List<Invoice> list = invoiceService.list(invoiceQueryWrapper);
            if(list.size() > 0){
                Invoice invoiceEntity = list.get(0);
                if(invoiceEntity != null){
                    responseInvoice = new ResponseInvoice();
                    BeanUtils.copyProperties(invoiceEntity, responseInvoice);
                    responseIncrementOrder.setInvoice(responseInvoice);
                }
            }
        }
        return voPage;

    }

    private List<ResponseIncrementOrder> convert(List<IncrementOrder> list, String name){
        List<ResponseIncrementOrder> result = new ArrayList<>();
        ResponseIncrementOrder responseIncrementOrder = null;
        for (IncrementOrder incrementOrderEntity : list) {
            responseIncrementOrder = new ResponseIncrementOrder();
            BeanUtils.copyProperties(incrementOrderEntity, responseIncrementOrder);
            responseIncrementOrder.setName(name);
            result.add(responseIncrementOrder);
        }
        return result;
    }

    @Override
    public IncrementOrder getByOrderSn(String orderSn) {
        QueryWrapper<IncrementOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(IncrementOrder::getRecStatus,"A");
        queryWrapper.lambda().eq(IncrementOrder::getOrderSn,orderSn);
        List<IncrementOrder> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean paySuccessIncrementOrder(String orderSn, String tradeNo, int paymentTypeName) {
        LambdaUpdateWrapper<IncrementOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(IncrementOrder::getPayType,paymentTypeName)
                .set(IncrementOrder::getNumber,tradeNo)
                .set(IncrementOrder::getPayStatus, 1)
                .set(IncrementOrder::getTradeTime, new Date())
                .eq(IncrementOrder::getOrderSn,orderSn)
                .eq(IncrementOrder::getRecStatus,"A");
        return this.update(updateWrapper);
    }

    @Override
    public ResponseIncrementOrder getVoById(Long incrementOrderId) {
        IncrementOrder incrementOrderEntity  = getById(incrementOrderId);
        ResponseIncrementOrder responseIncrementOrder = new ResponseIncrementOrder();
        if(incrementOrderEntity!=null){
            BeanUtils.copyProperties(incrementOrderEntity,responseIncrementOrder);
        }
        return responseIncrementOrder;
    }
}
