package com.sunda.spmsweb.wmscontroller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.ISapDeliveryNoteBatchReceiptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

/**
 * <p>
 * 国内仓收货入库分批收货表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2023-06-16
 */
@RestController
@RequestMapping("/sapDeliveryNoteBatchReceipt")
@Api(tags = "收货入库分批收货", description = "收货入库分批收货接口")
public class SapDeliveryNoteBatchReceiptController {
	
	@Autowired
	ISapDeliveryNoteBatchReceiptService iSapDeliveryNoteBatchReceiptService;
	
	@RequestMapping("/getSapDeliveryNoteBatchReceiptList")
    @ApiOperation(value = "根据交货单号查询收货入库关联的分批收货信息", notes = "根据交货单号查询收货入库关联的分批收货信息 \n" +
            "deliveryNoteSap, 交货单号示例：1180148330 \n", httpMethod = "GET")
    public ResponseResult getSapDeliveryNoteBatchReceiptList(@RequestParam String deliveryNoteSap) {
        try {
            return ResponseResult.success().add("list", iSapDeliveryNoteBatchReceiptService.getSapDeliveryNoteBatchReceiptList(deliveryNoteSap));
        } catch (Exception e) {
            return ResponseResult.error("获取关联单据失败").add("error", e.getMessage());
        }
    }



	@RequestMapping("/getSapDeliveryNoteBatchReceiptPage")
	@ApiOperation(value = "根据交货单号查询收货入库关联的分批收货信息", notes = "根据交货单号查询收货入库关联的分批收货信息 \n" +
			"werks, 工厂示例：CN01; whsLocationCode, 仓库示例：1061 \n" +
			"deliveryNoteSap, 交货单号示例：1180148330; spmsStatus, 单据状态示例：1 \n", 
			httpMethod = "POST")
	public ResponseResult getSapDeliveryNoteBatchReceiptPage(String deliveryNoteSap,
															String werks,
															String whsLocationCode,
															String spmsStatus,
															String werksList,
															@RequestParam(defaultValue = "1") int pageNo,
															@RequestParam(defaultValue = "20") int pageSize) {
		try {
			Page<Map<String, Object>> pageData = iSapDeliveryNoteBatchReceiptService.selectBatchReceiptHeadPage(pageNo, pageSize, deliveryNoteSap, werks, whsLocationCode, spmsStatus, werksList);
			return ResponseResult.success().add("list", pageData);
		} catch (Exception e) {
			return ResponseResult.error("获取关联单据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/getSapDeliveryNoteBatchReceiptDtl")
	@ApiOperation(value = "查询分批收货的详情信息", notes = "查询分批收货的详情信息 \n" +
			"uuid, 交货单号示例：1180148330 \n", httpMethod = "GET")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-getSapDeliveryNoteBatchReceiptDtl")
	public ResponseResult getSapDeliveryNoteBatchReceiptDtl(@RequestParam String uuid) {
		try {
			return iSapDeliveryNoteBatchReceiptService.selectBatchReceipt(uuid);
		} catch (Exception e) {
			return ResponseResult.error("获取分批收货详情信息失败").add("error", e.getMessage());
		}
	}
	
	@PostMapping("/saveSapDeliveryNoteBatchReceipt")
    @ApiOperation(value="保存其他收货分批收货数据",notes = "保存其他收货分批收货数据" +
            "{\n" +
            "  \"batchReceiptHead\": {\n" +
            "    \"uuid\": \"UUID\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"locationCode\": \"仓库\",\n" +
            "    \"srmOrderNumber\": \"SRM送货单号\",\n" +
            "    \"spmsStatus\": \"单据状态：0已关闭，1已保存，2已提交，3提交SAP成功，4提交SAP失败\",\n" +
            "    \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "    \"packageNote\": \"包装单号\",\n" +
            "    \"operateUser\": \"操作员\",\n" +
            "    \"plannedArrivalDate\": \"计划到货时间\",\n" +
            "    \"supplierCode\": \"供应商编号\",\n" +
            "    \"supplierDesc\": \"供应商名称\",\n" +
            "    \"remark\": \"备注\"\n" +
            "  },\n" +
            "  \"batchReceiptDtl\": [\n" +
            "    {\n" +
            "      \"uuid\": \"UUID\",\n" +
            "      \"item\": \"行号\",\n" +
            "      \"spmsId\": \"单据号\",\n" +
            "      \"materialNo\": \"物料编号\",\n" +
            "      \"boxNote\": \"箱码\",\n" +
            "      \"basicUnit\": \"基本单位\",\n" +
            "      \"deliveryQty\": \"交货数量\",\n" +
            "      \"receivedQty\": \"实收数量\",\n" +
            "      \"unitSales\": \"销售单位\",\n" +
            "      \"qtySales\": \"销售单位应收数量\",\n" +
            "      \"actualQtySales\": \"销售单位实收数量\",\n" +
            "      \"tempStorage\": \"[{\\\"storageUuid\\\":\\\"cbd9576a4e53461fe0530101007f3e24\\\",\\\"qty\\\":3,\\\"storageNo\\\":\\\"A010101\\\",\\\"unitValue\\\":3}]\",\n" +
            "      \"storageUuid\": \"储位UUID\",\n" +
            "      \"boxMaterialName\": \"外箱材质\",\n" +
            "      \"boxMaterialName\": \"外箱材质\",\n" +
            "      \"boxGrossWeight\": \"毛重\",\n" +
            "      \"boxNetWeight\": \"净重\",\n" +
            "      \"boxColor\": \"颜色\",\n" +
            "      \"remark\": \"备注\",\n" +
            "      \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "      \"deliveryNoteSapItem\": \"SAP交货单行号\"\n" +
            "    }\n" +
            "  ]\n" +
            "}", httpMethod = "POST")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-saveSapDeliveryNoteBatchReceipt")
    public ResponseResult saveSapDeliveryNoteBatchReceipt(@RequestBody JSONObject obj){
		try {
	        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
	        return iSapDeliveryNoteBatchReceiptService.saveSapDeliveryNoteBatchReceipt(obj, userId);
		} catch (Exception e) {
			return ResponseResult.error(e.getMessage());
		}
    }
	
	@PostMapping("/submitSapDeliveryNoteBatchReceipt")
    @ApiOperation(value="提交收货入库分批收货数据",notes = "提交收货入库分批收货数据" +
            "{\n" +
            "  \"batchReceiptHead\": {\n" +
            "    \"uuid\": \"UUID\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"locationCode\": \"仓库\",\n" +
            "    \"srmOrderNumber\": \"SRM送货单号\",\n" +
            "    \"spmsStatus\": \"单据状态：0已关闭，1已保存，2已提交，3提交SAP成功，4提交SAP失败\",\n" +
            "    \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "    \"packageNote\": \"包装单号\",\n" +
            "    \"operateUser\": \"操作员\",\n" +
            "    \"plannedArrivalDate\": \"计划到货时间\",\n" +
            "    \"supplierCode\": \"供应商编号\",\n" +
            "    \"supplierDesc\": \"供应商名称\",\n" +
            "    \"remark\": \"备注\"\n" +
            "  },\n" +
            "  \"batchReceiptDtl\": [\n" +
            "    {\n" +
            "      \"uuid\": \"UUID\",\n" +
            "      \"item\": \"行号\",\n" +
            "      \"spmsId\": \"单据号\",\n" +
            "      \"materialNo\": \"物料编号\",\n" +
            "      \"boxNote\": \"箱码\",\n" +
            "      \"basicUnit\": \"基本单位\",\n" +
            "      \"deliveryQty\": \"交货数量\",\n" +
            "      \"receivedQty\": \"实收数量\",\n" +
            "      \"unitSales\": \"销售单位\",\n" +
            "      \"qtySales\": \"销售单位应收数量\",\n" +
            "      \"actualQtySales\": \"销售单位实收数量\",\n" +
            "      \"tempStorage\": \"[{\\\"storageUuid\\\":\\\"cbd9576a4e53461fe0530101007f3e24\\\",\\\"qty\\\":3,\\\"storageNo\\\":\\\"A010101\\\",\\\"unitValue\\\":3}]\",\n" +
            "      \"storageUuid\": \"储位UUID\",\n" +
            "      \"boxMaterialName\": \"外箱材质\",\n" +
            "      \"boxGrossWeight\": \"毛重\",\n" +
            "      \"boxNetWeight\": \"净重\",\n" +
            "      \"boxColor\": \"颜色\",\n" +
            "      \"remark\": \"备注\",\n" +
            "      \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "      \"deliveryNoteSapItem\": \"SAP交货单行号\"\n" +
            "    }\n" +
            "  ]\n" +
            "}", httpMethod = "POST")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-submitSapDeliveryNoteBatchReceipt")
    public ResponseResult submitSapDeliveryNoteBatchReceipt(@RequestBody JSONObject obj){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iSapDeliveryNoteBatchReceiptService.submitWmsBatchReceipt(obj, userId);
		} catch (Exception e) {
			return ResponseResult.error(e.getMessage());
		}
    }
	
	@PostMapping("/submitToSap")
    @ApiOperation(value="提交SAP",notes = "提交SAP" +
            "{\n" +
            "  \"batchReceiptHead\": {\n" +
            "    \"uuid\": \"UUID\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"locationCode\": \"仓库\",\n" +
            "    \"srmOrderNumber\": \"SRM送货单号\",\n" +
            "    \"spmsStatus\": \"单据状态：0已关闭，1已保存，2已提交，3提交SAP成功，4提交SAP失败\",\n" +
            "    \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "    \"packageNote\": \"包装单号\",\n" +
            "    \"operateUser\": \"操作员\",\n" +
            "    \"postingDate\": \"过账日期\",\n" +
            "    \"remarks\": \"备注\"\n" +
            "  }\n" +
            "}", httpMethod = "POST")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-submitToSap")
    public ResponseResult submitToSap(@RequestBody JSONObject obj){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iSapDeliveryNoteBatchReceiptService.submitToSap(obj, userId);
		} catch (Exception e) {
			return ResponseResult.error(e.getMessage());
		}
    }
	
	@PostMapping("/cancelDn")
	@ApiOperation(value="撤销",notes = "撤销" +
			"{\n" +
			"  \"batchReceiptHead\": {\n" +
			"    \"uuid\": \"UUID\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"locationCode\": \"仓库\",\n" +
            "    \"srmOrderNumber\": \"SRM送货单号\",\n" +
            "    \"spmsStatus\": \"单据状态：0已关闭，1已保存，2已提交，3提交SAP成功，4提交SAP失败\",\n" +
            "    \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "    \"packageNote\": \"包装单号\",\n" +
            "    \"operateUser\": \"操作员\",\n" +
            "    \"cancelPostingDate\": \"撤销日期\",\n" +
            "    \"remark\": \"备注\"\n" +
			"  }\n" +
			"}", httpMethod = "POST")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-cancelDn")
	public ResponseResult cancelDn(@RequestBody JSONObject obj){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iSapDeliveryNoteBatchReceiptService.cancelDn(obj, userId);
		} catch (Exception e) {
			return ResponseResult.error("撤销失败："+e.getMessage());
		}
	}
	
	@PostMapping("/deleteReceipt")
	@ApiOperation(value="删除",notes = "删除" +
			"{\n" +
			"  \"batchReceiptHead\": {\n" +
			"    \"uuid\": \"UUID\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"locationCode\": \"仓库\",\n" +
            "    \"srmOrderNumber\": \"SRM送货单号\",\n" +
            "    \"spmsStatus\": \"单据状态：0已关闭，1已保存，2已提交，3提交SAP成功，4提交SAP失败\",\n" +
            "    \"deliveryNoteSap\": \"SAP交货单号\",\n" +
            "    \"packageNote\": \"包装单号\",\n" +
            "    \"operateUser\": \"操作员\",\n" +
            "    \"remark\": \"备注\"\n" +
			"  }\n" +
			"}", httpMethod = "POST")
	@RequiresPermissions("sapDeliveryNoteBatchReceipt-deleteReceipt")
	public ResponseResult deleteReceipt(@RequestBody JSONObject obj){
		String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
		return iSapDeliveryNoteBatchReceiptService.deleteReceipt(obj, userId);
	}
	
	
}
