package com.sunda.spmsweb.wmscontroller;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.IWhsOtherTaskCancelService;
import com.sunda.spmswms.service.IWhsOtherTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.*;

@RestController
@RequestMapping("/whsOther")
@Api(tags = "其它出入库接口")
public class WhsOtherController {

    @Autowired
    IWhsOtherTaskService iWhsOtherTaskService;

    @Autowired
    IWhsOtherTaskCancelService iWhsOtherTaskCancelService;

    @RequestMapping("/updateWhsOut")
    @ApiOperation(value = "其它出入库创建、更新接口", notes = "其它出入库创建、更新接口\n" +
            "{\n" +
            "  \"whsOther\": {\n" +
            "    \"updateTime\": \"\",\n" +
            "    \"operateUser\": \"\",\n" +
            "    \"createTime\": \"\",\n" +
            "    \"costCenter\": \"\",\n" +
            "    \"createDate\": \"2021-06-25\",\n" +
            "    \"createUserid\": \"999904\",\n" +
            "    \"deliveryNoteSrm\": \"\",\n" +
            "    \"documentDate\": \"\",\n" +
            "    \"goodsType\": \"M\",\n" +
            "    \"movementType\": \"311\",\n" +
            "    \"packageList\": \"\",\n" +
            "    \"packageNote\": \"\",\n" +
            "    \"postingDate\": \"\",\n" +
            "    \"purchaseGroup\": \"\",\n" +
            "    \"purchaseGroupName\": \"\",\n" +
            "    \"relatedDocumentDate\": \"\",\n" +
            "    \"relatedPostingDate\": \"\",\n" +
            "    \"relatedTaskId\": \"\",\n" +
            "    \"relatedVoucherNumber\": \"\",\n" +
            "    \"relatedVoucherYear\": \"\",\n" +
            "    \"remarks\": \"\",\n" +
            "    \"sapOrderType\": \"\",\n" +
            "    \"sapPo\": \"\",\n" +
            "    \"sapVoucherNumber\": \"\",\n" +
            "    \"sapVoucherYear\": \"\",\n" +
            "    \"spmsId\": \"\",\n" +
            "    \"taskStatus\": \"1\",\n" +
            "    \"uuid\": \"\",\n" +
            "    \"werks\": \"CN01\",\n" +
            "    \"whsLocationCode\": \"1061\",\n" +
            "    \"goodsTypeDesc\": \"贸易货\",\n" +
            "    \"movementTypeDesc\": \"卸货入库\",\n" +
            "    \"taskStatusDesc\": \"草稿\",\n" +
            "    \"currentStatus\": \"1\",\n" +
            "    \"targetStatus\": \"1\"\n" +
            "  },\n" +
            "  \"whsOtherDtl\": [\n" +
            "    {\n" +
            "      \"rowId\": 1624592938262,\n" +
            "      \"autoId\": 1624592938263,\n" +
            "      \"materialNo\": \"180002671\",\n" +
            "      \"packageList\": \"PL00027372\",\n" +
            "      \"packageRowId\": \"10\",\n" +
            "      \"basicUnit\": \"PCS\",\n" +
            "      \"operateQty\": 100,\n" +
            "      \"packageUnit\": \"PAC\",\n" +
            "      \"packageQty\": 8.333,\n" +
            "      \"materialNoIn\": \"\",\n" +
            "      \"qtyIn\": \"\",\n" +
            "      \"materialNoOut\": \"\",\n" +
            "      \"qtyOut\": \"\",\n" +
            "      \"costCenter\": \"\",\n" +
            "      \"rowRemarks\": \"\",\n" +
            "      \"supplierDesc\": \"\",\n" +
            "      \"supplierNo\": \"\",\n" +
            "      \"tempStorage\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"cd8a479246954765bc927f992f368987\",\n" +
            "          \"qty\": 100,\n" +
            "          \"storageNo\": \"A020101\",\n" +
            "          \"unitValue\": 8.333\n" +
            "        }\n" +
            "      ],\n" +
            "      \"werks\": \"CN01\",\n" +
            "      \"whsLocationCode\": \"1061\",\n" +
            "      \"whsOtherUuid\": \"\",\n" +
            "      \"materialZhDesc\": \"方铁锹1.5KG S501 铁柄 SHOVEL S501 12PCS/BAG\",\n" +
            "      \"materialEnDesc\": \"Shovel iron-handle Square S501 W16\",\n" +
            "      \"packingSpec\": \"\",\n" +
            "      \"numerator\": 12,\n" +
            "      \"denominator\": 1,\n" +
            "      \"storageUuidList\": []\n" +
            "    },\n" +
            "    {\n" +
            "      \"rowId\": 1624592938264,\n" +
            "      \"autoId\": 1624592938265,\n" +
            "      \"materialNo\": \"180002671\",\n" +
            "      \"packageList\": \"PL00027372\",\n" +
            "      \"packageRowId\": \"20\",\n" +
            "      \"basicUnit\": \"PCS\",\n" +
            "      \"operateQty\": 3600,\n" +
            "      \"packageUnit\": \"PAC\",\n" +
            "      \"packageQty\": 300,\n" +
            "      \"materialNoIn\": \"\",\n" +
            "      \"qtyIn\": \"\",\n" +
            "      \"materialNoOut\": \"\",\n" +
            "      \"qtyOut\": \"\",\n" +
            "      \"costCenter\": \"\",\n" +
            "      \"rowRemarks\": \"\",\n" +
            "      \"supplierDesc\": \"\",\n" +
            "      \"supplierNo\": \"\",\n" +
            "      \"tempStorage\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"b95946ac1dab42b79ade3438acb0b538\",\n" +
            "          \"qty\": 1200,\n" +
            "          \"storageNo\": \"A010101\",\n" +
            "          \"unitValue\": 100\n" +
            "        },\n" +
            "        {\n" +
            "          \"storageUuid\": \"cd8a479246954765bc927f992f368987\",\n" +
            "          \"qty\": 2400,\n" +
            "          \"storageNo\": \"A020101\",\n" +
            "          \"unitValue\": 200\n" +
            "        }\n" +
            "      ],\n" +
            "      \"werks\": \"CN01\",\n" +
            "      \"whsLocationCode\": \"1061\",\n" +
            "      \"whsOtherUuid\": \"\",\n" +
            "      \"materialZhDesc\": \"方铁锹1.5KG S501 铁柄 SHOVEL S501 12PCS/BAG\",\n" +
            "      \"materialEnDesc\": \"Shovel iron-handle Square S501 W16\",\n" +
            "      \"packingSpec\": \"\",\n" +
            "      \"numerator\": 12,\n" +
            "      \"denominator\": 1,\n" +
            "      \"storageUuidList\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"b95946ac1dab42b79ade3438acb0b538\",\n" +
            "          \"qty\": 1200,\n" +
            "          \"storageNo\": \"A010101\",\n" +
            "          \"unitValue\": 100\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  ]\n" +
            "}\n", httpMethod = "POST")
    @RequiresPermissions("whsOther-updateWhsOut")
    public synchronized ResponseResult updateWhsOut(@RequestBody JSONObject WhsOtherDtl) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            return iWhsOtherTaskService.onSaveWhsOther(WhsOtherDtl, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error().add("error", e.getMessage());
        }
    }


    @PostMapping("/getWhsOtherList")
    @ApiOperation(value = "获取其它出入库任务列表", notes = "获取其它出入库任务列表" +
            "{\n" +
            "  \"werks\":\"CN01\",\n" +
            "  \"whsLocationCode\":\"1011\",\n" +
            "  \"movementType\":\"311\",\n" +
            "  \"goodsType\":\"M\",\n" +
            "  \"createDate\":[\"2021-06-18\",\"2021-06-21\"],\n" +
            "  \"taskStatus\":\"1\",\n"+
            "  \"pageNo\": 1,\n"+
            "  \"pageSize\": 20 \n"+
            "}", httpMethod = "POST")
    @RequiresPermissions("whsOther-getWhsOtherList")
    public ResponseResult getWhsOtherList(@RequestBody JSONObject whsOFilter) {
        return iWhsOtherTaskService.getWhsOtherList(whsOFilter);
    }

    @GetMapping("/getWhsOtherWithDtl")
    @ApiOperation(value = "根据uuid获取其它出入库单据详情数据", notes = "根据uuid获取其它出入库单据详情数据")
    @RequiresPermissions("whsOther-getWhsOtherWithDtl")
    public ResponseResult getWhsOtherWithDtl(@RequestParam(required = true) String uuid) {
        return iWhsOtherTaskService.getWhsOtherWithDtl(uuid);
    }

    @GetMapping("/getSapReturnedOrder")
    @ApiOperation(value = "从SAP Rest请求退货订单信息", notes = "退货出库\n" +
            "退货出库退货订单获取接口，输入交货单号，采购订单号等信息向SAP REST请求\n" +
            "示例参数 purchaseOrderNo = 4300000007, 4500104823")
    public ResponseResult getSapReturnedOrder(String purchaseOrderNo,String werks) {
        return iWhsOtherTaskService.getSapReturnedOrder("", purchaseOrderNo,werks);
    }

    @PostMapping("/cancelWhsOtherTask")
    @ApiOperation(value = "从SAP撤销其他出入库", notes = "从SAP撤销其他出入库\n" +
            "入参为其他出入库任务 uuid = 218aff6238bf4265978923742955fe32 \n")
    @RequiresPermissions("whsOther-cancelWhsOtherTask")
    public ResponseResult cancelWhsOtherTask(@RequestParam String whsOtherUuid,
                                             @RequestParam String cancelDate,
                                             @RequestParam(required = false) String cancelRemark) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            return iWhsOtherTaskCancelService.cancelWhsOtherTask(userId, whsOtherUuid, cancelDate, cancelRemark);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error().add("error", e.getMessage());
        }
    }

    @PostMapping("/updateLoadingData")
    @ApiOperation(value = "更新装卸货信息", notes = "更新装卸货信息；\n" +
            "{\n" +
            "\t\"spmsId\": \"SPMS单号\",\n" +
            "\t\"batchNo\": 破损数及批次号,\n" +
            "\t\"warehouseKeeper\": 作业仓管,\n" +
            "\t\"toWarehouseTime\": 到库时间,\n" +
            "\t\"startOperationTime\": 开始作业时间,\n" +
            "\t\"endOperationTime\": 结束作业时间,\n" +
            "\t\"handlingGroup\": 搬运组\n" +
            "}", httpMethod = "POST")
    public ResponseResult updateLoadingData(@RequestBody JSONObject doc) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            return iWhsOtherTaskService.updateLoadingData(doc, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("更新装卸货信息失败");
        }
    }

    @PostMapping("/getLoadingData")
    @ApiOperation(value = "获取装卸货信息", notes = "获取装卸货信息\n" +
            "获取交货单关联日志，入参 交货单号：spmsId = CN01-1061-20220304-002",
            httpMethod = "POST")
    public ResponseResult getLoadingData(String spmsId) {
        return ResponseResult.success().add("loadingData", iWhsOtherTaskService.getLoadingData(spmsId));
    }

//    @GetMapping("/checkException")
//    @ApiOperation(value = "checkException异常测试", notes = "checkException")
//    public ResponseResult checkException(String aa, String bb) {
//        try {
//            return iWhsOtherTaskCancelService.checkException(aa, bb);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResponseResult.error().add("error", e.getMessage());
//        }
//    }

}
