package com.sunda.spmsweb.wmscontroller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.feign.IFeignService;
import com.sunda.spmswms.service.ISapBoxNoteService;
import com.sunda.spmswms.service.ISapDeliveryNoteLogService;
import com.sunda.spmswms.service.ISapDeliveryNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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-03-19
 */
@RestController
@RequestMapping("/sapBoxNote")
@Api(tags = "箱码信息接口", description = "箱码信息接口")
public class SapBoxNoteController {

    @Autowired
    ISapDeliveryNoteLogService iSapDeliveryNoteLogService;

    @Autowired
    ISapBoxNoteService iSapBoxNoteService;

    @Autowired
    IFeignService iFeignService;

    @Autowired
    ISapDeliveryNoteService iSapDeliveryNoteService;


    /**
     * 根据箱码，验证箱码信息
     * 1. 从SAP_BOX_NOTE中验证箱码有效
     * 2. 从SAP_BOX_NOTE_DTL中验证箱码有DTL
     * 3. 从WHS_INVENTORY_INFO验证有仓库库存
     * 4. 从WHS_STORAGE_INVENTORY中验证是否有储位库存
     * */
    @GetMapping("/validateBoxNote")
    @ApiOperation(value = "根据箱码，工厂，仓库验证箱码信息", notes = "1. 从SAP_BOX_NOTE中验证箱码有效" +
            "2. 从SAP_BOX_NOTE_DTL中验证箱码有DTL\n" +
            " 3. 从WHS_INVENTORY_INFO验证有仓库库存\n" +
            "4. 从WHS_STORAGE_INVENTORY中验证是否有储位库存")
    public ResponseResult validateBoxNote(
            @RequestParam String boxNote,
            @RequestParam(required = false) String werks,
            @RequestParam(required = false) String whsLocationCode
    ) {
        if (StringUtils.isEmpty(boxNote)) {
            return ResponseResult.error("boxNote is mandatory requried");
        }
        werks=StringUtils.isEmpty(werks)?null:werks;
        whsLocationCode=StringUtils.isEmpty(whsLocationCode)?null:whsLocationCode;
        return iSapBoxNoteService.validateBoxNote(boxNote, werks, whsLocationCode);
    }


    @RequestMapping("/getSapBoxNote")
    @ApiOperation(value = "根据包装单号获取箱码信息", notes = "根据包装单号获取箱码信息 \n" +
            "packageNote, 包装单号示例：ZX-10212100001 \n", httpMethod = "GET")
    @RequiresPermissions("sapBoxNote-getSapBoxNote")
    public ResponseResult getSapBoxNote(@RequestParam String packageNote) {
        try {
            return ResponseResult.success().add("SapBoxNote", iSapBoxNoteService.getSapBoxNote(packageNote));
        } catch (Exception e) {
            return ResponseResult.error("获取箱码失败");
        }
    }

    @GetMapping("/getDnMaterialDtl")
    @ApiOperation(value = "根据箱码获取物料详情信息", notes = "根据箱码获取物料详情信息 \n" +
            "boxNote, 包装单号示例：ZX-10215800001-0404 \n")
    public ResponseResult getDnMaterialDtl(@RequestParam String boxNote) {

        try {
            return ResponseResult.success().add("boxNoteDtl", iSapBoxNoteService.getDnMaterialDtl(boxNote));
        } catch (Exception e) {
            return ResponseResult.error("获取箱码下物料详情数据失败");
        }
    }


    @GetMapping("/getDnMaterialDtlByPackage")
    @ApiOperation(value = "根据包装单号获取物料详情信息", notes = "根据包装单号获取物料详情信息 \n" +
            "packageNote, 包装单号示例：ZX-10215800001 \n")
    public ResponseResult getDnMaterialDtlByPackage(@RequestParam String packageNote) {

        try {
            return ResponseResult.success().add("boxNoteDtl", iSapBoxNoteService.getDnMaterialDtlByPackage(packageNote));
        } catch (Exception e) {
            return ResponseResult.error("获取箱码下物料详情数据失败");
        }
    }



    @GetMapping("/getNewBoxList")
    @ApiOperation(value="根据箱码获取拆箱信息",notes="根据箱码获取拆箱信息")
    public ResponseResult getNewBoxList(@RequestParam String boxNote){
        return iSapBoxNoteService.checkNewBox(boxNote);
    }

//    @PostMapping("/getSparePartsDnDataToSap")
//    @ApiOperation(value = "交货单提交SAP(备件货)", notes = "根据交货单号查询交货单表头及行项目信息提交SAP\n" +
//            "1.交货单号示例：deliveryNoteSap = 1180066369；过账日期用户可输入，示例 postingDate = 20210330；\n" +
//            "2.首先将 过账日期字段 写入数据库；\n" +
//            "3.提交前单据状态一定是 6 —— 待提交SAP\n" +
//            "4.\"O_TYPE\": \"S\" ———— 提交SAP成功，单据完成；更新交货单状态为 8 —— 提交SAP完成；\n" +
//            "5.\"O_TYPE\": \"E\" ———— 提交SAP不成功，单据未完成，更新单据状态为 7 —— 提交SAP失败； 显示错误信息给用户；（上游单据锁住，提交失败、过账日期错误、网络错误等原因会导致提交失败。）" +
//            "6.以上各步骤均要写入用户操作日志信息。" )
//    public ResponseResult getSparePartsDnDataToSap(String deliveryNoteSap, String postingDate){
//        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
//        try{
//            /** 先将过账日期字段更新到交货单，再获取将要提交到SAP的交货单表头及明细信息 */
//            Map<String, Object> getDnDataToSap = iSapBoxNoteService.getDnDataToSap(deliveryNoteSap, postingDate, userId);
//            if (getDnDataToSap == null){
//                return ResponseResult.error("获取交货单信息出错");
//            }
//            String message = iFeignService.sendRestToSap(JSONObject.parseObject(getDnDataToSap.toString()));
//            if (StringUtils.isEmpty(message)){
//                iSapDeliveryNoteLogService.insertSapDeliveryNoteLogM(deliveryNoteSap, userId, UPDATE_SAP_DELIVERY_NOTE7);
//                return ResponseResult.error("获取SAP返回信息出错");
//            }
//            JSONObject RETURN_DATA = JSONObject.parseObject(message).getJSONObject("RESPONSE").getJSONObject("RETURN_DATA");
//            if ("S".equals(RETURN_DATA.getString("O_TYPE"))) {
//                iSapDeliveryNoteService.updateSapDeliveryNoteStatus(deliveryNoteSap, "6", "8");
//                iSapDeliveryNoteLogService.insertSapDeliveryNoteLogM(deliveryNoteSap, userId, UPDATE_SAP_DELIVERY_NOTE8);
//                return ResponseResult.success(RETURN_DATA.getString("O_MSTXT")).add("sapReturnMsg", RETURN_DATA.getString("O_MSTXT"))
//                        .add("sapReturnTotalMsg", JSONObject.parseObject(message)).add("sendSapMsg", getDnDataToSap);
//            }
//            iSapDeliveryNoteService.updateSapDeliveryNoteStatus(deliveryNoteSap, "6", "7");
//            iSapDeliveryNoteLogService.insertSapDeliveryNoteLogM(deliveryNoteSap, userId, UPDATE_SAP_DELIVERY_NOTE7);
//            return ResponseResult.error(RETURN_DATA.getString("O_MSTXT")).add("sapReturnMsg", RETURN_DATA.getString("O_MSTXT"))
//                    .add("sapReturnTotalMsg", JSONObject.parseObject(message)).add("sendSapMsg", getDnDataToSap);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResponseResult.error();
//        }
//    }

}
