package com.mortals.xhx.daemon.task;

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;
import com.mortals.xhx.module.attendance.service.AttendanceVacationBalanceService;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.dingding.personal.service.IDingPersonService;
import com.mortals.xhx.module.hik.person.model.req.org.OrgListReq;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgDataInfo;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgInfo;
import com.mortals.xhx.module.hik.person.service.IHikPersonService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffLeaveService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * 同步海康用户
 */
@Slf4j
@Service("SyncUserTask")
public class SyncUserTaskImpl implements ITaskExcuteService {

    @Autowired
    private StaffService staffService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private IHikPersonService hikPersonService;
    @Autowired
    private IDingPersonService dingPersonService;
    @Value("${hik.host:}")
    protected String hikhost;

    @Override
    public void excuteTask(ITask task) throws AppException {
        try {
            if (!ObjectUtils.isEmpty(hikhost)) {
                log.info("同步部门");
                deptService.syncDept(null);
                log.info("同步用户");
                staffService.syncPersons(null);
                log.info("同步钉钉usreId");
                try {
                    List<UserEntity> userList = userService.getCacheList().stream()
                            .filter(f -> !ObjectUtils.isEmpty(f.getCustomerId()))
                            .filter(f -> ObjectUtils.isEmpty(f.getDingUserId())).collect(Collectors.toList());
                    List<UserEntity> updateUserList = userList.stream().map(item -> {
                        UserEntity userEntity = new UserEntity();
                        userEntity.setId(item.getId());

                        String mobile = "";
                        StaffEntity staffCache = staffService.getCache(item.getCustomerId().toString());
                        if (!ObjectUtils.isEmpty(staffCache) && !ObjectUtils.isEmpty(staffCache.getPhoneNumber())) {
                            mobile = staffCache.getPhoneNumber();
                            if (ObjectUtils.isEmpty(item.getMobile())) {
                                userEntity.setMobile(mobile);
                            }
                        }

                        if (!ObjectUtils.isEmpty(mobile)) {
                            Rest<String> personByMobile = dingPersonService.getPersonByMobile(mobile);
                            if (!ObjectUtils.isEmpty(personByMobile) &&
                                    YesNoEnum.YES.getValue() == personByMobile.getCode()
                                    && !ObjectUtils.isEmpty(personByMobile.getData())) {
                                userEntity.setDingUserId(personByMobile.getData());
                            }
                        }

                        userEntity.setUpdateTime(new Date());
                        userEntity.setUpdateUserId(1L);
                        return userEntity;
                    }).filter(f -> !ObjectUtils.isEmpty(f.getDingUserId())).collect(Collectors.toList());

                    if (!ObjectUtils.isEmpty(updateUserList)) {
                        log.info("更新用户钉钉id信息数量：{}", updateUserList.size());
                        userService.getUserDao().updateBatch(updateUserList);
                    }
                } catch (Exception e) {
                    log.error("同步钉钉usreId失败");
                }
            } else {
                log.info("设备直连同步人员信息");
                staffService.syncPersonsByDevices(null);
            }
        } catch (Exception e) {
            log.error("同步人事异常", e);
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }
}
