package com.qmth.exam.reserve.controller.student;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.fss.store.FileStore;
import com.qmth.exam.reserve.bean.Constants;
import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
import com.qmth.exam.reserve.bean.org.OrgInfo;
import com.qmth.exam.reserve.bean.systemproperty.SystemPropertyBean;
import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
import com.qmth.exam.reserve.cache.impl.OrgCacheService;
import com.qmth.exam.reserve.controller.BaseController;
import com.qmth.exam.reserve.entity.SystemPropertyEntity;
import com.qmth.exam.reserve.service.SystemPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("sysPropertyController")
@Api(tags = "【考生端】系统常用属性相关接口")
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/student/system")
@Aac(strict = false, auth = false)
public class SystemPropertyController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SystemPropertyController.class);

    @Autowired
    private FileStore fileStore;

    @Autowired
    private OrgCacheService orgCacheService;

    @Autowired
    private ApplyTaskCacheService applyTaskCacheService;

    @Autowired
    private SystemPropertyService systemPropertyService;

    @ApiOperation(value = "获取系统常用属性集合")
    @PostMapping(value = "/properties")
    public SystemPropertyBean properties(@ApiParam("学校ID") @RequestParam(required = false) Long orgId) {
        SystemPropertyBean properties = new SystemPropertyBean();
        properties.setFileUrlPrefix(fileStore.getServer());

        OrgInfo org;
        if (orgId != null) {
            org = orgCacheService.getOrgById(orgId);
        } else {
            org = orgCacheService.currentOrg();
        }
        if (org != null) {
            properties.setOrgTitle(org.getOrgName());
            orgId = org.getOrgId();
        }

        CurrentApplyTaskVO curApplyTask = applyTaskCacheService.currentApplyTask(orgId);
        if (curApplyTask != null) {
            // 当前预约任务
            properties.setTaskTitle(curApplyTask.getTaskName());
        }

        SystemPropertyEntity systemProperty = systemPropertyService.findByPropKey(orgId, Constants.CATEGORY_LEVEL);
        if (systemProperty != null) {
            properties.setCategoryLevels(systemProperty.getPropValue());
        }

        return properties;
    }

}
