package com.usoftchina.uas.office.dingtalk.manage.controller;

import com.usoftchina.uas.office.dingtalk.service.UasEmployeeService;
import com.usoftchina.uas.office.dingtalk.service.UasOrgService;
import com.usoftchina.uas.office.dto.Result;
import com.usoftchina.uas.office.entity.DataCenter;
import com.usoftchina.uas.office.jdbc.DataSourceHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yingp
 * @date 2020/2/16
 */
@RequestMapping(path = "/mgm")
@RestController
public class DingTalkAddrBookController {

    @Autowired
    private UasOrgService orgService;

    @Autowired
    private UasEmployeeService employeeService;

    @PostMapping(path = "/addrbook/sync")
    public Result syncAll() {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            orgService.sync();
            employeeService.sync();
            orgService.clearUseless();
            orgService.updateManagerUser();
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }
}
