package com.hzs.activity.base.provider;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hzs.activity.base.IActivityApprovalServiceApi;
import com.hzs.activity.base.service.IAcApprovalLogService;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.consume.service.IAcConsumeRuleConfigService;
import com.hzs.activity.consume.service.IAcMemberConsumeRuleService;
import com.hzs.activity.draw.param.PurchasedTimesParam;
import com.hzs.activity.draw.service.IAcDrawGiftRuleConfigService;
import com.hzs.activity.draw.service.IAcDrawRewardNumService;
import com.hzs.activity.pick.param.AcPickSaveDetailParam;
import com.hzs.activity.pick.service.IAcPickService;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.activity.recommend.service.IAcRepurCouponsRuleConfigService;
import com.hzs.activity.wares.service.IAcWaresRuleConfigService;
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 IAcDrawRewardNumService drawRewardNumService;
    @Autowired
    private IAcConsumeRuleConfigService consumeRuleConfigService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IAcWaresRuleConfigService waresRuleConfigService;
    @Autowired
    private IAcDrawGiftRuleConfigService drawGiftConfigService;
    @Autowired
    private IAcPickService iAcPickService;
    @Autowired
    private IAcApprovalLogService iAcApprovalLogService;
    @Autowired
    private IAcRepurCouponsRuleConfigService acRepurCouponsRuleConfigService;
    @Autowired
    private IAcMemberConsumeRuleService iAcMemberConsumeRuleService;

    @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 PICK_ADD_DEL:
                        // 提货充值删除
                        str = iAcPickService.delpickApprovalAgree(approvalBusinessResultDTO);
                        break;
                    // 直推赠送
                    case DIRECT_PUSH_GIFT:
                        str = acRepurCouponsRuleConfigService.approvalAgree(approvalBusinessResultDTO);
                        break;
                    case LOTTERY_DRAW:
                        str = drawGiftConfigService.approvalAgree(approvalBusinessResultDTO);
                        break;
                    case TOURISM_SAVE:
                    case TOURISM_UPDATE:
                    case TOURISM_DELETE:
                        str = activityService.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 MEMBER_CONSUME_GIFT_ADD:
                    case MEMBER_CONSUME_GIFT_UPDATE:
                    case MEMBER_CONSUME_GIFT_REMOVE:
                        // 会员消费赠送添加/修改/删除
                        iAcMemberConsumeRuleService.approvalAgree(approvalBusinessResultDTO);
                        break;
                    case LOTTERY_DRAW_ADD:
                    case LOTTERY_DRAW_UPDATE:
                    case LOTTERY_DRAW_REMOVE:
                        // 抽奖配置添加/修改/删除
                        drawGiftConfigService.approvalAgree(approvalBusinessResultDTO);
                        break;
                    case PUR_CHASED_TIMES:
                        // 充值已购买次数
                        drawRewardNumService.rechargePurchasedTimes(approvalBusinessResultDTO);
                        break;
                    case UN_PUR_CHASED_TIMES:
                        // 充值未购买次数
                        drawRewardNumService.unRechargePurchasedTimes(approvalBusinessResultDTO);
                        break;
                    default:
                }
            } 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 PRODUCT_GIFT_ADD:
                    case PRODUCT_GIFT_UPDATE:
                    case PRODUCT_GIFT_REMOVE:
                        // 商品赠送添加
                        str = waresRuleConfigService.approvalReject(approvalBusinessResultDTO);
                        break;
                    default:
                }
            }
            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:
                case PICK_ADD_DEL:
                    // 提货充值
                    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());
                        if (afterData.getPkProduct() != null) {
                            afterData.setProductName(iProductServiceApi.getProduct(afterData.getPkProduct()).getData().getProductName() + "(" + afterData.getSpecsName() + ")");
                        }
                        list.add(afterData);
                    }
                    return R.ok(list);
                // 直推赠送
                case DIRECT_PUSH_GIFT:
                case TOURISM_UPDATE:
                case TOURISM_DELETE:
                case TOURISM_SAVE:
                case LOTTERY_DRAW:
                    // 抽奖
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                case PUR_CHASED_TIMES:
                case UN_PUR_CHASED_TIMES:
                    // 充值已购买次数
                    return R.ok(getRecommendApprovalDataInfo(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 LOTTERY_DRAW_ADD:
                case LOTTERY_DRAW_UPDATE:
                case LOTTERY_DRAW_REMOVE:
                case MEMBER_CONSUME_GIFT_ADD:
                case MEMBER_CONSUME_GIFT_UPDATE:
                case MEMBER_CONSUME_GIFT_REMOVE:
                    return R.ok(getRecommendApprovalData(acApprovalLogList.get(0)));
                default:
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }


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

    }

    private PurchasedTimesParam getRecommendApprovalDataInfo(AcApprovalLog acApprovalLog) {
        String data = acApprovalLog.getAfterData();
        if (StringUtils.isNotBlank(data)) {
            JSONObject js = new JSONObject(data);
            String memberCode = js.getStr("memberCode");
            String memberName = js.getStr("memberName");
            Integer num = js.getInt("num");
            PurchasedTimesParam purchasedTimesParam = new PurchasedTimesParam();
            purchasedTimesParam.setMemberCode(memberCode);
            purchasedTimesParam.setMemberName(memberName);
            purchasedTimesParam.setNum(num);
            return purchasedTimesParam;
        }
        return null;
    }

}
