package com.usoftchina.uas.office.dingtalk.listener;

import com.usoftchina.dingtalk.sdk.AddrBookSdk;
import com.usoftchina.uas.office.dingtalk.entity.Employee;
import com.usoftchina.uas.office.dingtalk.service.UasEmployeeService;
import com.usoftchina.uas.office.dto.UasEvent;
import com.usoftchina.uas.office.listener.UasEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yingp
 * @date 2020/2/16
 */
@Component
public class UasEmployeeListener {

    @Autowired
    private AddrBookSdk addrBookSdk;

    @Autowired
    private UasEmployeeService employeeService;

    /**
     * uas人员资料审核，修改钉钉通讯录人员
     */
    @UasEventListener(caller = "Employeemanager", operation = "AUDIT")
    public void onEmployeeAudit(UasEvent event) {
        Employee employee = employeeService.getById(Integer.parseInt(event.getKey().toString()));
        if (null != employee) {
            if ("离职".equals(employee.getEm_class())) {
                if (null != employee.getEm_ding()) {
                    addrBookSdk.deleteUser("Uas", employee.getEm_ding());
                }
            } else {
                employeeService.sync(employee);
            }
        }
    }

    /**
     * uas人员资料删除，删除企业微信通讯录人员
     */
    @UasEventListener(caller = "Employeemanager", operation = "DELETE")
    public void onEmployeeDelete(UasEvent event) {
        Employee employee = employeeService.getRemovedById(Integer.parseInt(event.getKey().toString()));
        if (null != employee && null != employee.getEm_ding()) {
            addrBookSdk.deleteUser("Uas", employee.getEm_ding());
        }
    }
}
