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

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.listvo.material.WebsitStockAccPVO;
import com.gree.mall.manager.bean.listvo.material.WebsitStockAccVO;
import com.gree.mall.manager.bean.listvo.material.WebsitStockPVO;
import com.gree.mall.manager.bean.listvo.material.WebsitStockVO;
import com.gree.mall.manager.bean.material.base.MaterialCategoryVO;
import com.gree.mall.manager.bean.material.base.WebsitGoodsMVO;
import com.gree.mall.manager.enums.material.WebsitGoodsTypeEnum;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.stock.WebsitStockLogic;
import com.gree.mall.manager.plus.entity.WebsitStockAcc;
import com.gree.mall.manager.utils.excel.ExcelUtils;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "库存管理-网点", tags ={"库存管理-网点"} )
@Validated
@RequestMapping(value = "/websit/stock", produces = "application/json; charset=utf-8")
public class WebsitStockController {

    @Autowired
    WebsitStockLogic websitStockLogic;

    @ZfireList
    @PostMapping("list")
    @ApiOperation(value = "辅材网点库存-列表")
    public ResponseHelper<IPage<WebsitStockVO>> websitStockList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitStockVO> page = websitStockLogic.websitStockList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitStockVO>() {});
    }

    @ZfireList
    @PostMapping("listP")
    @ApiOperation(value = "配件网点库存-列表")
    public ResponseHelper<IPage<WebsitStockPVO>> websitStockPList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitStockPVO> page = websitStockLogic.websitStockPList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitStockPVO>() {});
    }



    @PostMapping("list/export")
    @ApiOperation("辅材网点库存-导出")
    public void mListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitStockVO> page = websitStockLogic.websitStockList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("listP/export")
    @ApiOperation("配件网点库存-导出")
    public void pListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitStockPVO> page = websitStockLogic.websitStockPList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }


    @PostMapping("importM")
    @ApiOperation("辅材网点库存-导入(模板名称：辅材网点库存.xlsx)")
    public ResponseHelper importData(MultipartFile file) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        websitStockLogic.importData(objects, WebsitGoodsTypeEnum.M);
        return ResponseHelper.success();
    }


    @PostMapping("importP")
    @ApiOperation("配件网点库存-导入(模板名称：配件网点库存.xlsx)")
    public ResponseHelper importDataP(MultipartFile file) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        websitStockLogic.importData(objects, WebsitGoodsTypeEnum.P);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("acc/list")
    @ApiOperation(value = "辅材库存明细-列表")
    public ResponseHelper<IPage<WebsitStockAccVO>> websitStockAccList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitStockAccVO> page = websitStockLogic.websitStockAccList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitStockAccVO>() {});
    }

    @PostMapping("acc/list/export")
    @ApiOperation("辅材网点库存-导出")
    public void accListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitStockAccVO> page = websitStockLogic.websitStockAccList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }



    @ZfireList
    @PostMapping("acc/listP")
    @ApiOperation(value = "配件库存明细-列表")
    public ResponseHelper<IPage<WebsitStockAccPVO>> websitStockAccPList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitStockAccPVO> page = websitStockLogic.websitStockAccPList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitStockAccVO>() {});
    }

    @PostMapping("acc/listP/export")
    @ApiOperation("配件网点库存-导出")
    public void accListPExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitStockAccPVO> page = websitStockLogic.websitStockAccPList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }



}
