package com.gree.mall.manager.controller.material.vender;

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.material.vender.WebsitVenderVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.vender.VenderLogic;
import com.gree.mall.manager.plus.entity.WebsitVender;
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 = "供应商管理API", tags ={"供应商管理API"} )
@RequestMapping(value = "/vender", produces = "application/json; charset=utf-8")
public class VenderController {

    @Resource
    VenderLogic venderLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "供应商管理-列表")
    public ResponseHelper<IPage<WebsitVenderVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitVenderVO> page = venderLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitVenderVO>() {});
    }

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

    @PostMapping("/detail")
    @ApiOperation(value = "供应商管理-详情")
    public ResponseHelper<WebsitVender> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(venderLogic.detail(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "供应商管理-添加")
    public ResponseHelper add(
            @RequestBody WebsitVender bean
    ) throws Exception {
        venderLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "供应商管理-编辑")
    public ResponseHelper edit(
            @RequestBody WebsitVender bean
    ) throws Exception {
        venderLogic.edit(bean);
        return ResponseHelper.success();
    }
}
