package jnpf.cultivate.controller.web.exam;

import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.config.ConfigValueUtil;
import jnpf.cultivate.service.FtbCultivateExamService;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.util.NoDataSourceBind;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import jnpf.util.data.DataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 考试老数据处理
 *
 * @author xuguilin
 */
@RestController
@RequestMapping("/examOldData")
public class FtbCultivateExamOldDataController {
    /**
     * 考试服务
     */
    @Autowired
    private FtbCultivateExamService examService;

    @Autowired
    private ConfigValueUtil configValueUtil;

    /**
     * 考试处理老数据
     *
     * @return
     */
    @NoDataSourceBind
    @GetMapping("/initOldExamData")
    public ActionResult<List<String>> initOldExamData(@RequestParam("tenantId") String tenantId) {

        // 判断是否为多租户
        if (configValueUtil.isMultiTenancy()) {
            // 判断是不是从外面直接请求
            if (StringUtil.isNotEmpty(tenantId)) {
                //切换成租户库
                try {
                    TenantDataSourceUtil.switchTenant(tenantId);
                } catch (LoginException e) {
                    throw new RuntimeException("切换租户失败");
                }
            } else {
                UserInfo userInfo = UserProvider.getUser();
                Assert.notNull(userInfo.getUserId(), "缺少租户信息");
                DataSourceContextHolder.setDatasource(userInfo.getTenantId(), userInfo.getTenantDbConnectionString(), userInfo.isAssignDataSource());
            }
        }
        //2检测和修改常规考试过期
        List<String> list = examService.initOldExamData();

//        //3、检测已经删除的岗位考试，并删除考试记录
//        examService.deleteExamUserForDeleteExam();
        return ActionResult.success(list);
    }


}
