package com.hzs.sale.order.provider;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.sale.ext.SaDeliverItemsExt;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
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.dto.*;
import com.hzs.sale.order.param.*;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.sale.order.vo.MemberFirstOrderVo;
import com.hzs.sale.order.vo.SingleItemStatVo;
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.*;
import java.util.stream.Collectors;

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

    @Autowired
    private RedisService redisService;
    @Autowired
    private ISaOrderService iSaOrderService;

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

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

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

    @Override
    public R<List<SaOrderItemsExt>> queryOrderItemsListByOrderId(List<Long> pkOrderList, Integer pkCountry) {
        OrderSelectParam param = new OrderSelectParam();
        param.setPkOrderList(pkOrderList);
        return R.ok(iSaOrderService.queryOrderSelect(param, pkCountry));
    }

    @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<Boolean> updateOrder(SaOrder saOrder) {
        return R.ok(iSaOrderService.updateById(saOrder));
    }

    @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<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<List<SaOrderExt>> selfAddressOrderList(String orderCode, Integer editType, Long memberId, Integer pkCountry) {
        MyOrderParam param = new MyOrderParam();
        param.setOrderCode(orderCode);
        param.setEditType(editType);
        return R.ok(iSaOrderService.selfAddressOrderList(param, memberId, pkCountry));
    }

    @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 List<MemberFirstOrderDTO> checkMemberIsFirstOrder(List<String> memberCodeList) {
        List<MemberFirstOrderVo> memberFirstOrderVos = iSaOrderService.checkMemberIsFirstOrder(memberCodeList);
        return memberFirstOrderVos.stream().map(a -> BeanUtil.copyProperties(a, MemberFirstOrderDTO.class)).collect(Collectors.toList());
    }

    @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());
        }
    }

    @Override
    public List<SingleItemStatDTO> singleItemStat(SingleItemStatParamDTO singleItemStatParamDTO) {
        SingleItemStatParam singleItemStatParam = BeanUtil.copyProperties(singleItemStatParamDTO, SingleItemStatParam.class);

        List<SingleItemStatVo> singleItemStatVoList = iSaOrderService.singleItemStat(singleItemStatParam);

        return singleItemStatVoList.stream().map(a -> BeanUtil.copyProperties(a, SingleItemStatDTO.class)).collect(Collectors.toList());
    }

    @Override
    public R<BigDecimal> getRetailPayOrder(Long pkMember, String orderCode) {
        // 订单缓存
        SaOrderExt saOrder = redisService.getCacheObject(CacheConstants.RETAIL_TEMP_ORDER + pkMember + orderCode);
        if (null == saOrder) {
            log.error("在线支付订单缓存已失效, pkMember: {}, orderCode: {}", pkMember, orderCode);
            return R.fail("订单已失效");
        }
        return R.ok(ComputeUtil.computeAdd(saOrder.getOrderAmount(), saOrder.getPostage()));
    }

    @Override
    public R<SaOrderExt> queryOrderByCode(String orderCode, Long pkMember) {
        SaOrderExt saOrder = redisService.getCacheObject(CacheConstants.TEMP_ORDER + pkMember + orderCode);
        if (null == saOrder) {
            // 直销订单
            saOrder = iSaOrderService.queryOrderByCode(orderCode, null);
        }
        return R.ok(saOrder);
    }
}
