package com.sunda.spmsweb.overseacontroller;


import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaWhsInDTO;
import com.sunda.spmsoversea.dto.OverseaWhsInQueryDTO;
import com.sunda.spmsoversea.dto.OverseaWhsInUpdateDTO;
import com.sunda.spmsoversea.service.IOverseaWhsInDtlService;
import com.sunda.spmsoversea.service.IOverseaWhsInService;
import com.sunda.spmsweb.util.JWTUtil;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 海外仓入库任务表头 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-08-26
 */
@RestController
@RequestMapping("/overseaWhsIn")
@Api(tags = "海外仓入库任务", description = "海外仓入库任务")
public class OverseaWhsInController {

    @Autowired
    IOverseaWhsInService iOverseaWhsInService;

    @Autowired
    IOverseaWhsInDtlService iOverseaWhsInDtlService;

    @RequestMapping("/createOverseaWhsIn")
    @ApiOperation(value = "创建入库任务", notes = "创建入库任务\n" +
            "请求参数示例：overseaWhsInDTO = \n" +
            "{\n" +
            "\t\"createDate\": \"2021-11-08\",\n" +
            "\t\"werks\": \"GF02\",\n" +
            "\t\"whsLocationCode\": \"2006\",\n" +
            "\t\"sapDeliveryNote\": \"2000000001\",\n" +
            "\t\"overseaDeliveryNoteItem\": [{\n" +
            "\t\t\"sapDeliveryNote\": \"2000000001\",\n" +
            "\t\t\"sapDeliveryNoteItem\": \"10\",\n" +
            "\t\t\"boxNote\": \"SP-12345678001-0201\",\n" +
            "\t\t\"boxNoteItem\": \"1\"\n" +
            "\t}]\n" +
            "}" +
            "\n" +
            "入库任务状态：(默认0待入库;1入库中;2入库完成;3审批通过;4提交SAP成功;5提交SAP失败;6已撤销;7关闭;)\n" +
            "1.检验数据有效性，确保传入的数据不跨交货单号；每个交货单号，交货单行号都存在；\n" +
            "2.给海外仓收货任务交货单表，按 交货单号 更新状态为收货中，记录收货工厂，禁止SAP更新；\n" +
            "3.给海外仓收货交货单表 按交货单号 记录到货日志；\n" +
            "4.根据传入交货单号，交货单行号，箱码及行号信息，更新 OVERSEA_PACKAGE_LIST_BOX 表状态，记录对应箱码行号已到货及到货日期，收货工厂仓库；\n" +
            "5.根据交货单号信息，创建海外入库任务表头；\n" +
            "6.查询 OVERSEA_PACKAGE_LIST_BOX 将有效行数据，复写到入库任务明细表中；\n" +
            "7.记录入库任务创建日志；\n" +
            "8.记录箱码海外到货；\n" +
            "返回信息结构参考：\n" +
            "{\n" +
            "\t\"code\": 200,\n" +
            "\t\"msg\": \"请求成功\",\n" +
            "\t\"data\": {\n" +
            "\t\t\"overseaWhsInDtl\": [{}],\n" +
            "\t\t\"overseaWhsIn\": {}\n" +
            "\t}\n" +
            "}",
            httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-createOverseaWhsIn")
    public ResponseResult createOverseaWhsIn(@RequestBody OverseaWhsInDTO overseaWhsInDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsInService.createOverseaWhsIn(overseaWhsInDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaWhsInPage")
    @ApiOperation(value = "获取入库任务表头分页", notes = "获取收货任务分页\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"beginDate\": \"\",\n" +
            "\t\"cabinetNumber\": \"\",\n" +
            "\t\"createUser\": \"999947\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"endDate\": \"\",\n" +
            "\t\"sapDeliveryNote\": \"\",\n" +
            "\t\"sapPackageList\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"uuidWhsIn\": \"\",\n" +
            "\t\"werks\": \"\",\n" +
            "\t\"whsInNo\": \"\",\n" +
            "\t\"whsLocationCode\": \"\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-getOverseaWhsInPage")
    public ResponseResult getOverseaWhsInPage(@RequestBody OverseaWhsInQueryDTO overseaWhsInQueryDTO){
        try {
            return ResponseResult.success().add("overseaWhsInList", iOverseaWhsInService.getOverseaWhsInPage(overseaWhsInQueryDTO));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaWhsInDtlList")
    @ApiOperation(value = "获取入库任务明细", notes = "获取入库任务明细\n" +
            "示例参数：\n" +
            "uuidWhsIn = c68abdf9217f4d0da8137d4af0d8a27f \n" +
            "20220218 修改：\n" +
            "    根据入库任务明细每行数据中的 需求号 需求行号，关联出原请购单信息，返回信息结构增加如下字段\n" +
            "    spmsWsId —— 车间请购单号\n" +
            "    rowIdWs —— 车间请购单行号\n" +
            "    workshopCode —— 请购车间代码\n" +
            "    userId —— 车间请购单创建人工号\n" +
            "    username —— 车间请购单创建人姓名\n", httpMethod = "GET")
    @RequiresPermissions("overseaWhsIn-getOverseaWhsInDtlList")
    public ResponseResult getOverseaWhsInDtlList(String uuidWhsIn){
        try {
            return ResponseResult.success().add("getOverseaWhsInDtlList", iOverseaWhsInDtlService.getOverseaWhsInDtlList(uuidWhsIn));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateOverseaWhsIn")
    @ApiOperation(value = "更新入库任务及明细", notes = "更新入库任务及明细\n" +
            "更新入库任务及明细\n" +
            "SPMS_STATUS: 入库任务状态(默认0待入库; 1入库中; 2入库完成; 3审批通过; 4提交SAP成功; 5提交SAP失败; 6已撤销; 7关闭;)\n" +
            "保存/提交/审批更新单据，更新入库任务表头及明细，状态变化包含：0-1，0-2，1-1，1-2，2-3（加库存）；\n" +
            "审批通过时，即状态变化 2-3（加库存），同时将箱码+行号对应实收信息记录到 装箱单明细箱码明细 表中；\n", httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-updateOverseaWhsIn")
    public ResponseResult updateOverseaWhsIn(@RequestBody OverseaWhsInUpdateDTO overseaWhsInUpdateDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsInService.updateOverseaWhsIn(overseaWhsInUpdateDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelOverseaWhsIn")
    @ApiOperation(value = "入库任务撤销/退回操作", notes = "入库任务撤销/退回操作\n" +
            "入库任务撤销/退回操作\n" +
            "SPMS_STATUS: 入库任务状态(默认0待入库; 1入库中; 2入库完成; 3审批通过; 4提交SAP成功; 5提交SAP失败; 6已撤销; 7关闭;)\n" +
            "撤销/退回单据到上一步，更新表头，状态变化包含：2-1，3-1（减库存），4-1（减库存），5-1（减库存）；\n" +
            "cancelPostingDate 只在从SAP撤销单据时需要传值，其余状态忽略；3-1 和 5-1 回撤操作过程一致；\n" +
            "涉及库存操作时，将箱码每一行实收信息进行更新；\n" +
            "入库任务所有撤回都会校验关联的收货任务/交货单状态，如果不是-收货中，则禁止一切撤回操作——20211201\n", httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-cancelOverseaWhsIn")
    public ResponseResult cancelOverseaWhsIn(String uuidWhsIn, String cancelPostingDate, int dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsInService.cancelOverseaWhsIn(uuidWhsIn, cancelPostingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/toSapOverseaWhsIn")
    @ApiOperation(value = "入库任务提交SAP", notes = "入库任务提交SAP\n" +
            "入库任务提交SAP\n" +
            "SPMS_STATUS: 入库任务状态(默认0待入库; 1入库中; 2入库完成; 3审批通过; 4提交SAP成功; 5提交SAP失败; 6已撤销; 7关闭;)\n" +
            "提交SAP，更新表头，状态变化包含：3-4，3-5，5-4，5-5；\n" +
            "\n", httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-toSapOverseaWhsIn")
    public ResponseResult toSapOverseaWhsIn(String uuidWhsIn, String postingDate, int dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsInService.toSapOverseaWhsIn(uuidWhsIn, postingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeOverseaWhsIn")
    @ApiOperation(value = "入库任务关闭", notes = "入库任务关闭\n" +
            "入库任务关闭\n" +
            "SPMS_STATUS: 入库任务状态(默认0待入库; 1入库中; 2入库完成; 3审批通过; 4提交SAP成功; 5提交SAP失败; 6已撤销; 7关闭;)\n" +
            "入库任务关闭，更新表头，状态变化包含：0-7，1-7；\n" +
            "\n", httpMethod = "POST")
    @RequiresPermissions("overseaWhsIn-closeOverseaWhsIn")
    public ResponseResult closeOverseaWhsIn(String uuidWhsIn, int dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsInService.closeOverseaWhsIn(uuidWhsIn, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaWhsInList")
    @ApiOperation(value = "获取交货单关联的所有入库任务", notes = "获取交货单关联的所有入库任务\n" +
            "示例参数：0700442836 ", httpMethod = "POST")
    public ResponseResult getOverseaWhsInList(@RequestParam String sapDeliveryNote){
        try {
            return ResponseResult.success().add("overseaWhsInList", iOverseaWhsInService.getOverseaWhsInList(sapDeliveryNote));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/getOverseaWhsInHead")
    @ApiOperation(value = "获取入库任务详情", notes = "获取入库任务详情\n" +
            "示例参数：\n" +
            "whsInNo = GF03-20211228-001 \n" +
            "\n", httpMethod = "POST")
    public ResponseResult getOverseaWhsInHead(String whsInNo){
        try {
            return iOverseaWhsInDtlService.getOverseaWhsInHead(whsInNo);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
