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

import com.usoftchina.uas.office.dingtalk.manage.service.DingTalkSettingService;
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.GetMapping;
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 DingTalkSettingController {

    @Autowired
    private DingTalkSettingService settingService;

    @GetMapping(path = "/setting")
    public Result getDingTalkSetting() {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            return Result.success(settingService.find());
        } finally {
            DataSourceHolder.clear();
        }
    }

    @PostMapping(path = "/setting/corp")
    public Result saveDingTalkCorp(String corpId) {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            settingService.setCorpId(corpId);
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }
}
