package com.zhiqiyun.open.core.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.core.enmus.Gender;
import com.zhiqiyun.open.core.enmus.PassengerType;
import com.zhiqiyun.open.core.models.DictCity;
import com.zhiqiyun.open.core.models.app.ApiRequestLog;
import com.zhiqiyun.open.core.models.equipment.EquipmentPassenger;
import com.zhiqiyun.open.core.models.equipment.EquipmentPassengerPeople;
import com.zhiqiyun.open.core.service.*;
import com.zhiqiyun.open.utils.DateUtil;
import com.zhiqiyun.open.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class AutoSchedule {
	@Autowired
	private EquipmentPassengerService equipmentPassengerService;

	@Autowired
	private EquipmentPassengerPeopleService equipmentPassengerPeopleService;

	@Autowired
	private DictCityService dictCityService;

	@Autowired
	private SequenceService sequenceService;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private ApiRequestLogService apiRequestLogService;

	@Value("${spring.profiles.active}")
	private String active;

	@Scheduled(cron = "0/20 * * * * ?")
	public void createEquipmentPassengerPeople() {
		if ("dev".equals(active)) {
			return;
		}
		QueryWrapper<DictCity> wrapper = new QueryWrapper<>();
		wrapper.eq("parent_id", "0");
		List<DictCity> listDictCity = this.dictCityService.list(wrapper);
		List<EquipmentPassenger> listData = this.equipmentPassengerService.list();
		for (EquipmentPassenger equipment : listData) {

			int r1 = RandomUtil.getInt(0, listDictCity.size() - 1);
			int r2 = r1 % 2 + 1;
			PassengerType type = PassengerType.valueOf(r2);

			EquipmentPassengerPeople people = new EquipmentPassengerPeople();
			people.setId(this.sequenceService.nextId());
			people.setPlaceBaseInfoId(equipment.getPlaceBaseInfoId());
			people.setEquipmentId(equipment.getId());
			people.setPassengerType(type);
			people.setCreatedTime(DateUtil.current());
			people.setGender(Gender.valueOf(r2));


			people.setFromSource(listDictCity.get(r1).getFullName());


			String redisKey = String.format("equipment%s", equipment.getId());
			BoundListOperations<String, String> boundListOps = this.stringRedisTemplate.boundListOps(redisKey);
			String faceId;
			if (PassengerType.IN.equals(type)) {
				faceId = RandomUtil.getuuid();
				boundListOps.leftPush(faceId);
			} else {
				faceId = boundListOps.rightPop();
			}
			if (StringUtils.isNotBlank(faceId)) {
				people.setFaceId(faceId);
				this.equipmentPassengerPeopleService.save(people);
			}
		}
	}

	@Scheduled(cron = "0/30 * * * * ?")
	public void deleteExpireEquipmentPassengerPeople() {
		QueryWrapper<EquipmentPassengerPeople> wrapper = new QueryWrapper<>();
		wrapper.lt("created_time", DateUtils.addDays(DateUtil.current(), -15));

		List<EquipmentPassengerPeople> list = this.equipmentPassengerPeopleService.list(wrapper);
		log.info("deleteExpireEquipmentPassengerPeople size>>>>{}", list.size());

		for (EquipmentPassengerPeople people : list) {
			this.equipmentPassengerPeopleService.removeById(people.getId());
		}
	}

	@Scheduled(cron = "0/30 * * * * ?")
	public void deleteOneFaceId() {
		QueryWrapper<EquipmentPassengerPeople> wrapper = new QueryWrapper<>();
		wrapper.inSql("face_id", "select face_id from equipment_passenger_people GROUP BY face_id HAVING count(face_id) = 1");
		wrapper.eq("passenger_type", PassengerType.OUT);
		List<EquipmentPassengerPeople> list = this.equipmentPassengerPeopleService.list(wrapper);
		log.info("deleteOneFaceId size>>>>{}", list.size());
		for (EquipmentPassengerPeople people : list) {
			this.equipmentPassengerPeopleService.removeById(people.getId());
		}
	}

	@Scheduled(cron = "0/30 * * * * ?")
	public void deleteExpireApiRequestLogs() {
		QueryWrapper<ApiRequestLog> wrapper = new QueryWrapper<>();
		wrapper.lt("service_begin_time", DateUtils.addDays(DateUtil.current(), -30));

		List<ApiRequestLog> list = this.apiRequestLogService.list(wrapper);
		log.info("deleteExpireApiRequestLogs size>>>>{}", list.size());

		for (ApiRequestLog log : list) {
			this.apiRequestLogService.removeById(log.getId());
		}
	}

}
