package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.model.ResponseData;
import com.hfkj.service.merchant.BsMerchantQrCodeService;
import com.hfkj.sysenum.merchant.MerchantQrCodeStatusEnum;
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
@RequestMapping(value = "/merchantQrCode")
@Api(value = "商户二维码管理")
public class BsMerchantQrCodeController {

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

    @Resource
    private BsMerchantQrCodeService merchantQrCodeService;

    @RequestMapping(value = "/restore", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "恢复")
    public ResponseData restore(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("qrCodeId") == null) {
                log.error("BsMerchantQrCodeController --> restore() error!", "请求参数校验失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            merchantQrCodeService.updateQrCodeStatus(body.getLong("qrCodeId"), MerchantQrCodeStatusEnum.status1);

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

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

    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "禁用")
    public ResponseData disable(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("qrCodeId") == null) {
                log.error("BsMerchantQrCodeController --> disable() error!", "请求参数校验失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            merchantQrCodeService.updateQrCodeStatus(body.getLong("qrCodeId"), MerchantQrCodeStatusEnum.status2);

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

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

    @RequestMapping(value = "/queryQrCode", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户二维码详情")
    public ResponseData queryQrCode(@RequestParam(value = "qrCodeId", required = true) Long qrCodeId) {
        try {

            return ResponseMsgUtil.success(merchantQrCodeService.getMerQrCode(qrCodeId));

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

    @RequestMapping(value = "/queryQrCodeList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户二维码列表")
    public ResponseData queryQrCodeList(@RequestParam(value = "merNo", required = true) String merNo) {
        try {
            return ResponseMsgUtil.success(merchantQrCodeService.getMerQrCodeList(merNo));

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



}
