package com.fs.his.strategy.impl;


import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.core.config.WxOpenProperties;
import com.fs.his.domain.FsAppRewardCourseRedPacketLog;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserRewards;
import com.fs.his.enums.ActivityTypeEnum;
import com.fs.his.mapper.FsAppRewardCourseRedPacketLogMapper;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.param.WxSendRedPacketParam;
import com.fs.his.service.IFsStorePaymentService;
import com.fs.his.strategy.RewardResult;
import com.fs.his.strategy.RewardStrategy;
import com.github.binarywang.wxpay.bean.transfer.TransferBillsResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class FirstLoginRedPacketStrategy implements RewardStrategy {

    @Autowired
    private FsUserMapper fsUserMapper;

    @Autowired
    private IFsStorePaymentService fsStorePaymentService;
    @Autowired
    private WxOpenProperties openProperties;

    @Autowired
    private FsAppRewardCourseRedPacketLogMapper appRewardCourseRedPacketLogMapper;
    @Override
    public RewardResult process(FsUserRewards reward) {
        log.info("处理首次登录-弹窗红包-发放: rewardId={}", reward.getId());
        try {
            FsUser fsUser=fsUserMapper.selectFsUserById(reward.getFsUserId());
            if (fsUser!=null){
                if (fsUser.getAppOpenId()==null) {
                    return RewardResult.fail(900,"请绑定微信后再领取");
                }
                WxSendRedPacketParam param=buildRedPacketParam(reward, fsUser);
                //调用发送红包接口逻辑
                R sendRedPacket = fsStorePaymentService.sendRedPacketAppReward(param);
                if (sendRedPacket.get("code").equals(200)) {
                    FsAppRewardCourseRedPacketLog record=new FsAppRewardCourseRedPacketLog();
                    TransferBillsResult transferBillsResult;
                    if (sendRedPacket.get("isNew").equals(1)) {
                        transferBillsResult = (TransferBillsResult) sendRedPacket.get("data");
                        record.setOutBatchNo(transferBillsResult.getOutBillNo());
                        record.setBatchId(transferBillsResult.getTransferBillNo());
                    } else {
                        record.setOutBatchNo(sendRedPacket.get("orderCode").toString());
                        record.setBatchId(sendRedPacket.get("batchId").toString());
                    }
                    record.setUpdateTime(new Date());
                    Object aPackage = sendRedPacket.get("packageInfo");
                    if (aPackage != null) {
                        record.setPackageInfo(aPackage.toString());
                    }
                    Object mchIdObj = sendRedPacket.get("mchId");
                    if (mchIdObj != null) {
                        record.setMchId(mchIdObj.toString());
                    }
                    record.setUserId(fsUser.getUserId());
                    record.setActivityType(ActivityTypeEnum.FIRST_LOGIN.getCode());
                    record.setAmount(reward.getRewardAmount());
                    record.setVideoId(reward.getGoodsId());
                    record.setCreateTime(DateUtils.getNowDate());
                    //保存App红包发放记录
                    appRewardCourseRedPacketLogMapper.insert(record);
                    return RewardResult.successWithExt(sendRedPacket);
                } else {
                    log.error("首次登录-弹窗红包-发放失败: {}", sendRedPacket);
                    return RewardResult.fail("首次登录-弹窗红包-发放失败，请稍后再试");
                }
            }
            return RewardResult.fail("首次登录-弹窗红包-发放失败，用户不存在");
        } catch (Exception e) {
            throw new RuntimeException("首次登录-弹窗红包-发放失败", e);
        }
    }

    /**
     * 构建红包参数
     */
    private WxSendRedPacketParam buildRedPacketParam(FsUserRewards reward, FsUser fsUser) {
        WxSendRedPacketParam param = new WxSendRedPacketParam();
        param.setAppId(openProperties.getAppId());
        param.setOpenId(fsUser.getAppOpenId());
        param.setAmount(reward.getRewardAmount());
        param.setRedPacketMode(1);//总公司
        param.setSource(3);//3:app端
        param.setUser(fsUser);
        return param;
    }

    @Override
    public String getActivityType() {
        return ActivityTypeEnum.FIRST_LOGIN.getCode();
    }

    @Override
    public Integer getRewardType() {
        return 1;
    }

}
