package com.yonge.cooleshow.portal.controller.open;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.portal.dto.ShareProductVo;
import com.yonge.cooleshow.portal.dto.ShareProfitParam;
import com.yonge.cooleshow.portal.service.PmsPortalProductService;
import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/open")
@Api(tags = "开放权限接口-分享")
public class OpenShareController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(OpenShareController.class);

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private PmsPortalProductService portalProductService;


    @ApiOperation(value = "老师商品分享分润")
    @PostMapping(value="/productProfit")
    @ResponseBody
    public CommonResult<ShareProductVo> shareProductProfit(@RequestBody ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return CommonResult.failed("用户信息获取失败");
        }
        HttpResponseResult<String> client = sysUserFeignService.client();
        if (client.getStatus()) {
            if (!StringUtil.isEmpty(client.getData()) && "teacher".equals(client.getData())) {
                return CommonResult.success(portalProductService.shareProductProfit(sysUser, param.getBizId()));
            }
        } else {
            return CommonResult.success(portalProductService.shareProductProfit(sysUser, param.getBizId()));
        }
        return CommonResult.failed("分享功能暂未开放");
    }
}
