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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigVO;
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.plus.entity.AdminCompanyWechatPayConfig;
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 org.springframework.web.bind.annotation.*;

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

@RestController
@Api(value = "商户支付配置", tags = {"商户支付配置"})
@RequestMapping(value = "/admin/company/pay/config", produces = "application/json; charset=utf-8")
public class AdminCompanyPayConfigController {

    @Resource
    AdminCompanyPayConfigLogic adminCompanyPayConfigLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<Page<AdminCompanyPayConfigVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<AdminCompanyPayConfigVO> vo = adminCompanyPayConfigLogic.list(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<AdminCompanyPayConfigVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<AdminCompanyPayConfigVO> page = adminCompanyPayConfigLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

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

    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public ResponseHelper save(
            @RequestBody AdminCompanyWechatPayConfig bean
    ) throws Exception {

        adminCompanyPayConfigLogic.save(bean);

        return ResponseHelper.success();
    }
}
