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

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.Constants;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.bean.org.OrgInfo;
import com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteReq;
import com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteVo;
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.enums.EventType;
import com.qmth.exam.reserve.enums.Role;
import com.qmth.exam.reserve.service.OperateLogService;
import com.qmth.exam.reserve.service.SystemPropertyService;
import com.qmth.exam.reserve.service.TimePeriodExamSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "【管理端】考点预约设置相关接口")
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/time/period/exam/site")
@Aac(strict = false, auth = true)
public class TimePeriodExamSiteController extends BaseController {

    @Autowired
    private TimePeriodExamSiteService timePeriodExamSiteService;

    @Autowired
    private SystemPropertyService systemPropertyService;

    @Autowired
    private OperateLogService operateLogService;

    @Autowired
    private OrgCacheService orgCacheService;

    @ApiOperation(value = "时段区间列表")
    @PostMapping(value = "/list")
    public List<String> list(@ApiParam("预约任务ID") @RequestParam(required = false) Long taskId) {
        return timePeriodExamSiteService.listTimePeriod(taskId);
    }

    @ApiOperation(value = "日期时段列表")
    @PostMapping(value = "/detail/list")
    public List<TimePeriodExamSiteVo> ListDetail(@ApiParam("考点ID") @RequestParam(required = false) Long examSiteId) {
        if (examSiteId == null) {
            return new ArrayList<>();
        }
        return timePeriodExamSiteService.ListDetail(examSiteId);
    }

    @ApiOperation(value = "考点预约时段编辑")
    @PostMapping(value = "/save")
    public void save(@ApiParam("考点ID") @RequestParam(required = true) Long examSiteId, @RequestBody List<TimePeriodExamSiteReq> timePeriodExamSiteList) {
        LoginUser loginUser = this.curLoginUser();
        timePeriodExamSiteService.save(loginUser.getId(), examSiteId, timePeriodExamSiteList);
    }

    @ApiOperation(value = "自主预约统一控制")
    @PostMapping(value = "/switch/enable")
    public void enableSwitch(@ApiParam("开关状态") @RequestParam Boolean status) {
        LoginUser loginUser = this.curLoginUser();
        if (!loginUser.getRole().equals(Role.ADMIN)) {
            throw new StatusException("无权限");
        }

        systemPropertyService.updatePropValue(loginUser.getOrgId(), Constants.APPLY_SWITCH,
                status ? Constants.APPLY_SWITCH_OPEN : Constants.APPLY_SWITCH_CLOSE);

        // 记录操作日志
        operateLogService.insertOperateLog(loginUser.getId(), EventType.APPLY_SWITCH, status ? "启用" : "禁用");
    }

    @ApiOperation(value = "查找自主预约统一控制属性")
    @PostMapping(value = "/switch/find")
    public Boolean findPropertyByKey(@ApiParam("学校ID") @RequestParam(required = false) Long orgId, @RequestParam String propKey) {
        OrgInfo org;
        if (orgId != null) {
            org = orgCacheService.getOrgById(orgId);
        } else {
            org = orgCacheService.currentOrg();
        }
        if (org != null) {
            orgId = org.getOrgId();
        }

        SystemPropertyEntity systemProperty = systemPropertyService.findByPropKey(orgId, propKey);
        return systemProperty == null || systemProperty.getPropValue().equals(Constants.APPLY_SWITCH_OPEN);
    }

}
