package com.zfire.mall.manager.controller.stock;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.invoice.StockStatusBean;
import com.zfire.mall.manager.bean.k3.StockPositionStock;
import com.zfire.mall.manager.bean.k3.StockPositionStockVO;
import com.zfire.mall.manager.bean.sale.SaleOrderBean;
import com.zfire.mall.manager.bean.stock.*;
import com.zfire.mall.manager.bean.supplier.KingDeeStockVO;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3TransferLogic;
import com.zfire.mall.manager.logic.stock.StockCommonLogic;
import com.zfire.mall.manager.plus.entity.StockCustomer;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "仓库库存管理", tags ={"仓库库存管理"} )
@RequestMapping(value = "/stock/manager", produces = "application/json; charset=utf-8")
public class StockController {

    @Autowired
    StockCommonLogic stockCommonLogic;

    @ApiOperation(value = "仓库库存列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<StockBean>> list(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "仓库名称集合") @RequestParam(required = false) String correspondNameS,
            @ApiParam(required = false, value = "仓位id") @RequestParam(required = false) String stockId,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "品类") @RequestParam(required = false) String mainNumber,
            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<StockBean> stockBeans = stockCommonLogic.list(correspondId,stockId,categoryId,materialOldNumber,materialName,materialNumber,specification,mainNumber,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }


    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("库存统计表-列表V2")
    public ResponseHelper<IPage<StockVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StockVO> list = stockCommonLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockVO>() {});
    }

    @PostMapping("/listV2/export")
    @ApiOperation("库存统计表-列表V2导出")
    public void listV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StockVO> list = stockCommonLogic.listV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }





    @ApiOperation(value = "经销商预留库存列表")
    @GetMapping("/listCustomer")
    public ResponseHelper<IPage<StockCustomerReserved>> listCustomer(
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<StockCustomerReserved> stockCustomerIPage = stockCommonLogic.listCustomer(categoryId,materialName,materialNumber,specification,customerName,customerNumber,pageNum,pageSize);
        return ResponseHelper.success(stockCustomerIPage);
    }




    @ZfireList
    @PostMapping("/listCustomerV2")
    @ApiOperation("经销商预留库存列表-列表V2")
    public ResponseHelper<IPage<StockCustomerReserved>> listCustomerV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StockCustomerReserved> list = stockCommonLogic.listCustomerV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockCustomerReserved>() {});
    }

    @PostMapping("/listCustomerV2/export")
    @ApiOperation("经销商预留库存列表-列表V2导出")
    public void listCustomerV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StockCustomerReserved> list = stockCommonLogic.listCustomerV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }




    @ApiOperation(value = "同步即时库存")
    @PostMapping("/syncStock")
    public ResponseHelper syncStock(
            @ApiParam(required = true, value = "组织机构") @RequestParam(required = true) String orgId,
            @ApiParam(required = true, value = "组织机构编码") @RequestParam(required = true) String orgNumber,
            @ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime
    ) throws Exception {
        stockCommonLogic.syncStock(orgId,orgNumber,startTime,endTime);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "查看仓库库存紧张")
    @GetMapping("/stockStatus")
    public ResponseHelper<List<StockStatusBean>> stockStatus(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) String correspondId,
            @ApiParam(required = false, value = "物料ID，按照顺序查询") @RequestParam(required = false) List<String> materialId
    ) throws Exception {
        List<StockStatusBean> stockBeans = stockCommonLogic.stockStatus(correspondId,materialId);
        return ResponseHelper.success(stockBeans);
    }

    @ApiOperation(value = "仓库库存数据导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "仓库名称集合") @RequestParam(required = false) String correspondNameS,
            @ApiParam(required = false, value = "仓位id") @RequestParam(required = false) String stockId,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "品类") @RequestParam(required = false) String mainNumber,
            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<StockBean> stockBeans = stockCommonLogic.list(correspondId,stockId,categoryId,materialOldNumber,
                materialName,materialNumber,specification,mainNumber,1,-1);
        ExcelData excelData = stockCommonLogic.export(stockBeans.getRecords());
        ExcelUtils.exportExcel(request,response,correspondNameS+"仓库库存数据.xlsx",excelData);
    }


    @ApiOperation(value = "经销商预留库存导出")
    @GetMapping("/listCustomerExport")
    public void listCustomerExport(
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String categoryId,

            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<StockCustomerReserved> stockCustomerIPage = stockCommonLogic.listCustomer(categoryId,materialName,materialNumber,specification,customerName,customerNumber,1,-1);
        ExcelData excelData = stockCommonLogic.listCustomerExport(stockCustomerIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"经销商预留库存.xlsx",excelData);
    }

    @ApiOperation(value = "经销商仓库列表")
    @GetMapping("/listStock")
    public ResponseHelper<IPage<StockPositionStock>> listStock(
            @ApiParam(value = "仓库名称",required = false)@RequestParam(required = false) String name,
            @ApiParam(value = "仓位名称",required = false)@RequestParam(required = false) String stockName,
            @ApiParam(value = "1 正常仓 2虚拟仓",required = false)@RequestParam(required = false) Integer type,
            @ApiParam(value = "页号",required = true)@RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam(required = true) Integer pageSize
    ) throws Exception {
        IPage<StockPositionStock> stockIPage = stockCommonLogic.listStock(name,stockName,pageNum,pageSize,type);
        return ResponseHelper.success(stockIPage);
    }

    @ApiOperation(value = "物料库存")
    @GetMapping("/stockNumber")
    public ResponseHelper<Integer> stockNumber(
            @ApiParam(required = false, value = "物料id") @RequestParam(required = false) String materialId,

            @ApiParam(value = "仓库id",required = false)@RequestParam(required = false) String correspondId
    ) throws Exception {
        Integer stockIPage = stockCommonLogic.stockNumber(materialId,correspondId);
        return ResponseHelper.success(stockIPage);
    }

    @ApiOperation(value = "经销商库存")
    @GetMapping("/stockKingNumber")
    public ResponseHelper<Integer> stockKingNumber(
            @ApiParam(required = false, value = "物料id") @RequestParam(required = false) String materialId,

            @ApiParam(value = "仓库id",required = false)@RequestParam(required = false) String correspondId
    ) throws Exception {
        Integer stockIPage = stockCommonLogic.stockKingDeeNumber(correspondId,materialId);
        Integer status = stockCommonLogic.stockKingDeeCorrespondStatus(correspondId,materialId);
        return ResponseHelper.success(stockIPage);
    }


    @ApiOperation(value = "仓库预约提示")
    @GetMapping("/stockHint")
    public ResponseHelper<String> stockHint(
            @ApiParam(required = false, value = "订单id") @RequestParam(required = false) List<String> invoiceOrderIds,
            @ApiParam(required = false, value = "预约时间") @RequestParam(required = false) String pickTime,
            @ApiParam(value = "仓库id",required = false)@RequestParam(required = false) String correspondId
    ) throws Exception {
        String s = stockCommonLogic.stockHint(correspondId,invoiceOrderIds,pickTime);
        return ResponseHelper.success(s);
    }


    @ApiOperation(value = "按钮更新发票")
    @GetMapping("/receipt")
    public ResponseHelper receipt(
            @ApiParam(required = true, value = "开始时间") @RequestParam(required = true) String startTime,
            @ApiParam(required = true, value = "结束时间") @RequestParam(required = true) String endTime
    ) throws Exception {
        String s = stockCommonLogic.receipt(startTime,endTime);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "仓库现存列表")
    @GetMapping("/listStockToDay")
    public ResponseHelper<IPage<StockBeanToDay>> listStockToDay(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,

            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,

            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<StockBeanToDay> stockBeans = stockCommonLogic.listStockToDay(correspondId,categoryId,materialOldNumber,materialName,materialNumber,specification,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }


    @ApiOperation(value = "仓库现存列表导出")
    @GetMapping("/exportStockToDay")
    public void exportStockToDay(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,

            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,

            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<StockBeanToDay> stockBeans = stockCommonLogic.listStockToDay(correspondId,categoryId,materialOldNumber,materialName,materialNumber,specification,1,-1);
        ExcelData excelData = stockCommonLogic.exportStockToDay(stockBeans.getRecords());
        ExcelUtils.exportExcel(request,response,"仓库现存列表导出.xlsx",excelData);
    }

    @ZfireList
    @PostMapping("/allPrice")
    @ApiOperation("成本价统计")
    public ResponseHelper<List<StockPrice>> allPrice(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId
            ) throws RemoteServiceException {
        List<StockPrice> list = stockCommonLogic.allPrice(correspondId);
        return ResponseHelper.success(list);
    }

    @ZfireList
    @PostMapping("/listStockToCostPriceV2")
    @ApiOperation("产品现存成本-列表V2")
    public ResponseHelper<IPage<StockBeanToCostPrice>> listStockToCostPriceV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StockBeanToCostPrice> list = stockCommonLogic.listStockToCostPriceV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockBeanToCostPrice>() {});
    }

    @PostMapping("/listStockToCostPriceV2/export")
    @ApiOperation("产品现存成本-列表V2导出")
    public void listStockToCostPriceV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StockBeanToCostPrice> list = stockCommonLogic.listStockToCostPriceV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ZfireList
    @PostMapping("/listStockToDayV2")
    @ApiOperation("产品现存-列表V2")
    public ResponseHelper<IPage<StockBeanToDay>> listStockV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StockBeanToDay> list = stockCommonLogic.listStockToDayV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockBeanToDay>() {});
    }

    @PostMapping("/listStockToDayV2/export")
    @ApiOperation("产品现存-列表V2导出")
    public void listStockV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StockBeanToDay> list = stockCommonLogic.listStockToDayV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "经销商仓库现存列表")
    @GetMapping("/listStockToDayCustomer")
    public ResponseHelper<IPage<StockBeanToDay>> listStockToDayCustomer(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,

            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,

            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<StockBeanToDay> stockBeans = stockCommonLogic.listStockToDayCustomer(correspondId,categoryId,materialOldNumber,materialName,materialNumber,specification,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }


    @ApiOperation(value = "经销商仓库现存列表导出")
    @GetMapping("/exportStockToDayCustomer")
    public void exportStockToDayCustomer(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,

            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,

            @ApiParam(required = false, value = "小类") @RequestParam(required = false) List<String> categoryId,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<StockBeanToDay> stockBeans = stockCommonLogic.listStockToDayCustomer(correspondId,categoryId,materialOldNumber,materialName,materialNumber,specification,1,-1);
        ExcelData excelData = stockCommonLogic.exportStockToDayCustomer(stockBeans.getRecords());
        ExcelUtils.exportExcel(request,response,"仓库现存列表导出.xlsx",excelData);
    }


    @ZfireList
    @PostMapping("/listStockToDayCustomerV2")
    @ApiOperation("经销商产品现存-列表V2")
    public ResponseHelper<IPage<StockBeanCustomerToDay>> listStockToDayCustomerV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StockBeanCustomerToDay> list = stockCommonLogic.listStockToDayCustomerV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockBeanCustomerToDay>() {});
    }

    @PostMapping("/listStockToDayCustomerV2/export")
    @ApiOperation("经销商产品现存-列表V2导出")
    public void listStockToDayCustomerV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StockBeanCustomerToDay> list = stockCommonLogic.listStockToDayCustomerV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiNotAuth
    @ApiOperation(value = "同步调拨单")
    @GetMapping("/syncStock")
    public void syncStock(

    ) throws Exception {
        stockCommonLogic.syncStockIn();

    }

}

