package com.hzs.activity.base.provider;

import cn.hutool.json.JSONUtil;
import com.hzs.activity.base.IActivityApprovalServiceApi;
import com.hzs.activity.base.service.IAcApprovalLogService;
import com.hzs.activity.consume.service.IAcConsumeRuleConfigService;
import com.hzs.activity.draw.service.IAcDrawGiftRuleConfigService;
import com.hzs.activity.fest.service.IAcFestivalRuleConfigService;
import com.hzs.activity.other.service.IAcOtherPriceConfigService;
import com.hzs.activity.people.param.AcBaseConfigParam;
import com.hzs.activity.people.service.IAcNewPeopleRuleConfigService;

import com.hzs.activity.pick.param.AcPickSaveDetailParam;
import com.hzs.activity.pick.service.IAcPickService;
import com.hzs.activity.policy.service.IAcPolicyConfigService;
import com.hzs.activity.recoilGive.service.IAcRecoilGiveConfigService;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.activity.recommend.service.IAcRepurCouponsRuleConfigService;
import com.hzs.activity.wares.service.IAcWaresRuleConfigService;
import com.hzs.activity.work.service.IAcSuperWorkRuleConfigService;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.exception.base.BaseException;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.activity.base.AcApprovalLog;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动审批相关服务
 */
@DubboService
public class ActivityApprovalProvider implements IActivityApprovalServiceApi {
    @Autowired
    private IAcRecoilGiveConfigService recoilGiveConfigService;
    @Autowired
    private IAcPolicyConfigService policyConfigService;
    @Autowired
    private IAcDrawGiftRuleConfigService drawGiftConfigService;
    @Autowired
    private IAcPickService iAcPickService;
    @Autowired
    private IAcApprovalLogService iAcApprovalLogService;
    @Autowired
    private IAcNewPeopleRuleConfigService iAcNewPeopleRuleConfigService;
    @Autowired
    private IAcFestivalRuleConfigService iAcFestivalRuleConfigService;
    @Autowired
    private IAcSuperWorkRuleConfigService iAcSuperWorkRuleConfigService;
    @Autowired
    private IAcRepurCouponsRuleConfigService acRepurCouponsRuleConfigService;
    @Autowired
    private IAcOtherPriceConfigService otherPriceConfigService;
    @Autowired
    private IAcWaresRuleConfigService waresRuleConfigService;
    @Autowired
    private IAcConsumeRuleConfigService consumeRuleConfigService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    IProductServiceApi iProductServiceApi;

    @Override
    public R<Boolean> approvalCallback(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        try {
            // 返回内容
            String str = null;
            if (EApproveStatus.FINISH.getValue() == approvalBusinessResultDTO.getEApproveStatus().getValue()) {
                // 通过处理
                switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                    case PICK_ADD:
                        // 提货充值
                        str = iAcPickService.pickApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case SUPE_RWORK:
                        // 超级工作室赠送
                        str = iAcSuperWorkRuleConfigService.approval(approvalBusinessResultDTO);
                        break;
                    case FESTIVAL:
                        // 节日赠送
                        str = iAcFestivalRuleConfigService.approval(approvalBusinessResultDTO);
                        break;
                    case NEW_PEOPLE:
                        // 新人礼包
                        str = iAcNewPeopleRuleConfigService.approval(approvalBusinessResultDTO);
                        break;
                    // 直推赠送
                    case DIRECT_PUSH_GIFT:
                        str = acRepurCouponsRuleConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    // 抽奖
                    case LOTTERY_DRAW:
                        str = drawGiftConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case POLICY_GIVE:
                        // 赠送政策币
                        policyConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case CAR_AWARD:
                        // 车奖
                        otherPriceConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case TOURISM_AWARD:
                        // 旅游奖
                        otherPriceConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case PRODUCT_GIFT_ADD:
                    case PRODUCT_GIFT_UPDATE:
                    case PRODUCT_GIFT_REMOVE:
                        // 商品赠送添加/修改/删除
                        str = waresRuleConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case CONSUME_GIFT_ADD:
                    case CONSUME_GIFT_UPDATE:
                    case CONSUME_GIFT_REMOVE:
                        // 消费赠送添加/修改/删除
                        consumeRuleConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                    case RECOIL_GIVE_ADD:
                    case RECOIL_GIVE_UPDATE:
                    case RECOIL_GIVE_REMOVE:
                        // 复消币赠送/添加/修改删除
                        recoilGiveConfigService.ApprovalAgree(approvalBusinessResultDTO);
                        break;
                }
            } else {
                // 驳回处理
                switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                    case PICK_ADD:
                        // 提货充值
                        str = iAcPickService.pickApprovalReject(approvalBusinessResultDTO);
                        break;
                    // 直推赠送
                    case DIRECT_PUSH_GIFT:
                        str = acRepurCouponsRuleConfigService.ApprovalReject(approvalBusinessResultDTO);
                        break;
                    case LOTTERY_DRAW:
                        str = acRepurCouponsRuleConfigService.ApprovalReject(approvalBusinessResultDTO);
                        break;
                    case POLICY_GIVE:
                        // 充值赠送政策币
                        policyConfigService.ApprovalReject(approvalBusinessResultDTO);
                    case CAR_AWARD:
                        // 车奖
                        otherPriceConfigService.ApprovalReject(approvalBusinessResultDTO);
                    case TOURISM_AWARD:
                        ///旅游奖
                        otherPriceConfigService.ApprovalReject(approvalBusinessResultDTO);
                    case PRODUCT_GIFT_ADD:
                    case PRODUCT_GIFT_UPDATE:
                    case PRODUCT_GIFT_REMOVE:
                        // 商品赠送添加
                        str = waresRuleConfigService.ApprovalReject(approvalBusinessResultDTO);
                        break;
                    case CONSUME_GIFT_ADD:
                    case CONSUME_GIFT_UPDATE:
                    case CONSUME_GIFT_REMOVE:
                        // 消费赠送添加/修改/删除
                        consumeRuleConfigService.ApprovalReject(approvalBusinessResultDTO);
                        break;
                    case RECOIL_GIVE_ADD:
                    case RECOIL_GIVE_UPDATE:
                    case RECOIL_GIVE_REMOVE:
                        // 复消币赠送/添加/修改删除
                        recoilGiveConfigService.ApprovalReject(approvalBusinessResultDTO);
                        break;
                }
            }
            if (StringUtils.isNotEmpty(str)) {
                // 返回有信息，说明有问题，抛出异常
                throw new BaseException(str);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    /**
     * @description: 活动类审批诉求查询
     * @author:
     * @date: 2023/4/25 16:55
     * @param: [eApprovalBusiness, businessCode, pkCountry]
     * @return: com.hzs.common.core.domain.R
     **/
    @Override
    public R queryApprovalData(EApprovalBusiness eApprovalBusiness, String businessCode, Integer pkCountry) {
        try {
            List<AcApprovalLog> acApprovalLogList = iAcApprovalLogService.queryApprovalLogList(eApprovalBusiness, businessCode, pkCountry);
            switch (eApprovalBusiness) {
                case PICK_ADD:
                    // 提货充值
                    List<AcPickSaveDetailParam> list = new ArrayList<>();
                    for (AcApprovalLog approvalLog : acApprovalLogList) {
                        AcPickSaveDetailParam afterData = JSONUtil.toBean(approvalLog.getAfterData(), AcPickSaveDetailParam.class);
                        afterData.setMemberName(iMemberServiceApi.getMember(afterData.getMemberCode()).getData().getMemberName());
                        afterData.setProductName(iProductServiceApi.getProduct(afterData.getPkProduct()).getData().getProductName() + "(" + afterData.getSpecsName() + ")");
                        list.add(afterData);
                    }
                    return R.ok(list);
                case SUPE_RWORK:
                    //超级工作室赠送
                case FESTIVAL:
                    //节日赠送
                case NEW_PEOPLE:
                    //新人礼包
                    AcBaseConfigParam nplData = JSONUtil.toBean(acApprovalLogList.get(0).getAfterData(), AcBaseConfigParam.class);
                    return R.ok(nplData);
                // 直推赠送
                case DIRECT_PUSH_GIFT:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                // 抽奖
                case LOTTERY_DRAW:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                // 政策币活动
                case POLICY_GIVE:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                // 旅游奖
                case TOURISM_AWARD:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                // 车奖
                case CAR_AWARD:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                case CONSUME_GIFT_ADD:
                case CONSUME_GIFT_UPDATE:
                case CONSUME_GIFT_REMOVE:
                case PRODUCT_GIFT_ADD:
                case PRODUCT_GIFT_UPDATE:
                case PRODUCT_GIFT_REMOVE:
                case TREE_RULE_PICK_SAVE:
                case TREE_RULE_PICK_UPDATE:
                case TREE_RULE_PICK_REMOVE:
                case RECOIL_GIVE_ADD:
                case RECOIL_GIVE_UPDATE:
                case RECOIL_GIVE_REMOVE:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));

            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }


    private AcRecommendParam getRecommendApprovalData(AcApprovalLog acApprovalLog) {
        String data = acApprovalLog.getAfterData();
        AcRecommendParam acRecommendParam = JSONUtil.toBean(data, AcRecommendParam.class);
        return acRecommendParam;

    }

}
