package com.gree.mall.manager.controller.admin;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigBean;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigVO;
import com.gree.mall.manager.bean.admin.AdminCompanyVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.admin.AdminCompanyPayConfigLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(value = "商户管理", tags = {"商户管理API"})
@RequestMapping(value = "/admin/company", produces = "application/json; charset=utf-8")
public class AdminCompanyPayConfigController {

    @Resource
    AdminCompanyPayConfigLogic adminCompanyPayConfigLogic;

    @ZfireList
    @PostMapping("/mch/list")
    @ApiOperation(value = "支付商户-列表")
    public ResponseHelper<IPage<AdminCompanyPayConfigVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<AdminCompanyPayConfigVO> page = adminCompanyPayConfigLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<AdminCompanyPayConfigVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("支付商户-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<AdminCompanyPayConfigVO> baseVOIPage = adminCompanyPayConfigLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "支付商户-详情")
    public ResponseHelper<AdminCompanyPayConfigBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        AdminCompanyPayConfigBean bean = adminCompanyPayConfigLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "支付商户-添加")
    public ResponseHelper add(
            @RequestBody AdminCompanyPayConfigBean bean
    ) throws Exception {
        adminCompanyPayConfigLogic.add(bean);

        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "支付商户-编辑")
    public ResponseHelper edit(
            @RequestBody AdminCompanyPayConfigBean bean
    ) throws Exception {
        adminCompanyPayConfigLogic.edit(bean);
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "商户列表")
    public ResponseHelper<IPage<AdminCompanyVO>> adminCompanyPage(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<AdminCompanyVO> page = adminCompanyPayConfigLogic.adminCompanyPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<AdminCompanyVO>() {});
    }
}
