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.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMer;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsMerService;
import com.hfkj.service.rebate.BsRebateMerAccountRecordService;
import com.hfkj.service.rebate.BsRebateMerAccountService;
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;

@Controller
@Api(value = "返利金商户账户")
@RequestMapping(value = "/rebateMerAccount")
public class BsRebateMerAccountController {

    private static Logger log = LoggerFactory.getLogger(BsRebateMerAccountController.class);

    @Resource
    private BsRebateMerAccountService rebateMerAccountService;
    @Resource
    private BsRebateMerAccountRecordService rebateMerAccountRecordService;
    @Resource
    private BsMerService merService;

    @RequestMapping(value="/queryAccount",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取账户")
    public ResponseData queryAccount(@RequestParam(name = "merNo", required = true) String merNo) {
        try {
            // 查询商户
            BsMer mer = merService.getMer(merNo);
            if (mer == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户");
            }

            return ResponseMsgUtil.success(rebateMerAccountService.getAccountByMerId(mer.getId()));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryAccountRecordList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取账户交易记录")
    public ResponseData queryAccountRecordList(@RequestParam(name = "accountNumber", required = true) Long accountNumber,
                                               @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                               @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(rebateMerAccountRecordService.getRecordList(accountNumber)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryAccountRecordDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取账户交易记录详情")
    public ResponseData queryAccountRecordDetail(@RequestParam(name = "tradeNumber", required = true) String tradeNumber) {
        try {

            return ResponseMsgUtil.success(rebateMerAccountRecordService.getDetailByTradeNumber(tradeNumber));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
