package com.hzs.activity.pick.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.activity.pick.IAcPickServiceApi;
import com.hzs.activity.pick.service.IAcPickMemberLogService;
import com.hzs.activity.pick.service.IAcPickService;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.domain.activity.pick.AcPick;
import com.hzs.common.domain.activity.pick.AcPickMemberLog;
import com.hzs.common.domain.activity.pick.ext.AcPickExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 提货相关dubbo服务入口
 * @Author: jiang chao
 * @Time: 2023/5/15 11:21
 * @Classname: AcPickProviderApi
 * @PackageName: com.hzs.activity.pick.provider
 */
@Slf4j
@DubboService
public class AcPickProviderApi implements IAcPickServiceApi {

    @Autowired
    private IAcPickService iAcPickService;
    @Autowired
    private IAcPickMemberLogService acPickMemberLogService;

    @Override
    public R<?> handlePickOrder(Long pkMember, String orderCode) {
        try {
            if (iAcPickService.callbackPickOrder(pkMember, orderCode)) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("提货订单回调处理异常", e);
        }
        return R.fail();
    }

    @Override
    public R<Boolean> updatePickUse(List<AcPickExt> acPickExtList, Integer signSource, List<AcPickMemberLog> acPickMemberLogList) {
        try {
            return R.ok(iAcPickService.updatePickUse(acPickExtList, signSource, acPickMemberLogList));
        } catch (Exception e) {
            log.error("更新提货单失败", e);
        }
        return R.fail();
    }

    @Override
    public R<Boolean> updatePickUse(List<AcPickExt> acPickExtList, List<AcPickMemberLog> acPickMemberLogList) {
        try {
            return R.ok(iAcPickService.updatePickUse(acPickExtList, acPickMemberLogList));
        } catch (Exception e) {
            log.error("更新提货单失败", e);
        }
        return R.fail();
    }

    @Override
    public R<List<AcPickMemberLog>> queryPickMemberLog(List<SaOrderExt> saOrderExtList) {
        return R.ok(acPickMemberLogService.queryPickMemberLog(saOrderExtList));
    }

    @Override
    public R<BigDecimal> queryOrderPostage(String orderCode, Integer recProvince, Integer recCity, Integer pkCountry) {
        try {
            LambdaQueryWrapper<AcPickMemberLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AcPickMemberLog::getPickOrder, orderCode);
            AcPickMemberLog acPickMemberLog = acPickMemberLogService.getOne(queryWrapper);
            if (null != acPickMemberLog) {
                AcPick acPick = iAcPickService.getById(acPickMemberLog.getPkPick());
                if (null != acPick) {
                    return R.ok(iAcPickService.postageHandle(acPick, acPickMemberLog.getPickQuantity(), recProvince, recCity, pkCountry));
                }
            }
            return R.fail("提货数据不存在");
        } catch (ServiceException se) {
            log.error("撤销订单查询提货邮费异常: {}", se.getMessage(), se);
            return R.fail(se.getMessage());
        } catch (Exception e) {
            log.error("撤销订单查询提货邮费异常", e);
        }
        return R.fail("查询提货邮费失败");
    }

}
