package com.gtiit.shr.controller;

import com.alibaba.fastjson.JSONObject;
import com.gtiit.shr.common.Result;
import com.gtiit.shr.service.ADSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;





// ADSyncController 类用于处理与 AD 同步相关的 HTTP 请求
@RestController
@CrossOrigin(origins = "*")
public class ADSyncController {
    @Autowired
    private ADSyncService adSyncService;

    // 同步 AD 用户信息
    @GetMapping({"/syncAD"})
    public Result syncAD(@RequestParam String startDate, @RequestParam String endDate, @RequestParam String personNumber) throws Exception {
        if (null != startDate) {
            startDate = startDate + " 00:00:00";
        }

        if (null != endDate) {
            endDate = endDate + " 23:59:59";
        }

        Result result = new Result();
        JSONObject resultJson = null;

        try {
            resultJson = this.adSyncService.syncADUsers(startDate, endDate, personNumber);
        } catch (Exception var7) {
            result.setData(var7);
            result.setMessage(var7.getMessage());
            result.setCode(500);
            return result;
        }

        Integer code = resultJson.getInteger("code");
        if (code == 500) {
            result.setData(resultJson.getString("msg"));
            result.setCode(500);
            return result;
        } else {
            result.setData(resultJson.get("result"));
            result.setCode(code);
            return result;
        }
    }
}
