package com.zhiqiyun.open.core.schedule;

import com.zhiqiyun.open.core.enmus.Gender;
import com.zhiqiyun.open.core.models.bz.PassengerEquipment;
import com.zhiqiyun.open.core.models.bz.PassengerEquipmentPeople;
import com.zhiqiyun.open.core.service.PassengerEquipmentPeopleService;
import com.zhiqiyun.open.core.service.PassengerEquipmentService;
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.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class EquipmentPeople {
	@Autowired
	private PassengerEquipmentService passengerEquipmentService;

	@Autowired
	private PassengerEquipmentPeopleService passengerEquipmentPeopleService;

	@Autowired
	private SequenceService sequenceService;

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

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

				PassengerEquipmentPeople people = new PassengerEquipmentPeople();
				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.passengerEquipmentPeopleService.save(people);
			}
		}
	}
}
