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

import com.usoftchina.qywx.sdk.config.QywxProperties;
import com.usoftchina.qywx.sdk.util.UrlUtils;
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.entity.QywxAgent;
import com.usoftchina.uas.office.qywx.manage.service.QywxAgentService;
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 QywxAgentController {

    @Autowired
    private QywxAgentService qywxAgentService;

    @Autowired
    private QywxProperties qywxProperties;

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

    @PostMapping(path = "/agent")
    public Result saveQywxAgent(QywxAgent agent) {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            qywxAgentService.save(agent);
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }

    @DeleteMapping(path = "/agent")
    public Result deleteQywxAgent(String code) {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            qywxAgentService.delete(code);
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }

    /**
     * 获取用于从企业微信跳转到具体应用的授权url
     *
     * @param code
     * @return
     */
    @GetMapping(path = "/agent/auth_url")
    public Result getAgentAuthUrl(String code) throws Exception{
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        if (null == dataCenter.getOuterUrl()) {
            return Result.error("外网地址未设置");
        }
        if (null == qywxProperties.getCorpId()) {
            return Result.error("企业微信ID未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            QywxAgent agent = qywxAgentService.findByCode(code);
            if (null == agent.getOuterUrl()) {
                return Result.error("应用外网地址未设置");
            }
            String authUrl = UrlUtils.generateOAuthUrl(qywxProperties.getCorpId(), dataCenter.getUsername(), code,
                    agent.getOuterUrl(), dataCenter.getOuterUrl() + "api/authorize", "/");
            return Result.success(authUrl);
        } finally {
            DataSourceHolder.clear();
        }
    }
}
