package com.shcd.service.impl;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shcd.mapper.ShipLockShipStatusMapper;
import com.shcd.service.ShipLockShipStatusService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.shcd.entity.ShipLockShipStatus;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class ShipLockShipStatusServiceImpl extends ServiceImpl<ShipLockShipStatusMapper, ShipLockShipStatus> implements ShipLockShipStatusService {
    private static final String[] status = {"预申报", "候泊中", "待闸中", "过闸中", "已过闸"};
    @Value("${shipStatusUrl}")
    private String shipStatusUrl;
    @Value("${infoSecretId}")
    private String infoSecretId;
    @Value("${infoSecretKey}")
    private String infoSecretKey;

    @Override
    public ReturnT<String> saveShipStatus() {
        try {
            for (String s : status) {
                HttpResponse execute = HttpUtil.createGet(shipStatusUrl + "status=" + s).
                        header("secretId", infoSecretId).
                        header("secretKey", infoSecretKey).execute();
                execute.close();
                HashMap hashMap = JSONObject.parseObject(execute.body(), HashMap.class);
                if (new Integer(0).equals(hashMap.get("code"))){
                    List<ShipLockShipStatus> data = JSONObject.parseArray(hashMap.get("data").toString(), ShipLockShipStatus.class);
                    this.saveOrUpdateBatch(data);
                }
            }
            return ReturnT.SUCCESS;
        } catch (HttpException e) {
            XxlJobLogger.log(e.getCause() + "------" + e.getMessage());
        }
        return ReturnT.FAIL;
    }
}




