package com.sunda.spmsweb.wmscontroller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IFileOperationService;
import com.sunda.spmswms.dto.WhsSearchDTO;
import com.sunda.spmswms.service.IWhsReport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 南沙仓报表 前端控制器
 * </p>
 *
 * @author Kunpeng
 * @since 2021-08-09
 */
@RestController
@Api(tags = "南沙仓报表", description = "南沙仓报表")
@RequestMapping("/nanshareporter")
public class WhsReportController {

    @Autowired
    IWhsReport iWhsReport;
    
    @Autowired
    IFileOperationService iFileOperationService;

    @RequestMapping("/inHouseDtlExport")
    @ApiOperation(value = "入库明细导出", notes = "入库明细导出" +
            "{\n" +
            "  \"startDate\":\"2021-08-09\",\n" +
            "  \"endDate\":\"2021-08-10\",\n" +
            "  \"goodsType\":\"X\",\n" +
            "  \"werks\":\"CN01\",\n" +
            "  \"whsLocationCode\":\"1061\",\n" +
            "  \"deliveryNoteSap\":\"deliveryNoteSap\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-inHouseDtlExport")
    public ResponseResult inHouseDtlExport(@RequestBody JSONObject queryObj) {
        String goodsType = queryObj.getString("goodsType");
        if (StringUtils.isEmpty(goodsType)) {
            return ResponseResult.error("GoodsType is mandatory required");
        }
        return iWhsReport.inHouseDtlExport(queryObj,goodsType);
    }

    @RequestMapping("/outHouseDtlExport")
    @ApiOperation(value = "出库明细导出", notes = "出库明细导出" +
            "{\n" +
            "  \"startDate\":\"2021-08-09\",\n" +
            "  \"endDate\":\"2021-08-10\",\n" +
            "  \"goodsType\":\"X\",\n" +
            "  \"werks\":\"CN01\",\n" +
            "  \"whsLocationCode\":\"1061\",\n" +
            "  \"packageList\":\"packageList\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-outHouseDtlExport")
    public ResponseResult outHouseDtlExport(@RequestBody JSONObject queryObj) {
        String goodsType = queryObj.getString("goodsType");
        if (StringUtils.isEmpty(goodsType)) {
            return ResponseResult.error("GoodsType is mandatory required");
        }
        return iWhsReport.outHouseDtlExport(queryObj,goodsType);
    }

    @RequestMapping("/outWHExportM")
    @ApiOperation(value = "按装箱单号或是柜号导出贸易货出库明细", notes ="按装箱单号或是柜号导出贸易货出库明细" +
            "{\n" +
            "  \"cabinetNumber\":\"UETU2463177\",\n" +
            "  \"packageList\":\"PL00038153\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-outWHExportM")
    public ResponseResult outWHExportM(@RequestBody JSONObject queryObj) {
        return iWhsReport.outWHExportM(queryObj);
    }
    
    @RequestMapping("/outWHExportX")
    @ApiOperation(value = "按装箱单号或是柜号导出备件货出库明细", notes ="按装箱单号或是柜号导出备件货出库明细" +
            "{\n" +
            "  \"cabinetNumber\":\"UETU2463177\",\n" +
            "  \"packageList\":\"PL00038153\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-outWHExportX")
    public ResponseResult outWHExportX(@RequestBody JSONObject queryObj) {
        return iWhsReport.outWHExportX(queryObj);
    }


    @RequestMapping("/queryBoxInfo")
    @ApiOperation(value = "按照物料编码,需求单号,箱码,包装单号,采购定单号,送货单号,内部交货单号等查询箱码信息", notes ="按照物料编码,需求单号,箱码,包装单号,采购定单号,送货单号,内部交货单号等查询箱码信息" +
            "{\n" +
            "  \"boxNote\":\"\",\n" +
            "  \"demandNo\":\"\",\n" +
            "  \"packageNote\":\"\",\n" +
            "  \"purchaseNo\":\"\",\n" +
            "  \"deliveryNoteSap\":\"\",\n" +
            "  \"srmContractNo\":\"\",\n" +
            "  \"materialNo\":\"\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-queryBoxInfo")
    public ResponseResult queryBoxInfo(@RequestBody JSONObject queryObj) {
        return iWhsReport.queryBoxInfo(queryObj);
    }
    
    @RequestMapping("/getWhsMaterialStockQtyDiffer")
    @ApiOperation(value = "国内仓获取SPMS与SAP库存差异报表", notes = "国内仓获取SPMS与SAP库存差异报表\n" +
    		"所有参数可选输入。示例参数：displayDiff是否显示差异：0否，1是\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "goodsType = M,\n" +
            "differNum = 0,\n" +
            "displayDiff = 0,\n" +
            "searchInfo = 210017651" +
            "}\n", httpMethod = "POST")
    public ResponseResult getWhsMaterialStockQtyDiffer(@RequestBody WhsSearchDTO dto){
        try {
            return iWhsReport.getMaterialStockQtyDifferList(dto);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/exportWhsMaterialStockQtyDifferExcel")
    @ApiOperation(value = "国内仓SPMS与SAP库存差异报表导出EXCEL", notes = "国内仓SPMS与SAP库存差异报表导出EXCEL\n" +
    		"所有参数可选输入。示例参数：displayDiff是否显示差异：0否，1是\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "goodsType = M,\n" +
            "differNum = 0,\n" +
            "searchInfo = 210017651" +
            "}\n", httpMethod = "POST")
    public ResponseResult exportWhsMaterialStockQtyDifferExcel(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("请求工厂不能为空");
    		}
    		if (StringUtils.isEmpty(dto.getWhsLocationCode())) {
    			return ResponseResult.error("仓库编号不能为空");
    		}
            String filename = "WhsMaterialStockQtyDiffer_" + System.currentTimeMillis()+".xlsx";
            // 1.附件缓存路径
            String excelPath = iFileOperationService.saveWhsMaterialStockQtyDifferExcel(iWhsReport.getMaterialStockQtyDifferList(dto), filename);
            if (StringUtils.isEmpty(excelPath)){
                return ResponseResult.error("附件创建失败");
            }
            // 2.上传附件至minIO文件服务器
            String fileUrl = iFileOperationService.fileUploader(filename, excelPath);
            if (fileUrl.isEmpty()){
                return ResponseResult.error("附件上传失败:" + excelPath + "--" + fileUrl);
            }
            // 3.返回附件地址，上传OA
            return ResponseResult.success().add("fileUrl",fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/getBoxInfoList")
    @ApiOperation(value = "国内仓箱码信息查询", notes = "国内仓箱码信息查询\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "storageNo = D080101,\n" +
            "packageNote = 0,\n" +
            "deliveryNoteSap = 0,\n" +
            "purchaseNo = 0,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20,\n" +
            "boxNote = 210017651" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-getBoxInfoList")
    public ResponseResult getBoxInfoList(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks()) && CollectionUtils.isEmpty(dto.getWerksList())) {
    			return ResponseResult.error("请求工厂不能为空");
    		}
            return iWhsReport.getBoxInfoList(dto);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/exportBoxInfoExcel")
    @ApiOperation(value = "国内仓箱码信息导出EXCEL", notes = "国内仓箱码信息导出EXCEL\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "storageNo = D080101,\n" +
            "packageNote = 0,\n" +
            "deliveryNoteSap = 0,\n" +
            "purchaseNo = 0,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20,\n" +
            "boxNote = 210017651" +
            "}\n", httpMethod = "POST")
    public ResponseResult exportBoxInfoExcel(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("请求工厂不能为空");
    		}
        	dto.setPageNo(1);
        	dto.setPageSize(Integer.MAX_VALUE);
            String filename = "WhsBoxInfo_" + System.currentTimeMillis()+".xlsx";
            // 1.附件缓存路径
            String excelPath = iFileOperationService.saveWhsBoxInfoExcel(iWhsReport.getBoxInfoList(dto), filename);
            if (StringUtils.isEmpty(excelPath)){
                return ResponseResult.error("附件创建失败");
            }
            // 2.上传附件至minIO文件服务器
            String fileUrl = iFileOperationService.fileUploader(filename, excelPath);
            if (fileUrl.isEmpty()){
                return ResponseResult.error("附件上传失败:" + excelPath + "--" + fileUrl);
            }
            // 3.返回附件地址，上传OA
            return ResponseResult.success().add("fileUrl",fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/getSapDeliveryNoteAndOtherTask")
    @ApiOperation(value = "工资计件报表查询", notes = "工资计件报表查询\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "searchInfo = 物料号/物料描述,\n" +
            "deliveryNoteSap = 交货单号,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20,\n" +
            "boxNote = 箱码" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-getSapDeliveryNoteAndOtherTask")
    public ResponseResult getSapDeliveryNoteAndOtherTask(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("工厂不能为空");
    		}
        	if (StringUtils.isEmpty(dto.getWhsLocationCode())) {
        		return ResponseResult.error("仓库不能为空");
        	}
            return iWhsReport.getSapDeliveryNoteAndOtherTask(dto);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/exportSapDeliveryNoteAndOtherTask")
    @ApiOperation(value = "工资计件报表导出EXCEL", notes = "工资计件报表导出EXCEL\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "storageNo = D080101,\n" +
            "packageNote = 0,\n" +
            "deliveryNoteSap = 0,\n" +
            "purchaseNo = 0,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20,\n" +
            "boxNote = 210017651" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-exportSapDeliveryNoteAndOtherTask")
    public ResponseResult exportSapDeliveryNoteAndOtherTask(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("工厂不能为空");
    		}
        	if (StringUtils.isEmpty(dto.getWhsLocationCode())) {
        		return ResponseResult.error("仓库不能为空");
        	}
            dto.setPageNo(1);
        	dto.setPageSize(Integer.MAX_VALUE);
            String filename = "SapDeliveryNoteAndOtherTask_" + System.currentTimeMillis()+".xlsx";
            // 1.附件缓存路径
            String excelPath = iFileOperationService.saveSapDeliveryNoteAndOtherTaskExcel(iWhsReport.getSapDeliveryNoteAndOtherTask(dto), filename);
            if (StringUtils.isEmpty(excelPath)) {
                return ResponseResult.error("附件创建失败");
            }
            // 2.上传附件至minIO文件服务器
            String fileUrl = iFileOperationService.fileUploader(filename, excelPath);
            if (fileUrl.isEmpty()){
                return ResponseResult.error("附件上传失败:" + excelPath + "--" + fileUrl);
            }
            // 3.返回附件地址，上传OA
            return ResponseResult.success().add("fileUrl",fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/getReconciliationReport")
    @ApiOperation(value = "对账报表查询", notes = "对账报表查询\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "searchInfo = 物料号,\n" +
            "deliveryNoteSap = 交货单号,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20\n" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-getReconciliationReport")
    public ResponseResult getReconciliationReport(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("工厂不能为空");
    		}
        	if (StringUtils.isEmpty(dto.getWhsLocationCode())) {
        		return ResponseResult.error("仓库不能为空");
        	}
            return iWhsReport.getReconciliationReport(dto);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/exportReconciliationReport")
    @ApiOperation(value = "对账报表导出EXCEL", notes = "对账报表导出EXCEL\n" +
    		"所有参数可选输入。示例参数：\n" +
            "{\n" +
            "werks = TF02,\n" +
            "whsLocationCode = 1006,\n" +
            "searchInfo = 物料号,\n" +
            "deliveryNoteSap = 交货单号,\n" +
            "pageNo = 1,\n" +
            "pageSize = 20\n" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("nanshareporter-exportReconciliationReport")
    public ResponseResult exportReconciliationReport(@RequestBody WhsSearchDTO dto){
        try {
        	if (StringUtils.isEmpty(dto.getWerks())) {
    			return ResponseResult.error("工厂不能为空");
    		}
        	if (StringUtils.isEmpty(dto.getWhsLocationCode())) {
        		return ResponseResult.error("仓库不能为空");
        	}
            dto.setPageNo(1);
        	dto.setPageSize(Integer.MAX_VALUE);
            String filename = "ReconciliationReport_" + System.currentTimeMillis()+".xlsx";
            // 1.附件缓存路径
            String excelPath = iFileOperationService.saveReconciliationReportExcel(iWhsReport.getReconciliationReport(dto), filename);
            if (StringUtils.isEmpty(excelPath)) {
                return ResponseResult.error("附件创建失败");
            }
            // 2.上传附件至minIO文件服务器
            String fileUrl = iFileOperationService.fileUploader(filename, excelPath);
            if (fileUrl.isEmpty()){
                return ResponseResult.error("附件上传失败:" + excelPath + "--" + fileUrl);
            }
            // 3.返回附件地址，上传OA
            return ResponseResult.success().add("fileUrl",fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}