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.ISapRepackage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @program: spms
 * @description:
 * @author: Wayne Wu
 * @create: 2021-10-21 10:29
 **/
@Slf4j
@RestController
@RequestMapping("/sapRepackage")
@Api(tags = "拆箱", description = "拆箱")
public class SapRepackageController {

    @Autowired
    ISapRepackage iSapRepackage;

    @RequestMapping("/sapRepackage")
    @ApiOperation(value = "拆箱任务接口", notes = "拆箱任务接口\n" +
            "1.业务基本原则：\n" +
            "  1.1.一次只拆一箱，如果原箱某行完全移出，则原箱该行 DOMESTIC_QTY_RECEIVED 国内实际数量字段调整为 0；\n" +
            "  1.2.前端请求参数只传原箱更新的行项目数据，以及新箱行项目数据；\n" +
            "  1.3.新箱码由后端生成；生成规则：CX- + 仓库地点代码 + 七位序列 + -0101，如：CX-10611000001-0101；\n" +
            "2.业务执行过程：\n" +
            "  2.1.根据前端传参，检查数据合法性有效性；库存数据合法有效；\n" +
            "  2.2.根据前端传参，后端首先生成新箱码；\n" +
            "  2.3.根据原箱码、新箱码、包装单号信息、箱码明细拼接请求SAP；\n" +
            "  2.4.SAP请求异常或失败，忽略一切操作，仅记录日志；\n" +
            "  2.5.SAP请求成功则：\n" +
            "    2.5.1.更新原箱箱码信息；\n" +
            "    2.5.2.逐行更新原箱码国内实际数量字段信息；\n" +
            "    2.5.3.根据新箱码，创建一条新箱码数据，除箱码以外信息从原箱带出保留到新箱码数据中；\n" +
            "    2.5.4.逐行创建新箱码明细，除数量外其余信息从原箱码明细行带出保存到新箱码明细行中；\n" +
            "    2.5.5.新增储位库存，记录储位库存增加日志；\n" +
            "    2.5.6.新增仓库库存，记录仓库库存增加日志；\n" +
            "    2.5.7.在步骤2.5.1——2.5.6过程如果执行错误，则程序回滚，返回错误信息；\n" +
            "3.前端请求拆箱请求参数结构参考如下:\n" +
            "{\n" +
            "\t\"boxNote\": \"BJ-10218800042-0305\",\n" +
            "\t\"werks\": \"CN01\",\n" +
            "\t\"whsLocationCode\": \"1061\",\n" +
            "\t\"boxNoteDtl\": [{\n" +
            "\t\t\"boxNote\": \"BJ-10218800042-0305\",\n" +
            "\t\t\"item\": 1,\n" +
            "\t\t\"domesticQtyReceived\": 6\n" +
            "\t}],\n" +
            "\t\"newBoxNoteDtl\": [{\n" +
            "\t\t\"item\": 1,\n" +
            "\t\t\"domesticQtyReceived\": 2\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("sapRepackage-sapRepackage")
    public ResponseResult repackageBoxNote(@RequestBody JSONObject doc) {
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSapRepackage.sapRepackageBox(doc, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("拆箱请求失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getSapBoxNoteListPage")
    @ApiOperation(value = "获取箱码分页列表——带子箱", notes = "获取箱码分页列表——带子箱\n" +
            "获取箱码分页列表——带子箱", httpMethod = "POST")
    @RequiresPermissions("sapRepackage-getSapBoxNoteListPage")
    public ResponseResult getSapBoxNoteListPage(@RequestParam String werks,
                                                @RequestParam String whsLocationCode,
                                                String boxNote,
                                                @RequestParam(defaultValue = "1") Integer pageNo,
                                                @RequestParam(defaultValue = "20") Integer pageSize) {
        try {
            return iSapRepackage.getSapBoxNoteListPage(werks, whsLocationCode, boxNote, pageNo, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("获取箱码分页请求失败").add("error", e.getMessage());
        }
    }


}
