CustomsDataInterface.java 5.25 KB
package com.erry.iclear.dateInterface.api;

import com.erry.iclear.dateInterface.api.request.Mawb;
import com.erry.iclear.dateInterface.api.response.ResultRS;
import com.erry.iclear.dateInterface.common.Constant;
import com.erry.iclear.dateInterface.entity.IClearApiLog;
import com.erry.iclear.dateInterface.service.IClearApiLogService;
import com.erry.iclear.dateInterface.service.MawbDetailService;
import com.erry.iclear.dateInterface.service.MawbService;
import com.erry.iclear.dateInterface.util.MD5Util;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/customs")
@Slf4j
public class CustomsDataInterface {

    private Gson gson = new Gson();

    @Autowired
    private MawbService mawbService;

    @Autowired
    private MawbDetailService  mawbDetailServicel;

    @Autowired
    private IClearApiLogService iClearApiLogService;


    @ApiOperation(value = "推送主单数据", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/customs/sendMawb")
    @PostMapping("/sendMawb")
    @ResponseBody
    public ResultRS sendMawb(HttpServletRequest request
                                , @RequestBody Mawb mawb) {
        log.info("接收到主单数据:" + gson.toJson(mawb));
        ResultRS result = new ResultRS();
        result.setSuccess(Boolean.FALSE);

        if(Objects.equals(mawb,null)){
            result.setCode("-7");
            result.setMsg("主单信息不能为空");
            return result;
        }
        //0、保存请求信息,保存到iClear的  SQLServer
        String appId=(String) request.getHeader("Appid");
        String appkey=(String) request.getHeader("Appkey");
        IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLog(mawb,appId,appkey));

        if(!Objects.equals(saveIClearApiLogResult,null)){
            log.info("请求保存成功,msgId:"+saveIClearApiLogResult.getMsgId());
            mawb.setMsgId(saveIClearApiLogResult.getMsgId());
//            mawb.getMawbDetailList().forEach(d->{
//                d.setMsgId(saveIClearApiLogResult.getMsgId());
//            });
            Mawb saveMawbResult= mawbService.saveMawb(mawb);
            if(!Objects.equals(saveMawbResult,null)){
                log.info("请求主单数据保存成功,msgId:"+saveMawbResult.getMsgId());
            }else{
                log.info("请求主单数据保存失败,msgId:"+saveMawbResult.getMsgId());
            }
        }else{
            log.info("请求保存失败,msgId:"+saveIClearApiLogResult.getMsgId());
        }




        //1、TODO 校验用户名密码
        result=this.checkAppSecret(request, result);



        //2、TODO 校验主单
        if(result.getSuccess()==true){
            result.setSuccess(mawbService.checkMawb(mawb));
        }
        //3、TODO 校验子单
        if(result.getSuccess()==true){

        }
        //4、TODO 保存主单
        if(result.getSuccess()==true){

        }


        return result;
    }


    /**
     * 校验用户
     * @param request
     * @param result
     */
    private ResultRS checkAppSecret(HttpServletRequest request, ResultRS result) {
        String appId=(String) request.getHeader("Appid");
        String appkey=(String) request.getHeader("Appkey");
        String timeStamp=(String) request.getHeader("TimeStamp");
        String token= (String) request.getHeader("token");





        if(StringUtils.isBlank(appId)){
            result.setCode("-1");
            result.setMsg("Appid 不能为空");
            return result;
        }


        if(StringUtils.isBlank(appkey)){
            result.setCode("-2");
            result.setMsg("Appkey 不能为空");
            return result;
        }


        if(StringUtils.isBlank(timeStamp)){
            result.setCode("-6");
            result.setMsg("TimeStamp 不能为空");
            return result;
        }


        if(StringUtils.isBlank(token)){
            result.setCode("-4");
            result.setMsg("TimeStamp 不能为空");
            return result;
        }


        HashMap<String,String> secretMap = Constant.appIdSecurity.get(appId);
        if(secretMap==null){
            result.setCode("-100");
            result.setMsg("系统异常,该appId没有注册");
            return result;
        }

        String secretInSys = secretMap.get(appkey);
        if (StringUtils.isBlank(secretInSys)) {
            result.setCode("-101");
            result.setMsg("系统异常,该appId没有权限/密码");
            return result;
        }

        String md5Str= MD5Util.md5(appId+appkey+secretInSys+timeStamp);
        if(!token.equals(md5Str)){
            result.setCode("-4");
            result.setMsg("Token验证失败");
            return result;
        }

        result.setSuccess(true);
        return result;
    }


}