package com.sky.ioc.service.parking.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.parking.Park;
import com.sky.ioc.entity.domain.space.Department;
import com.sky.ioc.entity.params.scene.ParkParam;
import com.sky.ioc.mapper.ParkMapper;
import com.sky.ioc.service.parking.ParkService;
import com.sky.ioc.tool.DateUtil;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
@Slf4j
public class ParkServiceImpl implements ParkService {

    @Autowired
    RestTemplateConfig restTemplateConfig;

    @Autowired
    ParkMapper parkMapper;

    final static  String PARK_URL="/api/parking/PARKING/parkingDuration/list";

    @Override
    public ReturnMsg getParkData() {
        String url = restTemplateConfig.addBaseUrl(PARK_URL+"?pageNum=1&pageSize=1000");

        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            ThreadLocalRandom random = ThreadLocalRandom.current();
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                parkMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    Park park = new Park();
                    park.setId((Integer) json.get("id"));
                    park.setParkingNumbers(String.valueOf(json.get("parkingNumbers")));
                    park.setEntryTime(String.valueOf(json.get("inParkTime")));
                    park.setLicencePlate(String.valueOf(json.get("illegalCardnumber")));
                    park.setDepartureTime(String.valueOf(json.get("outParkTime")));
                    if(json.get("outParkTime")!=null){
                        Long hours = DateUtil.hoursBetween(String.valueOf(json.get("inParkTime")),String.valueOf(json.get("outParkTime")));
                        park.setHours(hours);
                    }
                    park.setType(Integer.parseInt(String.valueOf(json.get("ifIllegal"))));
                    parkMapper.insert(park);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取停车数据："+message);
            return  ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg pageList(ParkParam parkParam) {
        long total = parkMapper.pageCount(parkParam);
        List<Map<String,Object>> lists = parkMapper.pageList(parkParam);
        for(Map<String,Object> map:lists){
            if(map.get("departure_time")==null||String.valueOf(map.get("departure_time")).equals("null")){
                map.put("departure_time","");
            }
            map.put("order_status","");
            map.put("pay_type","");
            map.put("order_money","");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",total);
        jsonObject.put("data",lists);
        return ReturnMsg.ok(jsonObject);
    }
}
