package com.hzs.sale.order.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOrderStatus;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.sale.ext.SaDeliverItemsExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.sale.order.SaOrderItems;
import com.hzs.sale.order.ISaOrderServiceApi;
import com.hzs.sale.order.param.OrderQueryParam;
import com.hzs.sale.order.service.ISaOrderItemsService;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 订单对外提供接口
 */
@Slf4j
@DubboService
public class SaOrderServiceProvider implements ISaOrderServiceApi {

    @Autowired
    private ISaOrderService iSaOrderService;
    @Autowired
    private ISaOrderItemsService iSaOrderItemsService;

    @Override
    public R<List<SaOrderExt>> querySaOrderByDay(Date startDate, Date endDate) {
        List<SaOrderExt> saOrderList = iSaOrderService.querySaOrderByTime(startDate, endDate, null);
        return R.ok(saOrderList);
    }

    @Override
    public R<SaOrder> querySaOrderExtByPkId(Long pkId) {
        return R.ok(iSaOrderService.querySaOrderExtByPkId(pkId));
    }

    @Override
    public R<List<SaOrderExt>> querySaOrderByEffectiveDay(Date startDate, Date endDate) {
        return R.ok(iSaOrderService.querySaOrderByEffectiveDay(startDate, endDate));
    }

    @Override
    public R<List<SaOrderExt>> queryDeliverySaOrderByDay(Date startDate, Date endDate) {
        return R.ok(iSaOrderService.queryDeliverySaOrderByDay(startDate, endDate));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> regEmptyOrderApproved(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        return R.ok(iSaOrderService.regEmptyOrderApproved(approvalBusinessResultDTO));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean regEmptyOrderReject(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        return iSaOrderService.regEmptyOrderReject(approvalBusinessResultDTO);
    }

    @Override
    public R<List<SaOrderExt>> queryOrderListByIdList(List<Long> orderIdList, Integer pkCountry) {
        return R.ok(iSaOrderService.queryOrderListByIdList(orderIdList, pkCountry));
    }

    @Override
    public void closeOrder() {
        iSaOrderService.closeOrder();
    }

    @Override
    public R<List<SaOrder>> querySaOrderByOrderType(String payTime) {
        return R.ok(iSaOrderService.querySaOrderByOrderType(payTime));
    }

    @Override
    public R<SaOrder> queryOrderTotalAchieveByTime(Date startDate, Date endDate) {
        return R.ok(iSaOrderService.queryOrderTotalAchieveByTime(startDate, endDate));
    }

    @Override
    public R<List<Long>> querySeqList(Integer param) {
        return R.ok(iSaOrderService.querySeqList(param));
    }

    @Override
    public R saveOrderBatch(List<SaOrder> orderList, List<SaOrderItems> orderItemsList) {
        try {
            if (iSaOrderService.insertBach(orderList, orderItemsList) > 0) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("批量插入订单和订单明细异常", e);
        }
        return R.fail();
    }

    @Override
    public R insertUnrealOrderBach(List<SaOrder> orderList) {
        try {
            if (iSaOrderService.insertUnrealOrderBach(orderList) > 0) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("批量插入订单和订单明细异常", e);
        }
        return R.fail();
    }


    @Override
    public R<Long> saveOrderInfo(SaOrder saOrder, SaOrderItems orderItems) {
        try {
            return R.ok(iSaOrderService.saveOrderInfo(saOrder, orderItems));
        } catch (Exception e) {
            log.error("插入订单和订单明细异常", e);
        }
        return R.fail();
    }

    @Override
    public R<SaOrderExt> queryUpgradeAmountAndAchieve(Long pkMember) {
        return R.ok(iSaOrderService.queryUpgradeAmountAndAchieve(pkMember));
    }

    @Override
    public R<BigDecimal> getMemberOrderAchieve(Long pkMember) {
        return R.ok(iSaOrderService.getMemberOrderAchieve(pkMember));
    }

    @Override
    public R<List<SaOrderExt>> querySaOrderByDeliverList(List<SaDeliverItemsExt> deliverItemExtList) {
        return R.ok(iSaOrderService.querySaOrderByDeliverList(deliverItemExtList));
    }

    @Override
    public R<Boolean> sendDeliverBySaOrder(List<SaOrderExt> saOrderExtList, Long pkMember) {
        try {
            return R.ok(iSaOrderItemsService.sendDeliverBySaOrder(saOrderExtList, pkMember));
        } catch (Exception e) {
            return R.fail(Boolean.FALSE);
        }
    }

    @Override
    public R<List<SaOrderExt>> queryStoreSaOrderByParam(List<Long> orderItemIdList) {
        OrderQueryParam queryParam = new OrderQueryParam();
        queryParam.setItemIdList(orderItemIdList);
        queryParam.setOrderStatus(EOrderStatus.PAY.getValue());
        return R.ok(iSaOrderService.queryStoreSaOrderByParam(queryParam));
    }

    @Override
    public R<CuMemberAccountExt> queryOrderSpendDetails(CuMemberAccountExt cuMemberAccount, SaOrder saOrder, List<SaOrderItems> orderItemList) {
        try {
            return R.ok(iSaOrderService.queryOrderSpendDetails(cuMemberAccount, saOrder, orderItemList));
        } catch (Exception e) {
            log.error("验证会员账户余额是否够支付失败 msg: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

}
