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.entity.TBOrg;
import com.qmth.themis.business.service.TBOrgService;
import com.qmth.themis.business.util.RedisUtil;
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.util.Map;
import java.util.Objects;

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

    @Resource
    private TBOrgService orgService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    DictionaryConfig dictionaryConfig;

    @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("orgId"))) {
            Long orgId = Long.parseLong(String.valueOf(mapParameter.get("orgId")));
            TBOrg org = (TBOrg) redisUtil.getOrg(orgId);
            if (Objects.isNull(org)) {
                org = orgService.getById(orgId);
            }
            if (Objects.nonNull(org)) {
                OrgInfoBean orgInfo = new OrgInfoBean();
                orgInfo.setLogo(org.getLogo());
                orgInfo.setName(org.getName());
                orgInfo.setUpdateTime((org.getUpdateTime() == null ? null : org.getUpdateTime()));
                env.setOrgInfo(orgInfo);
            }
        }
        env.setCurrentTime(System.currentTimeMillis());
        VersionBean v = new VersionBean();
        v.setName("1.2.0");
        v.setValue(12);
        v.setUrl("xxxxxx");
        env.setVersion(v);
        boolean oss = dictionaryConfig.sysDomain().isOss();
        String url = null;
        if (oss) {
            url = dictionaryConfig.aliYunOssDomain().getUrl() + "/client/denyList.json";
        } else {
            url = "http://" + dictionaryConfig.sysDomain().getFileHost() + "/client/denyList.json";
        }
        env.setDenyList(url);
        return ResultUtil.ok(env);
    }
}
