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.entity.BsMerchantChainBrand;
import com.hfkj.entity.BsMerchantChainBrandAccount;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountRecordService;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountService;
import com.hfkj.service.merchant.BsMerchantChainBrandService;
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: BsMerchantChainBrandController
 * @author: HuRui
 * @date: 2024/8/15
 **/
@Controller
@RequestMapping(value = "/merchantChainBrandAccount")
@Api(value = "商户连锁管理")
public class BsMerchantChainBrandAccountController {
    private static Logger log = LoggerFactory.getLogger(BsMerchantChainBrandAccountController.class);
    @Resource
    private BsMerchantChainBrandAccountService merchantChainBrandAccountService;
    @Resource
    private BsMerchantChainBrandAccountRecordService merchantChainBrandAccountRecordService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/getAccount", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询账户")
    public ResponseData getAccount(@RequestParam(value = "chainBrandId", required = true) Long chainBrandId) {
        try {
            // 查询账户
            return ResponseMsgUtil.success(merchantChainBrandAccountService.getAccount(chainBrandId));

        } catch (Exception e) {
            log.error("BsMerchantChainBrandAccountController --> 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("chainBrandId") == null
                    || body.getBigDecimal("amount") == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 账户充值
            merchantChainBrandAccountService.recharge(body.getLong("chainBrandId"), body.getBigDecimal("amount"));

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

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

    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "记录列表")
    public ResponseData getRecordList(@RequestParam(value = "chainBrandId", required = true) Long chainBrandId,
                                      @RequestParam(value = "merNo", required = false) String merNo,
                                      @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {

            // 查询账户
            BsMerchantChainBrandAccount account = merchantChainBrandAccountService.getAccount(chainBrandId);

            Map<String,Object> param = new HashMap<>();
            param.put("merChainBrandAccountId", account.getId());
            param.put("merNo", merNo);

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

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

}
