package com.zhiqiyun.open.core.schedule;

import com.zhiqiyun.open.core.enmus.Gender;
import com.zhiqiyun.open.core.models.equipment.EquipmentPassenger;
import com.zhiqiyun.open.core.models.equipment.EquipmentPassengerPeople;
import com.zhiqiyun.open.core.service.EquipmentPassengerPeopleService;
import com.zhiqiyun.open.core.service.EquipmentPassengerService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.utils.DateUtil;
import com.zhiqiyun.open.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

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

	@Autowired
	private EquipmentPassengerPeopleService equipmentPassengerPeopleService;

	@Autowired
	private SequenceService sequenceService;

	@Scheduled(cron = "0/10 * * * * ?")
	public void createPeople() {
		List<EquipmentPassenger> listData = this.equipmentPassengerService.list();
		for (EquipmentPassenger equipment : listData) {
			int r = RandomUtil.getInt(0, 5);
			if (r == 0 || r == 1) {

				Long id = this.sequenceService.nextId();
				int retentionTime = RandomUtil.getInt(10, 10000);

				EquipmentPassengerPeople people = new EquipmentPassengerPeople();
				people.setId(id);
				people.setEquipmentId(equipment.getId());
				people.setFaceId(RandomUtil.getuuid());
				people.setGender(Gender.valueOf(r));
				people.setFromSource("南宁");

				people.setInTime(DateUtil.current());
				int r1 = RandomUtil.getInt(0, 2);
				if (r1 == 1) {
					people.setOutTime(DateUtils.addSeconds(people.getInTime(), retentionTime));
				}
				people.setRetentionTime(retentionTime);
				this.equipmentPassengerPeopleService.save(people);
			}
		}
	}
}
