package com.bweb.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.BsAgentMerAccount;
import com.hfkj.entity.BsAgentStaff;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.agent.BsAgentMerAccountRecordService;
import com.hfkj.service.agent.BsAgentMerAccountService;
import com.hfkj.service.agent.BsAgentStaffService;
import com.hfkj.service.merchant.BsMerchantAccountRecordService;
import com.hfkj.service.merchant.BsMerchantAccountService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
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: BsMerchantAccountController
 * @author: HuRui
 * @date: 2024/7/2
 **/
@Controller
@RequestMapping(value = "/agentMerAccount")
@Api(value = "代理商商户账户")
public class BsAgentMerAccountController {
    private static Logger log = LoggerFactory.getLogger(BsAgentMerAccountController.class);
    @Resource
    private BsAgentMerAccountService agentMerAccountService;
    @Resource
    private BsAgentMerAccountRecordService agentMerAccountRecordService;
    @Resource
    private BsAgentStaffService agentStaffService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/getAccount", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询账户")
    public ResponseData getAccount(@RequestParam(value = "agentId", required = true) Long agentId,
                                   @RequestParam(value = "merNo", required = true) String merNo) {
        try {

            // 查询账户
            return ResponseMsgUtil.success(agentMerAccountService.getAccount(agentId, merNo));

        } catch (Exception e) {
            log.error("BsAgentMerAccountController --> getAccount() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "充值")
    public ResponseData recharge(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (SecUserObjectTypeEnum.type1.getCode() != userSession.getAccount().getObjectType()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            if (body == null
                    || body.getLong("agentId") == null
                    || StringUtils.isBlank(body.getString("merNo"))
                    || body.getBigDecimal("amount") == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 账户充值
            agentMerAccountService.recharge(body.getLong("agentId"), body.getString("merNo"), body.getBigDecimal("amount"));

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

        } catch (Exception e) {
            log.error("BsAgentMerAccountController --> recharge() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getMerList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public ResponseData getMerList(@RequestParam(value = "agentId", required = false) Long agentId,
                                   @RequestParam(value = "merNo", required = false) String merNo,
                                   @RequestParam(value = "merName", required = false) String merName,
                                   @RequestParam(value = "provinceCode", required = false) Long provinceCode,
                                   @RequestParam(value = "cityCode", required = false) Long cityCode,
                                   @RequestParam(value = "areaCode", required = false) Long areaCode,
                                   @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                   @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {

            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            Map<String,Object> param = new HashMap<>();
            if (SecUserObjectTypeEnum.type1.getCode() == userSession.getAccount().getObjectType()) {
                param.put("agentId", agentId);

            } else if (SecUserObjectTypeEnum.type4.getCode() == userSession.getAccount().getObjectType()) {
                param.put("agentId", userSession.getAccount().getObjectId());

            } else if (SecUserObjectTypeEnum.type5.getCode() == userSession.getAccount().getObjectType()) {
                // 查询代理商工作人员
                BsAgentStaff agentStaff = agentStaffService.getStaffDetailById(userSession.getAccount().getObjectId());
                if (agentStaff == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
                }
                param.put("agentId", agentStaff.getAgentId());
                param.put("agentStaffId", agentStaff.getId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("provinceCode", provinceCode);
            param.put("cityCode", cityCode);
            param.put("areaCode", areaCode);

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

        } catch (Exception e) {
            log.error("BsMerchantAccountController --> getRecordList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "记录列表")
    public ResponseData getRecordList(@RequestParam(value = "agentId", required = false) Long agentId,
                                      @RequestParam(value = "merNo", required = true) String merNo,
                                      @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (SecUserObjectTypeEnum.type1.getCode() == userSession.getAccount().getObjectType()) {

            } else if (SecUserObjectTypeEnum.type4.getCode() == userSession.getAccount().getObjectType()) {
                agentId = userSession.getAccount().getObjectId();;

            } else if (SecUserObjectTypeEnum.type5.getCode() == userSession.getAccount().getObjectType()) {
                // 查询代理业务员
                BsAgentStaff staff = agentStaffService.getStaffDetailById(userSession.getAccount().getObjectId());
                if (staff == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
                }
                agentId = staff.getAgentId();
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            // 查询账户
            BsAgentMerAccount account = agentMerAccountService.getAccount(agentId, merNo);
            if (account == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("agentMerAccountId", account.getId());

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

        } catch (Exception e) {
            log.error("BsAgentMerAccountController --> getRecordList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
