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.WebsitVenderGoodsBean;
import com.gree.mall.manager.bean.material.vender.WebsitVenderGoodsRelaVO;
import com.gree.mall.manager.bean.material.vender.WebsitVenderGoodsVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.vender.VenderGoodsLogic;
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;
import java.util.List;

@Slf4j
@RestController
@Api(value = "供应商商品管理API", tags ={"供应商商品管理API"} )
@RequestMapping(value = "/vender/goods", produces = "application/json; charset=utf-8")
public class VenderGoodsController {

    @Resource
    VenderGoodsLogic venderGoodsLogic;

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

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

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

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

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

    @PostMapping("/confirm")
    @ApiOperation(value = "供应商供应商品-审核")
    public ResponseHelper confirm(
            @RequestBody WebsitVenderGoodsBean bean
    ) throws Exception {
        venderGoodsLogic.confirm(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "供应商供应商品-批量删除")
    public ResponseHelper del(
            @RequestBody List<String> ids
    ) throws Exception {
        venderGoodsLogic.del(ids);
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/rela/list")
    @ApiOperation(value = "供应商商品关系-列表")
    public ResponseHelper<IPage<WebsitVenderGoodsRelaVO>> ralaPage(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitVenderGoodsRelaVO> page = venderGoodsLogic.ralaPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitVenderGoodsRelaVO>() {});
    }

    @PostMapping("/rela/list/export")
    @ApiOperation("供应商商品关系-导出")
    public void ralaListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitVenderGoodsRelaVO> baseVOIPage = venderGoodsLogic.ralaPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }
}
