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

import com.usoftchina.uas.office.dto.Result;
import com.usoftchina.uas.office.entity.DataCenter;
import com.usoftchina.uas.office.jdbc.DataSourceHolder;
import com.usoftchina.uas.office.qywx.manage.service.QywxSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

    @Autowired
    private QywxSettingService settingService;

    @GetMapping(path = "/setting")
    public Result getQywxSetting() {
        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 saveQywxCorp(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();
        }
    }
}
