package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
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.BsUserSpreadAccount;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.gas.BsGasOrderSpreadService;
import com.hfkj.service.spread.BsUserSpreadAccountRecordService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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: BsUserSpreadAccountController
 * @author: HuRui
 * @date: 2025/3/31
 **/
@Controller
@RequestMapping(value = "/userSpreadAccount")
@Api(value = "个人推广账户业务")
public class BsUserSpreadAccountController {
    private static Logger log = LoggerFactory.getLogger(BsUserSpreadAccountController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsUserSpreadAccountService userSpreadAccountService;
    @Resource
    private BsGasOrderSpreadService gasOrderSpreadService;
    @Resource
    private BsUserSpreadAccountRecordService userSpreadAccountRecordService;

    @RequestMapping(value = "/getAccount", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询余额账户")
    public ResponseData getAccount() {
        try {
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询账户
            BsUserSpreadAccount userSpreadAccount = userSpreadAccountService.getDetailByUser(userSessionObject.getUser().getId());
            if (userSpreadAccount == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            HashMap<String,Object> map = new HashMap<>();
            // 账户余额
            map.put("amount", userSpreadAccount.getAmount());
            map.put("cashOutAmount", userSpreadAccount.getCashOutAmount());
            map.put("notCashOutAmount", gasOrderSpreadService.getNotCashOutAmount(userSpreadAccount.getUserId()));
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "提现")
    public ResponseData withdraw(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getBigDecimal("amount") == null
                    || StringUtils.isBlank(body.getString("openId"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(userSpreadAccountService.withdraw(userSessionObject.getUser().getId(),body.getString("openId"), body.getBigDecimal("amount")));

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

    @RequestMapping(value = "/cancelWithdraw", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "撤销提现")
    public ResponseData cancelWithdraw(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("orderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 撤销提现
            userSpreadAccountService.cancelWithdraw(body.getString("orderNo"));
            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询记录列表")
    public ResponseData getRecordList(@RequestParam(value = "type", required = false) Integer type,
                                      @RequestParam(value = "sourceType", required = false) Integer sourceType,
                                      @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("userId", userSessionObject.getUser().getId());
            param.put("type", type);
            param.put("sourceType", sourceType);

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

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