package com.sunda.spmsweb.overseacontroller;


import org.apache.shiro.SecurityUtils;
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;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.service.IOverseaWhsInventoryLossService;
import com.sunda.spmsuser.entity.SpmsUser;
import com.sunda.spmsuser.service.ISpmsUserService;
import com.sunda.spmsweb.util.JWTUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 海外仓-存货损益头表 前端控制器
 * </p>
 *
 * @author Hzc
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/overseaWhsInventoryLoss")
@Api(tags = "海外仓存货损益", description = "海外仓存货损益")
public class OverseaWhsInventoryLossController {
	
	@Autowired
	IOverseaWhsInventoryLossService iOverseaWhsInventoryLossService;
	
	@Autowired
	ISpmsUserService iSpmsUserService;
	
	@RequestMapping("/getWhsInventoryLossPage")
    @ApiOperation(value = "分页查询存货损益数据", notes = "分页查询存货损益数据\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"werks\": \"\",\n" +
            "\t\"whsLocationCode\": \"\",\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"orderType\": 单据类型,\n" +
            "\t\"movementType\": 移动类型,\n" +
            "\t\"spmsId\": SPMS单据号,\n" +
            "\t\"oaWorkflow\": OA审批单号,\n" +
            "\t\"sapDeliveryNote\": 交货单号,\n" +
            "\t\"beginDate\": \"\",\n" +
            "\t\"endDate\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 20\n" +
            "}", httpMethod = "POST")
    public ResponseResult getWhsInventoryLossPage(@RequestBody JSONObject jsonObject){
        try {
            return iOverseaWhsInventoryLossService.getWhsInventoryLossPage(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
	
	@RequestMapping("/findDetails")
    @ApiOperation(value = "存货损益查询详情数据", notes = "存货损益查询详情数据\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"uuid\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult findDetails(@RequestBody JSONObject jsonObject){
        try {
            return iOverseaWhsInventoryLossService.getWhsInventoryLossDetails(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
	
	@RequestMapping("/createWhsInventoryLoss")
	@ApiOperation(value = "保存存货损益数据", notes = "保存存货损益数据\n" +
			"示例参数：\n" +
			"{\n" +
            "  \"head\": {\n" +
            "    \"uuid\": \"\",\n" +
            "    \"spmsId\": \"单据号\",\n" +
            "    \"sapPackageList\": \"装箱单号\",\n" +
            "    \"sapDeliveryNote\": \"交货单号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"whsLocationCode\": \"仓库\",\n" +
            "    \"spmsStatus\": \"单据状态：0关闭,1草稿,2待OA审批,3OA审批退回,4OA审批通过,5提交SAP成功,6提交SAP失败\",\n" +
            "    \"movementType\": \"移动类型：Z15-收货少收,Z16-收货多收\",\n" +
            "    \"orderType\": \"单据类型：1海运收货,2海外转储,3空运人带,4盘盈盘亏,5领用出库\",\n" +
            "    \"costCenter\": \"成本中心\",\n" +
            "    \"postingDate\": \"过账日期:2024-05-08\",\n" +
            "    \"dataVersion\": \"数据版本号\",\n" +
            "    \"remark\": \"备注\"\n" +
            "  },\n" +
            "  \"itemList\": [\n" +
            "    {\n" +
            "      \"headUuid\": \"头表UUID\",\n" +
            "      \"itemNumber\": \"行号\",\n" +
            "      \"sapDeliveryNote\": \"交货单号\",\n" +
            "      \"sapDeliveryNoteItem\": \"交货单行号\",\n" +
            "      \"materialNo\": \"物料编号\",\n" +
            "      \"basicUnit\": \"基本单位\",\n" +
            "      \"unitSales\": \"销售单位\",\n" +
            "      \"receivableQtyBasicUnit\": \"应收数量-基本单位\",\n" +
            "      \"receivableQtyUnitSales\": \"应收数量-销售单位\",\n" +
            "      \"actualQtyBasicUnit\": \"实收数量-基本单位\",\n" +
            "      \"actualQtyUnitSales\": \"实收数量-销售单位\",\n" +
            "      \"underchargedQtyBasicUnit\": \"少收数量-基本单位\",\n" +
            "      \"underchargedQtyUnitSales\": \"少收数量-销售单位\",\n" +
            "      \"overchargedQtyBasicUnit\": \"多收数量-基本单位\",\n" +
            "      \"overchargedQtyUnitSales\": \"多收数量-销售单位\",\n" +
            "      \"remarks\": \"备注\",\n" +
            "      \"movementType\": \"移动类型：Z15-收货少收,Z16-收货多收\",\n" +
            "      \"werks\": \"工厂\",\n" +
            "      \"whsLocationCode\": \"仓库\"\n" +
            "    }\n" +
            "  ]\n" +
            "}", httpMethod = "POST")
	public ResponseResult createWhsInventoryLoss(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iOverseaWhsInventoryLossService.createWhsInventoryLoss(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/toOaWhsInventoryLoss")
	@ApiOperation(value = "存货损益数据提交OA", notes = "存货损益数据提交OA\n" +
			"示例参数：\n" +
			"{\n" +
			"  \"head\": {\n" +
			"    \"uuid\": \"\",\n" +
			"    \"spmsId\": \"单据号\",\n" +
			"    \"sapPackageList\": \"装箱单号\",\n" +
			"    \"sapDeliveryNote\": \"交货单号\",\n" +
			"    \"werks\": \"工厂\",\n" +
			"    \"whsLocationCode\": \"仓库\",\n" +
			"    \"spmsStatus\": \"单据状态：0关闭,1草稿,2待OA审批,3OA审批退回,4OA审批通过,5提交SAP成功,6提交SAP失败\",\n" +
			"    \"movementType\": \"移动类型：Z15-收货少收,Z16-收货多收\",\n" +
			"    \"orderType\": \"单据类型：1海运收货,2海外转储,3空运人带,4盘盈盘亏,5领用出库\",\n" +
			"    \"costCenter\": \"成本中心\",\n" +
			"    \"postingDate\": \"过账日期:2024-05-08\",\n" +
			"    \"dataVersion\": \"数据版本号\",\n" +
			"    \"remark\": \"备注\"\n" +
			"  },\n" +
			"  \"itemList\": [\n" +
			"    {\n" +
			"      \"headUuid\": \"头表UUID\",\n" +
			"      \"itemNumber\": \"行号\",\n" +
			"      \"sapDeliveryNote\": \"交货单号\",\n" +
			"      \"sapDeliveryNoteItem\": \"交货单行号\",\n" +
			"      \"materialNo\": \"物料编号\",\n" +
			"      \"basicUnit\": \"基本单位\",\n" +
			"      \"unitSales\": \"销售单位\",\n" +
			"      \"receivableQtyBasicUnit\": \"应收数量-基本单位\",\n" +
			"      \"receivableQtyUnitSales\": \"应收数量-销售单位\",\n" +
			"      \"actualQtyBasicUnit\": \"实收数量-基本单位\",\n" +
			"      \"actualQtyUnitSales\": \"实收数量-销售单位\",\n" +
			"      \"underchargedQtyBasicUnit\": \"少收数量-基本单位\",\n" +
			"      \"underchargedQtyUnitSales\": \"少收数量-销售单位\",\n" +
			"      \"overchargedQtyBasicUnit\": \"多收数量-基本单位\",\n" +
			"      \"overchargedQtyUnitSales\": \"多收数量-销售单位\",\n" +
			"      \"remarks\": \"备注\",\n" +
			"      \"movementType\": \"移动类型：Z15-收货少收,Z16-收货多收\",\n" +
			"      \"werks\": \"工厂\",\n" +
			"      \"whsLocationCode\": \"仓库\"\n" +
			"    }\n" +
			"  ]\n" +
			"}", httpMethod = "POST")
	public ResponseResult toOaWhsInventoryLoss(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			SpmsUser spmsUser = iSpmsUserService.getByUserId(userId);
			return iOverseaWhsInventoryLossService.toOaWhsInventoryLoss(jsonObject, spmsUser);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/toSapWhsInventoryLoss")
	@ApiOperation(value = "存货损益数据提交SAP", notes = "存货损益数据提交SAP\n" +
			"示例参数：\n" +
			"{\n" +
			"    \"uuid\": \"\",\n" +
			"    \"postingDate\": \"过账日期:2024-05-08\",\n" +
			"    \"dataVersion\": \"数据版本号\"\n" +
			"}", httpMethod = "POST")
	public ResponseResult toSapWhsInventoryLoss(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iOverseaWhsInventoryLossService.toSapWhsInventoryLoss(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/cancelWhsInventoryLoss")
	@ApiOperation(value = "存货损益数据撤销", notes = "存货损益数据撤销\n" +
			"示例参数：\n" +
			"{\n" +
			"    \"uuid\": \"\",\n" +
			"    \"cancelPostingDate\": \"撤销过账日期:2024-05-08\",\n" +
			"    \"dataVersion\": \"数据版本号\"\n" +
			"}", httpMethod = "POST")
	public ResponseResult cancelWhsInventoryLoss(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iOverseaWhsInventoryLossService.cancelWhsInventoryLoss(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/closeWhsInventoryLoss")
	@ApiOperation(value = "关闭存货损益数据", notes = "关闭存货损益数据\n" +
			"示例参数：\n" +
			"{\n" +
			"    \"uuid\": \"\",\n" +
			"    \"cancelReason\": \"关闭原因\",\n" +
			"    \"dataVersion\": \"数据版本号\"\n" +
			"}", httpMethod = "POST")
	public ResponseResult closeWhsInventoryLoss(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iOverseaWhsInventoryLossService.closeWhsInventoryLoss(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
}
