package com.jpsoft.shinestar.scheduled;

import com.jpsoft.shinestar.modules.base.service.DeviceInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonDeviceRelationService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.pacs.service.IGuardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 墨鱼_mo
 * @date 2020-4-8 14:27
 */

@Component
@Slf4j
@Transactional
public class UpdatePersonDeviceBoundTask {


    @Autowired
    private PersonDeviceRelationService personDeviceRelationService;

    @Autowired
    private IGuardService lapiService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    //已改为实时查询，已不需要执行
//    @Scheduled(cron = "0 0/10 * * * ?")
    public  void run() {
        log.warn("人员设备绑定关系更新定时任务开始");

        //200条数据，更新时间要晚于当前时间的前一天
//        List<PersonDeviceRelation> personDeviceRelationList = personDeviceRelationService.findByIsBoundAndUpdateTime(DateUtil.offset(new Date(),DateField.DAY_OF_MONTH, -1),200);
//        if (personDeviceRelationList.size()>0){
//            for (PersonDeviceRelation personDeviceRelation : personDeviceRelationList){
//                try{
//                    lapiService.keepAlive(personDeviceRelation.getDeviceId());
//                    PersonInfo personInfo = personInfoService.get(personDeviceRelation.getPersonId());
//                    DeviceInfo deviceInfo = deviceInfoService.get(personDeviceRelation.getDeviceId());
//                    String faceDbId = lapiService.getFaceDbId(deviceInfo,personInfo.getCompanyName());
//                    Boolean isSuccessAdd =  lapiService.isSuccessAddPerson(personInfo,deviceInfo,faceDbId);
//                    if (isSuccessAdd){
//                        personDeviceRelation.setIsBound(true);
//                        personDeviceRelation.setUpdateTime(new Date());
//                    }else {
//                        personDeviceRelation.setIsBound(false);
//                        personDeviceRelation.setUpdateTime(new Date());
//                    }
//                }catch (Exception e){
//                    personDeviceRelation.setUpdateTime(new Date());
//                    personDeviceRelation.setIsBound(null);
//                }
//
//                personDeviceRelationService.update(personDeviceRelation);
//
//            }
//        }
    }
}
