package com.zehong.system.service.impl.shelf;

import com.alibaba.fastjson.JSONObject;
import com.zehong.system.service.impl.shelf.annotation.DockingTaskType;
import com.zehong.system.service.impl.shelf.annotation.PlatformDockingTaskLog;
import com.zehong.system.service.impl.shelf.annotation.SendNotification;
import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.StringUtils;
import com.zehong.common.utils.http.HttpUtils;
import com.zehong.system.domain.shelf.TPlatformDockingTask;
import com.zehong.system.domain.shelf.TShelfInfo;
import com.zehong.system.domain.shelf.TShelfStorageLocation;
import com.zehong.system.mapper.shelf.TPlatformDockingTaskMapper;
import com.zehong.system.mapper.shelf.TShelfInfoMapper;
import com.zehong.system.mapper.shelf.TShelfStorageLocationMapper;
import com.zehong.system.service.shelf.ITShelfApiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 货架第三方接口
 */
@Service
public class ITShelfApiServiceImpl implements ITShelfApiService{

    @Resource
    private TShelfStorageLocationMapper shelfStorageLocationMapper;

    @Resource
    private TShelfInfoMapper shelfInfoMapper;

    /**
     * 获取料架状态接口
     */
    private final static String RACK_STATUS_URL = "/api/TyChargingData/RackStatus";

    /**
     * 料架出入库接口
     */
    private final static String RACK_IN_OUT_PUT_URL = "/api/TyChargingData/RackInOutput";

    /**
     * 获取料架所有储位信息
     */
    private final static String LOOK_ALL_LOCATION_URL = "/api/TyChargingData/LookAllLocation";

    @Value("${shelf.apiUrl}")
    private String shelfApiUrl;

    /**
     * 获取料架状态
     * @param param 请求参数
     */
    @Override
    public JSONObject getRackStatus(String param) {
        String result = HttpUtils.sendPost(shelfApiUrl + RACK_STATUS_URL,param);
        if(StringUtils.isEmpty(result)) throw new CustomException("获取料架状态信息失败");
        return JSONObject.parseObject(result);
    }


    /**
     * 料架出库入库接口
     * @param param 入参
     */
    @Override
    @PlatformDockingTaskLog(type = DockingTaskType.CALL_RACK_SYSTEM)
    public String rackInOutput(String param){
        String result = HttpUtils.sendPost(shelfApiUrl + RACK_IN_OUT_PUT_URL,param);
        if(StringUtils.isEmpty(result)) throw new CustomException("料架出入库接口错误");
        return result;
    }

    /**
     * 获取料架所有储位信息
     * @param param 请求参数
     */
    @Override
    public JSONObject lookAllLocation(String param){
        String result = HttpUtils.sendPost(shelfApiUrl + LOOK_ALL_LOCATION_URL,param);
        if(StringUtils.isEmpty(result)) throw new CustomException("获取料架所有储位信息错误");
        return JSONObject.parseObject(result);
    }


    /**
     * 获取储出储成功后反馈
     * @param request 入参
     */
    @Override
    @SendNotification
    @PlatformDockingTaskLog(type = DockingTaskType.RECEIVE_RACK_SYSTEM_INFO)
    @Transactional(rollbackFor = Exception.class)
    public JSONObject receiveFeedBackInfo(JSONObject request) {
        try {
            JSONObject result = new JSONObject();
            TShelfInfo shelfInfo = shelfInfoMapper.selectTShelfInfoByShelf(request.getString("SHELF"));
            request.put("SHELFID",shelfInfo.getfShelfId());
            //更新位置状态
            TShelfStorageLocation tShelfStorageLocation = new TShelfStorageLocation();
            tShelfStorageLocation.setLocation(request.getString("LOCATION"));
            tShelfStorageLocation.setShelfId(shelfInfo.getfShelfId());
            if("I".equals(request.getString("OPTYPE"))){
                tShelfStorageLocation.setInboundTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(request.getString("OPERATEDATE")));
                tShelfStorageLocation.setRid(request.getString("RID"));
            }
            if("O".equals(request.getString("OPTYPE"))){
                tShelfStorageLocation.setOutboundTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(request.getString("OPERATEDATE")));
            }
            shelfStorageLocationMapper.updateTShelfStorageLocationByLocationAndShelfId(tShelfStorageLocation);
            result.put("FLAG","0");
            result.put("MESSAGE","");
            result.put("ERRORCODE","");
            return result;
        } catch (ParseException e) {
            throw new CustomException("储出储成功后反馈接口错误");
        }
    }
}
