package com.fs.his.strategy.impl;

import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.his.domain.FsPackageOrder;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserRewards;
import com.fs.his.enums.ActivityTypeEnum;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.param.FsPackageOrderAddRewardsParam;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.his.strategy.RewardResult;
import com.fs.his.strategy.RewardStrategy;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.param.FsStoreOrderCreateParamAppReward;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WatchCourseProductStrategy  implements RewardStrategy {

    @Autowired
    private FsUserMapper fsUserMapper;

    @Autowired
    private IFsPackageOrderService packageOrderService;

    @Autowired
    private IFsStoreOrderScrmService storeOrderScrmService;
    @Override
    public RewardResult process(FsUserRewards reward) {
        log.info("处理看课商品发放: rewardId={}", reward.getId());
        try {
            FsUser fsUser = fsUserMapper.selectFsUserById(reward.getFsUserId());
            if (fsUser == null) {
                return RewardResult.fail("用户不存在");
            }
            if (reward.getAddressId() == null) {
                return RewardResult.fail("请选择收货地址");
            }
            //互联网医院和商城要调用不同的订单创建逻辑
            R result;
            String orderCode;
            if (reward.getProductType() == 1) {
                FsPackageOrderAddRewardsParam param = buildOrderCreateParam(reward, fsUser);
                //创建套餐包订单
                result = packageOrderService.addPackageOrderForAppRewards(param);
                if (result.isSuccess()) {
                    Object orderObj = result.get("order");
                    if (orderObj instanceof FsPackageOrder) { // 安全检查
                        FsPackageOrder order = (FsPackageOrder) orderObj;
                        orderCode = order.getOrderSn();
                        log.info("套餐包订单创建成功: orderId={}, orderCode={}", order.getOrderId(), orderCode);
                    } else {
                        log.error("套餐包订单创建失败: 返回数据类型错误");
                        return RewardResult.fail("订单创建失败：返回数据类型错误");
                    }
                } else {
                    log.error("套餐包订单创建失败: {}", result.getMsg());
                    return RewardResult.fail("订单创建失败：" + result.getMsg());
                }
            } else {
                //创建商城订单
                FsStoreOrderCreateParamAppReward storeAppReward = buildStoreOrderCreateParam(reward, fsUser);
                result = storeOrderScrmService.createOrderScrmForAppRewards(reward.getFsUserId(), storeAppReward);
                if (result.isSuccess()) {
                    Object orderObj = result.get("order");
                    if (orderObj instanceof FsStoreOrderScrm) {
                        FsStoreOrderScrm order = (FsStoreOrderScrm) orderObj; //类型转换
                        orderCode = order.getOrderCode();
                        log.info("商城订单创建成功: orderId={}, orderCode={}", order.getId(), orderCode);
                    } else {
                        log.error("商城订单创建失败: 返回数据类型错误");
                        return RewardResult.fail("订单创建失败：返回数据类型错误");
                    }
                } else {
                    log.error("商城订单创建失败: {}", result.getMsg());
                    return RewardResult.fail("订单创建失败：" + result.getMsg());
                }
            }
            return RewardResult.success(orderCode);

        } catch (Exception e) {
            log.error("商品订单创建异常", e);
            throw new RuntimeException("看课奖励商品订单创建失败", e);
        }
    }

    /**
     * 构建创建订单参数
     */
    private FsPackageOrderAddRewardsParam buildOrderCreateParam(FsUserRewards reward, FsUser fsUser) {
        FsPackageOrderAddRewardsParam param = new FsPackageOrderAddRewardsParam();
        param.setCompanyId(reward.getCompanyId());
        param.setCompanyUserId(reward.getCompanyUserId());
        param.setUserId(fsUser.getUserId());
        param.setAddressId(reward.getAddressId());
        param.setPackageId(reward.getGoodsId());
        param.setPayType(1);//全款
        param.setAppRewardFlag(2);//2:看课奖品订单
        return param;
    }
    private FsStoreOrderCreateParamAppReward buildStoreOrderCreateParam(FsUserRewards reward, FsUser fsUser) {
        FsStoreOrderCreateParamAppReward param = new FsStoreOrderCreateParamAppReward();
        param.setAppRewardFlag(2);//2:看课奖品订单
        param.setPayType("1");//全款
        param.setCompanyId(reward.getCompanyId());
        param.setCompanyUserId(reward.getCompanyUserId());
        param.setUserId(fsUser.getUserId());
        param.setAddressId(reward.getAddressId());
        param.setGoodsId(reward.getGoodsId());
        return param;
    }

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

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