package com.cpxt.web.task;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cpxt.biz.domain.BizCar;
import com.cpxt.biz.domain.BizCarState;
import com.cpxt.biz.domain.TrackTemp;
import com.cpxt.biz.mapper.BizCarMapper;
import com.cpxt.biz.mapper.BizCarStateMapper;
import com.cpxt.biz.mapper.TrackTempMapper;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.common.utils.Helper;
import com.e6yun.project.sdk.api.E6SdkApi;
import com.e6yun.project.sdk.api.exception.ApiException;
import com.e6yun.project.sdk.api.pojo.basevo.basic.VehicleInfoResp;
import com.e6yun.project.sdk.api.pojo.business.GetVehicleInfoVO;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component("getLocationTask")
public class GetLocationTask {

    // 域名
    private static final String ADDRESS = "http://api.e6yun.com/";

    private static final String APP_KEY_JSON = "[{ 'APP_KEY':'FEB9F91D-BCD7-4DC2-97D1-CB599EEAF9D5','SECRET_KEY':'A8211E8F-9E95-4C80-9414-CD5E1F675FEF' }, " +
            "{ 'APP_KEY':'ECB3CDEA-ACD1-4E62-840D-717DCFBEAE79','SECRET_KEY':'BE3544F5-C488-41E8-9DA5-0172BF767210' }]";
    // 公钥
    private static final String APP_KEY = "FEB9F91D-BCD7-4DC2-97D1-CB599EEAF9D5";
    // 密钥
    private static final String SECRET_KEY = "A8211E8F-9E95-4C80-9414-CD5E1F675FEF";

    // 公钥
    private static final String APP_KEY2 = "ECB3CDEA-ACD1-4E62-840D-717DCFBEAE79";
    // 密钥
    private static final String SECRET_KEY2 = "BE3544F5-C488-41E8-9DA5-0172BF767210";

    /*添加代码 begin*/
    private static GetLocationTask getLocationTask;

    @Autowired
    private BizCarStateMapper stateMapper;
    @Autowired
    private BizCarMapper carMapper;
    @Autowired
    private TrackTempMapper trackMapper;

    @PostConstruct
    public void init(){
        getLocationTask = this;
//        getLocationTask.commonService = this.commonService;
        getLocationTask.carMapper = this.carMapper;
        getLocationTask.stateMapper = this.stateMapper;
        getLocationTask.trackMapper = this.trackMapper;
    }

    // 同步车辆定位数据
    public void syncLocationData(){
        JSONArray array = JSONUtil.parseArray(APP_KEY_JSON);
        for (int i = 0; i < array.size(); i++) {
            JSONObject apikey = array.getJSONObject(i);

            String APPKEY = apikey.getStr("APP_KEY");
            String APPSECRET = apikey.getStr("SECRET_KEY");

            // 创建 E6SdkApi 以使用SDK API
            E6SdkApi e6SdkApi = new E6SdkApi(ADDRESS, APPKEY, APPSECRET);
            try {
                // 通过 e6SdkApi 来使用API
                GetVehicleInfoVO vehicleInfoVO = e6SdkApi.GetVehcileInfo("-1");
                System.out.println(vehicleInfoVO);

                if (Helper.FInt(vehicleInfoVO.getCode())==1){
                    List<VehicleInfoResp> list = vehicleInfoVO.getData();
                    for (VehicleInfoResp info : list) {
                        String serialNo = info.getEquipCode();

                        BizCar dbcar = getLocationTask.carMapper.selectBySerialNo(serialNo);
                        if (dbcar!=null){
                            Long carId = dbcar.getId();
//                            String carNo = dbcar.getCarNo();
                            BizCarState newstate = new BizCarState();

                            newstate.setCarId(dbcar.getId());
                            newstate.setCarNo(dbcar.getCarNo());
                            newstate.setVin(dbcar.getVin());
                            newstate.setVehicleNo(info.getVehicleNO());
                            newstate.setRegName(info.getRegName());
                            if (!Helper.NStr(info.getGpsTime()).equals(""))
                                newstate.setGpsTime(DateUtils.parseDate(Helper.NStr(info.getGpsTime()).replace(".0","")));
                            newstate.setSpeed(Helper.FLong(info.getSpeed()));
                            newstate.setOdometer(BigDecimal.valueOf(Helper.FDouble(info.getOdometer())));
                            newstate.setLat(info.getLat02());
                            newstate.setLon(info.getLon02());
                            newstate.setDirection(Helper.FInt(info.getDirection()));
                            newstate.setStatus(info.getStatus());
                            newstate.setPlaceName(info.getPlaceName());
                            newstate.setProvice(info.getProvice());
                            newstate.setCity(info.getCity());
                            newstate.setDistrict(info.getDistrict());
                            newstate.setRoadName(info.getRoadName());
                            newstate.setT1(info.getT1());
                            newstate.setT2(info.getT2());
                            newstate.setT3(info.getT3());
                            newstate.setT4(info.getT4());
                            newstate.setT5(info.getT5());
                            newstate.setT6(info.getT6());
                            newstate.setT7(info.getT7());
                            newstate.setT8(info.getT8());
                            newstate.setLat02(info.getLat());
                            newstate.setLon02(info.getLon());
                            newstate.setAreaName(info.getAreaName());
                            newstate.setTime1(info.getTime1());
                            newstate.setTime2(info.getTime2());
                            newstate.setTime3(info.getTime3());
                            newstate.setTime4(info.getTime4());
                            newstate.setTime5(info.getTime5());
                            newstate.setTime6(info.getTime6());
                            newstate.setTime7(info.getTime7());
                            newstate.setTime8(info.getTime8());
                            newstate.setH1(info.getH1());
                            newstate.setH2(info.getH2());
                            newstate.setH3(info.getH3());
                            newstate.setH4(info.getH4());
                            newstate.setH5(info.getH5());
                            newstate.setH6(info.getH6());
                            newstate.setH7(info.getH7());
                            newstate.setH8(info.getH8());
                            newstate.setHt1(info.getHt1());
                            newstate.setHt2(info.getHt2());
                            newstate.setHt3(info.getHt3());
                            newstate.setHt4(info.getHt4());
                            newstate.setHt5(info.getHt5());
                            newstate.setHt6(info.getHt6());
                            newstate.setHt7(info.getHt7());
                            newstate.setHt8(info.getHt8());
                            newstate.setDeviceTypeStr(info.getDeviceTypeStr());
                            newstate.setEquipCode(info.getEquipCode());
                            newstate.setOil(info.getOil());
                            newstate.setHeight(Helper.FLong(info.getHeight()));
                            newstate.setStopSec(Helper.FInt(info.getStopSec()));
                            newstate.setAcc(info.getAcc());
                            newstate.setOhm(info.getOhm());
                            newstate.setCurAccSta(info.getCurAccSta());
                            newstate.setCommIds(info.getCommIds());

                            // 更新实时定位信息
                            BizCarState carState = getLocationTask.stateMapper.selectBizCarStateByCarId(carId);
                            if (carState!=null){
                                newstate.setId(carState.getId());
                                getLocationTask.stateMapper.updateById(newstate);
                            }else{
                                getLocationTask.stateMapper.insert(newstate);
                            }

                            // 插入轨迹信息
                            try {
                                TrackTemp track = new TrackTemp();
                                PropertyUtils.copyProperties(track, newstate);
                                getLocationTask.trackMapper.insertTrack(track);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }

            } catch (ApiException e) {
                // 异常捕获
                e.printStackTrace();
            }

        }
    }

}
