package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.common.PageInfo;
import com.fdkankan.common.util.NumberUtils;
import com.fdkankan.ucenter.constant.OrderConstant;
import com.fdkankan.ucenter.entity.DownloadOrder;
import com.fdkankan.ucenter.entity.Invoice;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.httpClient.param.PayGoods;
import com.fdkankan.ucenter.httpClient.service.PayService;
import com.fdkankan.ucenter.httpClient.vo.PayOrderVo;
import com.fdkankan.ucenter.mapper.IDownloadOrderMapper;
import com.fdkankan.ucenter.service.IDownloadOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IInvoiceService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.request.DownNumParam;
import com.fdkankan.ucenter.vo.request.IncrementOrderParam;
import com.fdkankan.ucenter.vo.response.DownloadVo;
import com.sun.javafx.font.directwrite.DWFactory;
import org.apache.commons.lang3.StringUtils;
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.Arrays;
import java.util.Date;
import java.util.List;

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

    @Autowired
    IUserService userService;
    @Autowired
    IInvoiceService invoiceService;

    @Override
    public PageInfo pageList(IncrementOrderParam param) {
        User user = userService.getByUserName(param.getUserName());
        LambdaQueryWrapper<DownloadOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DownloadOrder::getUserId,user.getId());
        wrapper.eq(DownloadOrder::getPayStatus,1);
        wrapper.orderByDesc(DownloadOrder::getTradeTime);
        Page<DownloadOrder> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);

        List<DownloadVo> listVo = new ArrayList<>();
        for (DownloadOrder record : page.getRecords()) {
            if(StringUtils.isNotBlank(record.getTradeTime())){
                record.setTradeTime(DateUserUtil.AddMinute(record.getTradeTime(),record.getTimeZoneOff()));
            }
            DownloadVo vo = new DownloadVo();
            BeanUtils.copyProperties(record,vo);

            Invoice invoice = invoiceService.getByDownId(record.getId());
            vo.setInvoice(invoice);
            listVo.add(vo);
        }
        Page<DownloadVo> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
        pageVo.setTotal(page.getTotal());
        pageVo.setRecords(listVo);
        return PageInfo.PageInfo(pageVo);
    }

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

    @Override
    public DownloadOrder getByOrderSnNoPay(String orderSn) {
        QueryWrapper<DownloadOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DownloadOrder::getOrderSn,orderSn);
        queryWrapper.lambda().ne(DownloadOrder::getPayStatus,1);
        List<DownloadOrder> list = this.list(queryWrapper);
        if(list == null || list.size() <=0){
            return null;
        }
        return list.get(0);    }

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

    }

    @Autowired
    PayService payService;

    @Override
    public DownloadOrder insertDownloadOrder(User user, DownNumParam param, BigDecimal price) {
        BigDecimal total = price.multiply(new BigDecimal(param.getCount()));
        PayGoods payGoods = new PayGoods(param.getSceneName() ,param.getCount(),param.getSceneNum(),2);
        PayOrderVo payOrderVo = payService.downOrder(total, "downOrder",user.getUserName(),user.getNickName(), Arrays.asList(payGoods),null);
        if(payOrderVo == null){
            throw new BusinessException(OrderConstant.FAILURE_CODE_8005,OrderConstant.FAILURE_MSG_8005);
        }

        DownloadOrder downloadOrderEntity = new DownloadOrder();
        downloadOrderEntity.setOrderSn(payOrderVo.getOrderSn());
        downloadOrderEntity.setAmount(total);
        downloadOrderEntity.setPayType(param.getPayType());
        downloadOrderEntity.setPayStatus(-1);
        downloadOrderEntity.setUserId(user.getId());
        downloadOrderEntity.setCount(param.getCount());
        downloadOrderEntity.setAbroad(param.getAbroad());
        downloadOrderEntity.setStatus(1);
        downloadOrderEntity.setSceneNum(param.getSceneNum());
        downloadOrderEntity.setSceneName(param.getSceneName());
        downloadOrderEntity.setTimeZoneOff(param.getTimeZoneOff());
        downloadOrderEntity.setRecStatus("A");
        downloadOrderEntity.setCreateTime(DateUserUtil.getDate(new Date()));
        downloadOrderEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
        downloadOrderEntity.setAgentKey(param.getAgentKey());
        this.save(downloadOrderEntity);
        return downloadOrderEntity;
    }
}
