package com.qmth.themis.exam.api;

import com.qmth.themis.business.annotation.ApiJsonObject;
import com.qmth.themis.business.annotation.ApiJsonProperty;
import com.qmth.themis.business.bean.exam.EnvBean;
import com.qmth.themis.business.bean.exam.OrgInfoBean;
import com.qmth.themis.business.bean.exam.VersionBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.TBClientVersion;
import com.qmth.themis.business.entity.TBOrg;
import com.qmth.themis.business.enums.UploadFileEnum;
import com.qmth.themis.business.service.CacheService;
import com.qmth.themis.business.service.TBClientVersionService;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import com.qmth.themis.exam.config.DictionaryConfig;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;
import java.util.Objects;

@Api(tags = "系统信息Controller")
@RestController
@RequestMapping("/${prefix.url.exam}/sys")
public class SysController {

    @Resource
    DictionaryConfig dictionaryConfig;

    @Resource
    TBClientVersionService tbClientVersionService;

    @Resource
    CacheService cacheService;

    @ApiOperation(value = "获取环境接口")
    @RequestMapping(value = "/env", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "环境信息", response = Result.class)})
    public Result env(@ApiJsonObject(name = "oeEnv", value = {
            @ApiJsonProperty(key = "orgId", type = "long", example = "1", description = "机构id"),
    }) @ApiParam(value = "机构id", required = false) @RequestBody Map<String, Object> mapParameter) {
        EnvBean env = new EnvBean();
        if (Objects.nonNull(mapParameter.get(SystemConstant.ORG_ID))) {
            Long orgId = Long.parseLong(String.valueOf(mapParameter.get(SystemConstant.ORG_ID)));
            TBOrg org = cacheService.addOrgCache(orgId);
            if (Objects.nonNull(org)) {
                OrgInfoBean orgInfo = new OrgInfoBean(org.getName(), org.getLogo(), (org.getUpdateTime() == null ? null : org.getUpdateTime()));
                env.setOrgInfo(orgInfo);
            } else {
                throw new BusinessException("没有id为[" + orgId + "]的机构");
            }
        }
        env.setCurrentTime(System.currentTimeMillis());
        TBClientVersion tbClientVersion = tbClientVersionService.getMaxClientVersion();
        if (Objects.nonNull(tbClientVersion)) {
            VersionBean v = new VersionBean(tbClientVersion.getName(), tbClientVersion.getValue(), tbClientVersion.getUrl());
            env.setVersion(v);
        }
        boolean oss = dictionaryConfig.sysDomain().isOss();
        String url = null;
        if (oss) {
            url = dictionaryConfig.aliYunOssDomain().getPublicUrl() + File.separator + UploadFileEnum.client.name() + "/denyList.json";
        } else {
            url = "http://" + dictionaryConfig.sysDomain().getFileHost() + File.separator + UploadFileEnum.client.name() + "/denyList.json";
        }
        env.setDenyList(url);
        return ResultUtil.ok(env);
    }
}
