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

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.base.WebsitGoodsBean;
import com.gree.mall.manager.bean.material.base.WebsitGoodsMVO;
import com.gree.mall.manager.bean.material.base.WebsitGoodsPVO;
import com.gree.mall.manager.enums.material.StateEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.base.WebsitGoodsLogic;
import com.gree.mall.manager.zfire.bean.WebsitGoodsParamBean;
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 org.springframework.web.multipart.MultipartFile;

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 = "/websit/goods", produces = "application/json; charset=utf-8")
public class WebsitGoodsController {

    @Resource
    WebsitGoodsLogic websitGoodsLogic;

    @ZfireList
    @PostMapping("/m/list")
    @ApiOperation(value = "网点辅材管理-列表")
    public ResponseHelper<IPage<WebsitGoodsMVO>> mPage(
            @RequestBody WebsitGoodsParamBean zfireParamBean
    ) {
        IPage<WebsitGoodsMVO> page = websitGoodsLogic.mPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitGoodsMVO>() {});
    }

    @PostMapping("m/list/export")
    @ApiOperation("网点辅材管理-导出")
    public void mListExport(
            @RequestBody WebsitGoodsParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitGoodsMVO> baseVOIPage = websitGoodsLogic.mPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/p/list")
    @ApiOperation(value = "网点配件管理-列表")
    public ResponseHelper<IPage<WebsitGoodsPVO>> pPage(
            @RequestBody WebsitGoodsParamBean zfireParamBean
    ) {
        IPage<WebsitGoodsPVO> page = websitGoodsLogic.pPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitGoodsPVO>() {});
    }

    @PostMapping("p/list/export")
    @ApiOperation("网点配件管理-导出")
    public void pListExport(
            @RequestBody WebsitGoodsParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitGoodsPVO> baseVOIPage = websitGoodsLogic.pPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "网点商品管理-详情")
    public ResponseHelper<WebsitGoodsBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        WebsitGoodsBean goods = websitGoodsLogic.detail(id);
        return ResponseHelper.success(goods);
    }

    @PostMapping("/add")
    @ApiOperation(value = "网点商品管理-添加")
    public ResponseHelper<WebsitGoodsBean> add(
            HttpServletRequest request,
            @RequestBody WebsitGoodsBean bean
    ) throws Exception {
        websitGoodsLogic.add(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "网点商品管理-编辑")
    public ResponseHelper<WebsitGoodsBean> edit(
            HttpServletRequest request,
            @RequestBody WebsitGoodsBean bean
    ) throws Exception {
        websitGoodsLogic.edit(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/status")
    @ApiOperation(value = "网点商品管理-批量更新状态")
    public ResponseHelper batchUpdateStatus(
            @ApiParam(value = "goodsId", required = true) @RequestParam List<String> ids,
            @ApiParam(value = "ON=有效 OFF=无效", required = true) @RequestParam StateEnum stateEnum
    ) throws Exception {
        websitGoodsLogic.batchUpdateStatus(ids, stateEnum);
        return ResponseHelper.success();
    }

    @PostMapping("/m/import")
    @ApiOperation(value = "网点商品管理-导入(下载模板:辅材模板.xlsx)")
    public ResponseHelper<String> materialImportData (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(websitGoodsLogic.materialImportData(file));
    }

    @PostMapping("/p/import")
    @ApiOperation(value = "网点商品管理-导入(下载模板:配件模板（配件物料）.xlsx)")
    public ResponseHelper<String> partsImportData (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(websitGoodsLogic.partsImportData(file));
    }

    @PostMapping("/p/import2")
    @ApiOperation(value = "网点商品管理-导入(下载模板:配件模板（服务收费）.xlsx)")
    public ResponseHelper<String> partsImportData2 (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(websitGoodsLogic.partsImportData2(file));
    }


}
