package com.gree.mall.manager.logic.material.manage;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.material.manage.WebsitTradeVO;
import com.gree.mall.manager.commonmapper.MaterialMapper;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.enums.material.PayOrderTypeEnum;
import com.gree.mall.manager.enums.material.PayTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.AllInPayLogic;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.WebsitSalesPayOrder;
import com.gree.mall.manager.plus.entity.WebsitSalesPayOrderRet;
import com.gree.mall.manager.plus.service.WebsitSalesPayOrderRetService;
import com.gree.mall.manager.plus.service.WebsitSalesPayOrderService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class WebsitTradeLogic {

    private final CommonLogic commonLogic;
    private final MaterialMapper materialMapper;
    private final AllInPayLogic allInPayLogic;
    private final WebsitSalesPayOrderService websitSalesPayOrderService;
    private final WebsitSalesPayOrderRetService websitSalesPayOrderRetService;

    public IPage<WebsitTradeVO> page(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.materialParam(zfireParamBean, WebsitTradeVO.class, adminUser);

        return materialMapper.websitTradePage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
    }

    @Transactional
    public void tradeRefund(String id, BigDecimal retAmount, HttpServletRequest request) {
        WebsitSalesPayOrder payOrder = websitSalesPayOrderService.getById(id);
        if (Objects.isNull(payOrder)) {
            throw new RemoteServiceException("订单不存在");
        }
        if (!payOrder.getPayType().equals(PayTypeEnum.ALLINPAY.getKey())) {
            throw new RemoteServiceException("支付方式非”通联支付“");
        }
        if (payOrder.getPayFlag().equals(IsYesNoEnum.NO.getKey())) {
            throw new RemoteServiceException("未支付，退款失败");
        }
        if (payOrder.getOrderType().equals(PayOrderTypeEnum.R.getKey())) {
            throw new RemoteServiceException("订单类型非”收款“");
        }
        if (Objects.isNull(retAmount) || retAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new RemoteServiceException("请输入正确的退款金额");
        }
        if (payOrder.getRetValue().compareTo(payOrder.getPayValue()) >= 0) {
            throw new RemoteServiceException("订单退款金额已等于支付金额");
        }
        final BigDecimal realRetAmount = payOrder.getRetValue().add(retAmount);
        if (realRetAmount.compareTo(payOrder.getPayValue()) > 0) {
            throw new RemoteServiceException("订单退款金额加本次退款金额已大于支付金额");
        }
        final String retOrderId = IdWorker.getIdStr();
        allInPayLogic.serviceRefundOrder(payOrder, retOrderId, retAmount);
        payOrder.setRetValue(realRetAmount)
                .updateById();
        new WebsitSalesPayOrderRet()
                .setId(retOrderId)
                .setWebsitSalesPayOrderId(payOrder.getId())
                .setOrderId(payOrder.getOrderId())
                .setAmount(retAmount)
                .insert();

        new WebsitSalesPayOrder()
                .setCompanyWechatId(payOrder.getCompanyWechatId())
                .setCompanyWechatName(payOrder.getCompanyWechatName())
                .setGoodsType(payOrder.getGoodsType())
                .setWebsitId(payOrder.getWebsitId())
                .setWebsitName(payOrder.getWebsitName())
                .setOrderId(retOrderId)
                .setOriginOrderId(payOrder.getOrderId())
                .setPayType(payOrder.getPayType())
                .setPayFlag(IsYesNoEnum.YES.getKey())
                .setOrderCreateTime(payOrder.getOrderCreateTime())
                .setPayValue(retAmount)
                .setPayTime(DateUtil.date())
                .setOrderType(PayOrderTypeEnum.R.getKey())
                .setOrderSource(payOrder.getOrderSource())
                .insert();
    }
}
