package com.hzs.activity.base.provider;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.activity.base.IActivityServiceApi;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.draw.service.IAcDrawRewardNumService;
import com.hzs.activity.dto.AcRecommendUpgradeDTO;
import com.hzs.activity.recommend.dto.AcRecommendUpgrade;
import com.hzs.activity.recommend.service.IAcRepurCouponsRuleConfigService;
import com.hzs.activity.recommend.service.IAcRepurSharRuleConfigService;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.activity.recommend.AcRepurCouponsRuleConfig;
import com.hzs.common.domain.activity.recommend.ext.AcRepurSharRuleConfigExt;
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.Date;
import java.util.List;

/**
 * 所有活动的生效，获得赠品入口
 */
@Slf4j
@DubboService
public class ActivityServiceProvider implements IActivityServiceApi {

    @Autowired
    private IAcDrawRewardNumService acDrawRewardNumService;
    @Autowired
    private IAcRepurCouponsRuleConfigService acRepurCouponsRuleConfigService;
    @Autowired
    private IAcRepurSharRuleConfigService acRepurSharRuleConfigService;
    @Autowired
    private IActivityService iActivityService;

    @Override
    public R<BigDecimal> queryDrawRewardPayNumMoney(Long pkMember, String orderCode) {
        BigDecimal payMoney = acDrawRewardNumService.queryDrawRewardPayNumMoney(pkMember, orderCode);
        if (payMoney == null) {
            return R.fail();
        }
        return R.ok(payMoney);
    }

    @Override
    public R<Boolean> handleActivity(SaOrderExt saOrderExt) {
        try {
            return R.ok(iActivityService.handleActivity(saOrderExt));
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<Boolean> callBackOnlinePay(Long pkMember, String orderCode) {
        try {
            return R.ok(acDrawRewardNumService.callBackOnlinePay(pkMember, orderCode));
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<Boolean> backGiveMemberDrawNum(SaOrderExt saOrderExt) {
        try {
            return R.ok(acDrawRewardNumService.backGiveMemberDrawNum(saOrderExt));
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<List<AcRepurCouponsRuleConfig>> queryAcRepurchaseCouponsRule(Date settleDate) {
        List<AcRepurCouponsRuleConfig> acRepurCouponsRuleConfigList = acRepurCouponsRuleConfigService.queryAcRepurchaseCouponsRule(settleDate);
        if (acRepurCouponsRuleConfigList != null && acRepurCouponsRuleConfigList.size() > 0) {
            return R.ok(acRepurCouponsRuleConfigList);
        } else {
            return R.fail();
        }
    }

    @Override
    public R<List<AcRepurSharRuleConfigExt>> queryRepurSharRuleConfig(Date startDate, Date endDate) {
        return R.ok(acRepurSharRuleConfigService.queryRepurSharRuleConfig(startDate, endDate));
    }

    @Override
    public R<Boolean> invokeRecommendUpgrade(AcRecommendUpgradeDTO recommendUpgradeDTO) {
        try {
            return R.ok(acRepurCouponsRuleConfigService.invokeRecommendUpgrade(BeanUtil.copyProperties(recommendUpgradeDTO, AcRecommendUpgrade.class)));
        } catch (Exception e) {
            return R.fail();
        }
    }

}
