QisdaApiController.java 18.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
package com.myproject.webapp.controller.webService;

import com.google.common.base.Strings;
import com.myproject.bean.qisda.*;
import com.myproject.bean.update.*;
import com.myproject.bean.update.qisda.DNInfo;
import com.myproject.bean.update.qisda.DNItem;
import com.myproject.bean.update.qisda.OutInfo;
import com.myproject.bean.update.qisda.OutItem;
import com.myproject.dao.mongo.IBarcodeDao;
import com.myproject.dao.mongo.IDataLogDao;
import com.myproject.dao.mongo.IStoragePosDao;
import com.myproject.dao.mongo.qisda.IOutInfoDao;
import com.myproject.dao.mongo.qisda.IOutItemDao;
import com.myproject.exception.ApiException;
import com.myproject.exception.ValidateException;
import com.myproject.manager.IComponentManager;
import com.myproject.util.*;
import com.myproject.webapp.controller.qisda.util.OutInfoCache;
import com.myproject.webapp.controller.qisda.util.SoseqCache;
import com.myproject.webapp.controller.storage.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/rest/api/qisda")
public class QisdaApiController extends BaseController {

    @Autowired
    protected ITaskService taskService;

    @Autowired
    private IStoragePosDao storagePosDao;

    @Autowired
    private IOutItemDao outItemDao;

    @Autowired
    private IOutInfoDao outInfoDao;

    @Autowired
    private OutInfoCache outInfoCache;

    @Autowired
    private SoseqCache soseqCache;


    @Autowired
    private QisdaBindService qisdaBindService;

    protected final static Logger log = LogManager.getLogger(QisdaApiController.class);
    /**
     * 基础数据同步
     */
    @RequestMapping(value = "/availableInventory")
    @ResponseBody
    public Object availableInventory(HttpServletRequest request) {
        try{
            String paramInfo = request.getParameter("paramInfo");
            if(Strings.isNullOrEmpty(paramInfo)){
                //从body里面再获取一次
                Map<String, String> bodyParamMap = getParamMapFromBody(request);
                paramInfo = bodyParamMap.get("paramInfo");
            }
            log.info("收到获取库存数量接口paramInfo="+paramInfo);
            if(Strings.isNullOrEmpty(paramInfo)){
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("errorcode","参数错误,参数paramInfo为空");
                return resultMap;
            }
            List<AvailableInventoryBean> results = new ArrayList<>();
            if(!Strings.isNullOrEmpty(paramInfo)){
                List<AvailableInventoryBean> items = JsonUtil.toList(paramInfo, AvailableInventoryBean.class);
                Map<String, Integer> availableMap = storagePosDao.availableInventory();
                for (AvailableInventoryBean item : items) {
                    String key = item.toMapKey();
                    Integer availableQty = availableMap.get(key);
                    if(availableQty != null && availableQty > 0){
                        //只有那些有可用库存的才返回,没有可用库存的不需要添加到返回结果中
                        item.setQty(availableQty);
                        results.add(item);
                    }
                }
            }

            if(results.isEmpty()){
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("msg","没有数据");
                return resultMap;
            }
            return results;
        }catch(Exception e){
            Map<String,String> resultMap = new HashMap<>();
            log.error("获取可用数量接口出错",e);
            resultMap.put("errorcode","系统错误," + e.getMessage());
            return resultMap;
        }

    }



    @RequestMapping("/executeOut")
    @ResponseBody
    public String executeOut(HttpServletRequest request){
        String hSerial = request.getParameter("hSerial");
        //OutInfo outInfo = outInfoCache.getOutInfoFromCache(hSerial);
        //qisdaBindService.realBindOutInfo(outInfo);
        ResultBean resultBean = outInfoCache.checkOutOutItems(hSerial);
        return resultBean.getMsg();
    }

    /**
     * 判断条码是否可以入库
     */
    public static Barcode CISInCheck(DNInfo dnInfo, Barcode barcode) throws ValidateException{
        //纯入库
        if(barcode.getAppendInfo().isCISIn()){
            return QisdaApi.VMILocationInCheck(barcode);
        }else{
            QisdaApi.VMIMateriaReceiveJudge(dnInfo, barcode);
        }
        return null;
    }

    /**
     * 入库完成通知
     * @param barcode
     * @param task
     */
    public static void PutInFinished(Barcode barcode, DataLog task){
        //纯入库
        if(barcode.getAppendInfo().isCISIn()){
            String fullCode = barcode.getFullCodeStr();
            QisdaApi.VMILocationIn(barcode.getBarcode(),task.getPosName(),fullCode);
        }else{
            //DN单收料或Facility收料
            QisdaApi.VMIMateriaReceive(barcode, task);
        }
    }
    /**
     * 获取工单的绑定信息
     */
    @RequestMapping(value = "/soSeqLockInfo")
    @ResponseBody
    public ResultBean getSoSeqLockInfo(HttpServletRequest request){
        String soseq = receiveParamInfo(request,"soseq");
        log.info("收到工单绑定查询请求soseq="+soseq);
        OutInfo cutOutInfo = soseqCache.getCutActionInfoFromCache(soseq);
        List<ItemLockInfo> itemLockInfoList = new ArrayList<>();
        if(cutOutInfo != null){
            for (OutItem outItem : cutOutInfo.getOutItems()) {
                ItemLockInfo itemLockInfo = new ItemLockInfo();
                itemLockInfo.setSoseq(outItem.getSoseq());
                itemLockInfo.setSlotlocation(outItem.getSlotlocation());
                itemLockInfo.setPartNum(outItem.getPn());
                itemLockInfo.setQty(outItem.getQty());
                itemLockInfo.setPreLockQty(outItem.getLockQty());
                itemLockInfo.setRealLockQty(outItem.getRealLockQty());
                itemLockInfo.setTotalSendQty(outItem.getSendQty());
                itemLockInfo.setFirstSendQty(outItem.getOutQty());
                itemLockInfoList.add(itemLockInfo);
            }
        }
        return ResultBean.newOkResult(itemLockInfoList);
    }

    /**
     * 检测料盘是否绑定工单
     */
    @RequestMapping(value = "/checkLockReel",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean checkLockSo(HttpServletRequest request) {
        List<Map<String, String>> bindReelInfos = new ArrayList<>();
        try {
            String paramInfo = receiveParamInfo(request,"paramInfo");
            log.info("收到检测锁定工单请求:"+paramInfo);
            if(paramInfo == null){
                return ResultBean.newErrorResult(-1,"参数为空");
            }
            List<String> reelIds = JsonUtil.toList(paramInfo, String.class);
            for (String reelId : reelIds) {
                //指定出某盘料或单独出库,如果已经绑定,不允许出,如果未绑定直接进行绑定
                StoragePos pos = storagePosDao.findByBarcode(reelId);
                if(pos != null){
                    Barcode barcode = pos.getBarcode();
                    AppendInfo appendInfo = barcode.getAppendInfo();
                    int bindSlot = Integer.valueOf(appendInfo.getBindSlot());
                    if(bindSlot > 0 || barcode.hasCutInfo()) {
                        //已经真实绑定过
                        Map<String,String> map = new HashMap<>();
                        map.put("reelId", reelId);
                        map.put("so", appendInfo.getSo());
                        bindReelInfos.add(map);
                    }
                }else{
                    //无库存,返回Not Found
                    Map<String,String> map = new HashMap<>();
                    map.put("reelId", reelId);
                    map.put("so", "Not Found");
                    bindReelInfos.add(map);
                }
            }
        } catch (Exception e) {
            log.error("检测料盘是否绑定工单请求处理出错", e);
            return ResultBean.newErrorResult(1001,"内部错误:" + e.getMessage());
        }
        return ResultBean.newOkResult(bindReelInfos);
    }

    /**
     * 检测料盘是否绑定工单
     */
    @RequestMapping(value = "/closeSoSeq",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean closeSoSeq(HttpServletRequest request) {
        try {
            String soseq = receiveParamInfo(request,"soseq");
            log.info("收到关闭工单请求soseq="+soseq);
            if(soseq == null){
                return ResultBean.newErrorResult(-1,"未找到soseq参数");
            }
            ResultBean resultBean = outInfoCache.closeSoSeq(soseq);
            return resultBean;
        } catch (Exception e) {
            log.error("需求单请求处理出错", e);
            return ResultBean.newErrorResult(1001,"内部错误:" + e.getMessage());
        }
    }

    /**
     * 出库
     */
    @RequestMapping(value = "/out",method = RequestMethod.POST)
    @ResponseBody
    public Object out(HttpServletRequest request) {
        List<String> failedReelIdList = new ArrayList<>();
        try {
            String paramInfo = receiveParamInfo(request,"paramInfo");
            log.info("收到需求单请求:"+paramInfo);
            if(paramInfo == null){
                return ResultBean.newErrorResult(-1,"参数为空");
            }

            List<RequestOutItemBean> items = JsonUtil.toList(paramInfo, RequestOutItemBean.class);

            //key为需求单号
            Map<String, OutInfo> outInfoMap = new HashMap<>();

            log.info("需求单请求解析成功,开始处理");
            for (RequestOutItemBean itemBean : items) {
                OutItem outItem = new OutItem(itemBean);
                String hSerial = outItem.gethSerial();
                OutInfo outInfo = outInfoMap.get(hSerial);
                if(outInfo == null){
                    outInfo = outInfoDao.findByHSerial(hSerial);
                    if(outInfo == null){
                        outInfo = new OutInfo(outItem);
                    }
                }
                String reelID = outItem.getReelID();
                if(reelID != null && !reelID.isEmpty()){
                    //指定出某盘料或单独出库,如果已经绑定,不允许出,如果未绑定直接进行绑定
                    StoragePos pos = storagePosDao.findByBarcode(reelID);
                    if(pos != null){
                        Barcode barcode = pos.getBarcode();
                        AppendInfo appendInfo = barcode.getAppendInfo();
                        int bindSlot = Integer.valueOf(appendInfo.getBindSlot());
                        if(bindSlot > 0 || barcode.hasCutInfo()){
                            //已经真实绑定过
                            log.error("料盘["+reelID+"]已经真实绑定过,不允许出库");
                            failedReelIdList.add(reelID);
                        }else{
                            //未真实绑定过,可以出库,绑定
                            appendInfo.sethSerial(outItem.gethSerial());
                            appendInfo.setRefno(outItem.getRefno());
                            appendInfo.setSo("HSerial-" + outItem.gethSerial());
                            appendInfo.setSoseq("HSerial-" + outItem.gethSerial());
                            appendInfo.setSlotStr(outItem.getSlotStr());

                            appendInfo.setBindSlot("1");
                            appendInfo.setSlotIndex(1);

                            barcode.setAppendInfo(appendInfo);
                            pos.setBarcode(barcode);
                            storagePosDao.save(pos);
                            outItem.setLockQty(barcode.getAmount());
                            outItem.setRealLockQty(barcode.getAmount());
                            outItem = outItemDao.save(outItem);

                            outInfo.updateItem(outItem);
                            outInfoMap.put(hSerial, outInfo);
                        }

                    }else{
                        //未找到指定料盘
                        log.error("料盘["+reelID+"]未找到,可能已经出库,不允许出库");
                        failedReelIdList.add(reelID);
                    }
                }else{
                    //不是指定料
                    if(outItem.isReelCutAction() || outItem.isFirstReelAction() || outItem.isTailAction()){
                        //判断是否有重复,如果有重复,忽略
                        OutItem oldItem = outItemDao.findItem(outItem.gethSerial(), outItem.getSlotlocation());
                        if(oldItem != null){
                            log.error("需求单["+outItem.gethSerial()+"]的站位["+outItem.getSlotlocation()+"]已存在,忽略");
                            continue;
                        }
                    }
                    outItem = outItemDao.save(outItem);
                    outInfo.updateItem(outItem);
                    outInfoMap.put(hSerial, outInfo);
                }
            }

            //分盘需求单进行预绑定
            qisdaBindService.preBindCutOutInfoList(outInfoMap.values());

            for (OutInfo outInfo : outInfoMap.values()) {

                log.info("创建出库需求单["+outInfo.gethSerial()+"]" + outInfo.getAction());
                outInfoDao.save(outInfo);
                //新的需求单,更新缓存
                outInfoCache.addOutInfo(outInfo);
            }

        } catch (Exception e) {
            log.error("需求单请求处理出错", e);
            return ResultBean.newErrorResult(1001,"内部错误:" + e.getMessage());
        }
        if(!failedReelIdList.isEmpty()){
            String data = String.join(";",failedReelIdList);
            return ResultBean.newOkResult(data);
        }
        return ResultBean.newOkResult("");
    }


//-------------------------Private Method----------------------------------------



    /**
     * 出库接口 (出仓完成时调用)
     * @param task 任务信息
     * @param latest 本次工单第几盘料(F 第一次,L 最后一次, M 中间)
     */
    public static void OutFinished(DataLog task, Barcode barcode, String latest){
        AppendInfo appendInfo = task.getAppendInfo();
        if(appendInfo.isFirstReelAction() || appendInfo.isTailAction()){
            log.info("工单料任务,出库时发送空的FML状态");
            latest = "";
        }

        Map<String, Object> materialInfoMap = getOutMaterialInfoMap(task, latest);
        List<Map<String, Object>> cutItems = barcode.getCutItems();
        if(cutItems != null){
            //有分盘信息,需要发送多次
            for (Map<String, Object> cutItem : cutItems) {
                Object hSerial = cutItem.get("hSerial");
                materialInfoMap.put("hSerial",hSerial);
                Object so = cutItem.get("so");
                materialInfoMap.put("so",so);
                Object qty = cutItem.get("qty");
                materialInfoMap.put("qty",qty + "");
                Object flag = cutItem.get("flag");
                materialInfoMap.put("flag", flag);//子盘或母盘(L:子盘,M:母盘,其他'')
                Object slot = cutItem.get("slot");
                materialInfoMap.put("slot", slot);

                Object slotlocation = cutItem.get("slotlocation");
                materialInfoMap.put("slotserial", slotlocation + "");

                log.info(barcode.getBarcode() + "发送分盘料信息:" + cutItem);
                QisdaApi.VMILocationOut(barcode.getBarcode(),materialInfoMap);
            }
        }else{
            QisdaApi.VMILocationOut(barcode.getBarcode(), materialInfoMap);
        }
    }

    /**
     * 出仓完成时的参数
     * @param task
     * @param latest
     * @return
     */
    private static Map<String,Object> getOutMaterialInfoMap(DataLog task, String latest){
        Map<String,Object> materialInfoMap = new HashMap<String,Object>();
        AppendInfo appendInfo = task.getAppendInfo();
        materialInfoMap.put("hSerial",appendInfo.gethSerial());
        materialInfoMap.put("so",appendInfo.getSo());
        materialInfoMap.put("qty",task.getNum() + "");
        materialInfoMap.put("flag", "");//子盘或母盘(L:子盘,M:母盘,其他'')
        materialInfoMap.put("slot", appendInfo.getSlotStr());
        materialInfoMap.put("slotserial", appendInfo.getSlotIndex());

        materialInfoMap.put("action",appendInfo.getAction());
        materialInfoMap.put("partNum",task.getPartNumber());
        materialInfoMap.put("cloudLocation",task.getStorageName());//云料仓的库位
        materialInfoMap.put("vehicleID","");//料车编号
        materialInfoMap.put("vehicleLocation","");//料车架位号
        materialInfoMap.put("facility",appendInfo.getFacility());
        materialInfoMap.put("latest",latest);//本次工单第几盘料(F 第一次,L 最后一次, M 中间)
        materialInfoMap.put("reelID",task.getBarcode());
        materialInfoMap.put("location",task.getPosName());//料架库位
        String odte = DateUtil.toDateString(task.getUpdateDate(), "yyyyMMdd");
        String otme = DateUtil.toDateString(task.getUpdateDate(), "HHmmss");
        materialInfoMap.put("odte",odte);//出库日期
        materialInfoMap.put("otme",otme);//出库时间
        return materialInfoMap;
    }



    /**
     * 获取接收参数
     */
    private String receiveParamInfo(HttpServletRequest request, String paramKey){
        String paramInfo = request.getParameter(paramKey);
        if(Strings.isNullOrEmpty(paramInfo)){
            //从body里面再获取一次
            Map<String, String> bodyParamMap = getParamMapFromBody(request);
            paramInfo = bodyParamMap.get(paramKey);
        }
        return paramInfo;
    }



    private Map<String, String> getParamMapFromBody(HttpServletRequest request){
        Map<String,String> paramMap = new HashMap<>();
        try {
            String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
            String[] paramArr = params.split("&");
            for (String paramInfo : paramArr) {
                String[] arr = paramInfo.split("=");
                if(arr.length == 2){
                    paramMap.put(arr[0],arr[1]);
                }else{
                    log.error("参数错误:" + paramInfo);
                }
            }
        } catch (IOException e) {
            log.info("解析body参数出错",e);
        }
        return paramMap;
    }



}