package com.qmth.jkserver.controller.system;

import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dto.SysAdminSetParam;
import com.qmth.jkserver.dto.result.SysConfigResult;
import com.qmth.jkserver.model.SysConfig;
import com.qmth.jkserver.service.BasicSchoolService;
import com.qmth.jkserver.service.CommonCacheService;
import com.qmth.jkserver.service.SysConfigService;
import com.qmth.jkserver.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 学校表 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "学校Controller")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_COMMON + "/school")
public class BasicSchoolController {

    @Autowired
    private BasicSchoolService basicSchoolService;

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    SysConfigService sysConfigService;

    /**
     * 学校查询
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list() {
        SystemConstant.superOper();
        return basicSchoolService.listSchool();
    }

    @ApiOperation(value = "同步配置查询")
    @ApiResponses({@ApiResponse(code = 200, message = "同步配置信息", response = SysConfigResult.class)})
    @RequestMapping(value = "/sync/select", method = RequestMethod.POST)
    public Object sysadminSyncSelect(@ApiParam(value = "学校id ", required = true) @RequestParam Long schoolId) {
        SystemConstant.superOper();
        SysConfig sysConfigCloudmarkHostUrl = commonCacheService.addSysConfigCache(schoolId, SystemConstant.CLOUDMARK_HOST_URL);
        List<SysConfigResult> sysConfigResultList = new ArrayList<>();
        if (Objects.nonNull(sysConfigCloudmarkHostUrl)) {
            sysConfigResultList.add(new SysConfigResult(sysConfigCloudmarkHostUrl));
        } else {
            sysConfigResultList.add(new SysConfigResult(null, SystemConstant.CLOUDMARK_HOST_URL, "云阅卷地址", "", 1));
        }
        return sysConfigResultList;
    }

    @ApiOperation(value = "同步配置保存")
    @ApiResponses({@ApiResponse(code = 200, message = "同步配置信息", response = Object.class)})
    @RequestMapping(value = "/sync/save", method = RequestMethod.POST)
    @Transactional
    public Object sysadminSyncSave(@Valid @RequestBody SysAdminSetParam sysAdminSetParam, BindingResult bindingResult) {
        SystemConstant.superOper();
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Optional.ofNullable(sysAdminSetParam.getParam()).orElseThrow(() -> new JkServerException("同步配置地址不能为空"));

        List<SysConfigResult> sysConfigResultList = sysAdminSetParam.getParam();
        List<SysConfig> sysConfigList = new ArrayList<>();
        for (SysConfigResult s : sysConfigResultList) {
            sysConfigList.add(new SysConfig(sysAdminSetParam.getSchoolId(), s));
        }
        sysConfigService.saveOrUpdateBatch(sysConfigList);

        for (SysConfigResult s : sysConfigResultList) {
            commonCacheService.updateSysConfigCache(sysAdminSetParam.getSchoolId(), s.getCode());
        }
        return ResultUtil.ok(true);
    }
}

