package com.fdkankan.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.goods.common.SkuCache;
import com.fdkankan.goods.service.IGoodsSkuService;
import com.fdkankan.goods.vo.ResponseGoodsSku;
import com.fdkankan.order.entity.DownloadOrder;
import com.fdkankan.order.entity.Invoice;
import com.fdkankan.order.mapper.IDownloadOrderMapper;
import com.fdkankan.order.service.IDownloadOrderService;
import com.fdkankan.order.service.IInvoiceService;
import com.fdkankan.user.request.RequestDownloadOrder;
import com.fdkankan.user.request.RequestIncrementOrder;
import com.fdkankan.user.vo.ResponseDownloadOrder;
import com.fdkankan.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.List;

/**
 * <p>
 * 下载次数订单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class DownloadOrderServiceImpl extends ServiceImpl<IDownloadOrderMapper, DownloadOrder> implements IDownloadOrderService {

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

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

        DownloadOrder downloadOrderEntity = new DownloadOrder();
        downloadOrderEntity.setOrderSn(NumberUtils.getOrderSn());
        downloadOrderEntity.setAmount(total);
        downloadOrderEntity.setPayType(order.getPayType());
        downloadOrderEntity.setPayStatus(-1);
        downloadOrderEntity.setUserId(userId);
        downloadOrderEntity.setCount(order.getCount());
        downloadOrderEntity.setAbroad(order.getAbroad());
        downloadOrderEntity.setDownStatus(1);
        downloadOrderEntity.setSceneNum(order.getSceneNum());
        downloadOrderEntity.setSceneName(order.getSceneName());
        if(!this.save(downloadOrderEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        ResponseDownloadOrder downloadOrder = new ResponseDownloadOrder();
        BeanUtils.copyProperties(downloadOrderEntity, downloadOrder);
        return downloadOrder;
    }

    @Override
    public Page<ResponseDownloadOrder> getPageByParam(Long userId, RequestDownloadOrder param) {
        QueryWrapper<DownloadOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DownloadOrder::getUserId,userId);
        queryWrapper.lambda().eq(DownloadOrder::getPayStatus,1);
        queryWrapper.lambda().orderByDesc(DownloadOrder::getTradeTime);
        Page<DownloadOrder> entityPage = new Page<>(param.getPageNum(),param.getPageSize());
        Page<DownloadOrder> page = this.page(entityPage, queryWrapper);

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

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

        ResponseInvoice responseInvoice = null;
        QueryWrapper<Invoice> invoiceQueryWrapper= null;
        for (ResponseDownloadOrder responseDownloadOrder : voPage.getRecords()) {
            invoiceQueryWrapper = new QueryWrapper<>();
            invoiceQueryWrapper.lambda().eq(Invoice::getDownloadOrderId,responseDownloadOrder.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);
                    responseDownloadOrder.setInvoice(responseInvoice);
                }
            }
        }
        return voPage;
    }

    private List<ResponseDownloadOrder> convert(List<DownloadOrder> records, String description) {
        List<ResponseDownloadOrder> result = new ArrayList<>();
        ResponseDownloadOrder responseDownloadOrder = null;
        for (DownloadOrder downloadOrderEntity : records) {
            responseDownloadOrder = new ResponseDownloadOrder();
            BeanUtils.copyProperties(downloadOrderEntity, responseDownloadOrder);
//            responseDownloadOrder.setName(name);
            result.add(responseDownloadOrder);
        }
        return result;
    }
}
