package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.IWhsMoveService;
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.RestController;

/**
 * <p>
 * 移仓任务表头 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/whsMove")
@Api(tags = "移仓任务接口", description = "移仓任务接口")
public class WhsMoveController {

    @Autowired
    IWhsMoveService iWhsMoveService;

//    @RequestMapping("/getWhsMove")
//    @ApiOperation(value = "获取全部移仓任务列表", notes = "获取全部移仓任务列表", httpMethod = "GET")
//    public ResponseResult getWhsMove() {
//        try {
//            return ResponseResult.success().add("getWhsMove", iWhsMoveService.whsMoveList());
//        } catch (Exception e) {
//            return ResponseResult.error("获取移仓任务信息出错");
//        }
//    }

    @RequestMapping("/getWhsMoveList")
    @ApiOperation(value = "获取移仓任务表头分页", notes = "获取移仓任务表头分页\n" +
            "入参示例：\n" +
            "{\n" +
            "\t\"whsMoveNo\": \"CN01-20210531-001\",\n" +
            "\t\"moveType\": \"123\",\n" +
            "\t\"goodsType\": \"M\",\n" +
            "\t\"materialNo\": \"130012803\",\n" +
            "\t\"werksMoveOut\": \"CN01\",\n" +
            "\t\"whsLocationCodeMoveOut\": \"1061\",\n" +
            "\t\"werksMoveIn\": \"CN01\",\n" +
            "\t\"whsLocationCodeMoveIn\": \"1011\",\n" +
            "\t\"beginDate\": \"2021-05-31\",\n" +
            "\t\"endDate\": \"2021-05-31\",\n" +
            "\t\"spmsStatus\": \"1,2\",\n" +
            "\t\"pageNo\": 1,\n" +
            "\t\"pageSize\": 20\n" +
            "}\n" +
            "spmsStatus 可传多个值，码值说明：单据状态(0删除;1草稿;2移出可执行;3移出提交(移入可执行);4移入提交(待上传SAP);5提交SAP成功;6完成(计算库存);\n" +
            "goodsType：X备件货，M贸易货；beginDate, endDate 为任务创建日期搜索区间；\n", httpMethod = "POST")
    @RequiresPermissions("whsMove-getWhsMoveList")
    public ResponseResult getWhsMoveList(@RequestBody JSONObject doc){
        try {
            return ResponseResult.success().add("getWhsMoveList", iWhsMoveService.getWhsMoveList(doc));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getWhsMoveDtls")
    @ApiOperation(value = "获取移仓任务明细", notes = "获取移仓任务明细\n" +
            "入参为移仓任务 uuid 示例值：test001 或 test002", httpMethod = "POST")
    @RequiresPermissions("whsMove-getWhsMoveDtls")
    public ResponseResult getWhsMoveDtls(String whsMoveUuid){
        try {
            if (StringUtils.isEmpty(whsMoveUuid)){
                return ResponseResult.error("请求参数不能为空");
            }
            return ResponseResult.success().add("getWhsMoveDtls", iWhsMoveService.getWhsMoveDtls(whsMoveUuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/addWhsMove")
    @ApiOperation(value = "创建或更新移仓任务及明细", notes = "创建或更新移仓任务及明细\n" +
            "{\n" +
            "\t\"whsMove\": {\n" +
            "\t\t\"whsMoveUuid\": \"icyuhbjifgygvvhujhhhjjhjhjhhuhvbhj\",\n" +
            "\t\t\"werksMoveOut\": \"CN01\",\n" +
            "\t\t\"whsLocationCodeMoveOut\": \"1061\",\n" +
            "\t\t\"moveOutRemark\": \"1061贸易货移仓移出\",\n" +
            "\t\t\"werksMoveIn\": \"CN01\",\n" +
            "\t\t\"whsLocationCodeMoveIn\": \"1011\",\n" +
            "\t\t\"remark\": null,\n" +
            "\t\t\"moveType\": \"123\",\n" +
            "\t\t\"goodsType\": \"M\",\n" +
            "\t\t\"moveInRemark\": \"1011贸易货移仓移入\",\n" +
            "\t\t\"currentStatus\": \"1\",\n" +
            "\t\t\"targetStatus\":\"1\",\n" +
            "\t\t\"createDate\": \"2021-05-31\"\n" +
            "\t},\n" +
            "\t\"whsMoveDtl\": [{\n" +
            "\t\t\"autoId\": 5,\n" +
            "\t\t\"actualOutQty\": null,\n" +
            "\t\t\"moveOutStorage\": null,\n" +
            "\t\t\"moveOutRemark\": null,\n" +
            "\t\t\"materialNo\": null,\n" +
            "\t\t\"costCenter\": null,\n" +
            "\t\t\"moveInStorage\": null,\n" +
            "\t\t\"supplierNo\": null,\n" +
            "\t\t\"whsMoveUuid\": \"test001\",\n" +
            "\t\t\"remark\": null,\n" +
            "\t\t\"itemNo\": 10,\n" +
            "\t\t\"basicUnit\": null,\n" +
            "\t\t\"moveInRemark\": null,\n" +
            "\t\t\"boxNote\": \"YC-test001-0101\",\n" +
            "\t\t\"materialZhDesc\": null,\n" +
            "\t\t\"materialEnDesc\": null,\n" +
            "\t\t\"plannedOutQty\": 1,\n" +
            "\t\t\"plannedOutStorage\": null,\n" +
            "\t\t\"actualInQty\": null\n" +
            "\t}]\n" +
            "}\n" +
            "参数简要说明：whsMoveUuid 移仓任务uuid为空时，新建移仓任务，新建时移出工厂、仓库、创建日期字段必输；提交时行项目长度不能为0；更新时，明细中的主键 autoId 不能为空。", httpMethod = "POST")
    @RequiresPermissions("whsMove-addWhsMove")
    public ResponseResult addWhsMove(@RequestBody JSONObject doc){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWhsMoveService.addWhsMove(doc, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/whsMoveToSap")
    @ApiOperation(value = "移仓任务移入完成提交SAP", notes = "移仓任务移入完成提交SAP\n" +
            "提交前状态为 4移入提交待上传SAP；提交SAP失败忽略；提交SAP成功更新到状态 5提交SAP成功；" +
            "成功后计算移出移入两边仓库库存，库存更新成功更新到状态 6(库存计算)任务完成。\n" +
            "示例任务uuid：备件货 376807a3d2714564b52871faeed3fd09\n" +
            "贸易货 a2074f01d70846b5b3c7518c80f560d2 或 3f32abbfc5bf485787172572c144d338", httpMethod = "POST")
    @RequiresPermissions("whsMove-whsMoveToSap")
    public ResponseResult whsMoveToSap(String whsMoveUuid, String postingDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            //return ResponseResult.error("接口未完成");
            return iWhsMoveService.whsMoveToSap(whsMoveUuid, postingDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
