package com.cweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsUserSpreadPower;
import com.hfkj.entity.BsUserSpreadRel;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.gas.BsGasService;
import com.hfkj.service.spread.BsUserSpreadOilChannelService;
import com.hfkj.service.spread.BsUserSpreadPowerService;
import com.hfkj.service.spread.BsUserSpreadRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsUserSpreadController
 * @author: HuRui
 * @date: 2025/3/31
 **/
@Controller
@RequestMapping(value = "/userSpread")
@Api(value = "个人推广业务")
public class BsUserSpreadController {
    private static Logger log = LoggerFactory.getLogger(BsUserSpreadController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsUserSpreadPowerService userSpreadPowerService;
    @Resource
    private BsUserSpreadRelService userSpreadRelService;
    @Resource
    private BsUserSpreadOilChannelService userSpreadOilChannelService;

    @RequestMapping(value = "/openPower", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "开通权限")
    public ResponseData openPower() {
        try {
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            userSpreadPowerService.open(userSessionObject.getUser().getId());

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getPower", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "个人推广权限权限")
    public ResponseData getPower() {
        try {
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            BsUserSpreadPower data = userSpreadPowerService.getDetailByUser(userSessionObject.getUser().getId());
            return ResponseMsgUtil.success(data!=null?true:false);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getTeamList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询团队")
    public ResponseData getTeamList(@RequestParam(value = "phone", required = false) String phone,
                                    @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                    @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("parentUserId", userSessionObject.getUser().getId());
            param.put("phone", phone);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(userSpreadRelService.getTeamList(param)));

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

}
