package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.common.code.SourceEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.face.service.FaceGroupService;
import com.mortals.xhx.module.face.service.FacePlanService;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.hik.face.service.IHikPlanService;
import com.mortals.xhx.module.hik.person.model.req.person.PersonReq;
import com.mortals.xhx.module.hik.person.model.rsp.person.PersonDataInfo;
import com.mortals.xhx.module.hik.person.service.IHikPersonService;
import com.mortals.xhx.module.person.model.PersonEntity;
import com.mortals.xhx.module.person.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 同步工作人员到本地注册人员中
 *
 * @author:
 * @date: 2023/4/24 10:50
 */
//@Component
@Slf4j
public class SyncWorkmanStartedService implements IApplicationStartedService {
    @Autowired
    private PersonService personService;
    @Autowired
    private IHikPersonService hikPersonService;


    @Override
    public void start() {
        log.info("开始服务..[工作人员同步]");

        PersonReq personReq = new PersonReq();
        personReq.setPageNo(1);
        personReq.setPageSize(3);
        Rest<PersonDataInfo> personListRest = hikPersonService.getPersonList(personReq);
        if (personListRest.getCode() == YesNoEnum.YES.getValue()) {
            List<PersonEntity> collect = personListRest.getData().getList().parallelStream().map(work -> {
                //判断是否存在，存在更新，
                if (work.getCertificateType() == 990) {
                    work.setCertificateNo(StrUtil.subSufByLength(work.getPersonId(), 20));
                }
                PersonEntity personEntity = personService.getExtCache(work.getCertificateNo());
                if (ObjectUtils.isEmpty(personEntity)) {
                    personEntity = new PersonEntity();
                    personEntity.initAttrValue();
                    personEntity.setName(work.getPersonName());
                    personEntity.setGender(work.getGender() == 1 ? 0 : 1);
                    personEntity.setIdCard(work.getCertificateNo());
                    personEntity.setPhone(work.getPhoneNo());
                    try {
                        personEntity.setBirthday(DateUtil.parse(IdcardUtil.getBirth(work.getCertificateNo()), "yyyyMMdd"));
                    } catch (Exception e) {
                    }
                    personEntity.setServerIndexCode(work.getPersonPhoto().getServerIndexCode());
                    personEntity.setPicUrl(work.getPersonPhoto().getPicUri());
                    personEntity.setSource(SourceEnum.海康系统.getValue());
                    personEntity.setCreateTime(new Date());
                    personEntity.setCreateUserId(1L);
                    personEntity.setCreateUserName("system");
                    return personEntity;
                }
                return personEntity;
            }).collect(Collectors.toList());

            Map<Boolean, List<PersonEntity>> booleanListMap = collect.parallelStream().collect(Collectors.partitioningBy(x -> x.getId() == null));
            List<PersonEntity> savePersonList = booleanListMap.get(true);
            List<PersonEntity> updatePersonList = booleanListMap.get(false);

            if (!ObjectUtils.isEmpty(savePersonList)) {
                log.info("新增人员数量:{}", savePersonList.size());
                personService.save(savePersonList);
            }

            if (!ObjectUtils.isEmpty(updatePersonList)) {
                log.info("更新人员数量:{}", updatePersonList.size());
                // personService.update(updatePersonList);
            }


        }


    }

    @Override
    public void stop() {
        log.info("停止服务..");
    }

    @Override
    public int getOrder() {
        return 11;
    }


}
