package com.yida.data.school.config.controller.in;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.school.EduSchoolFunctionConfig;
import com.yida.data.common.core.enums.EnableStatusEnum;
import com.yida.data.school.config.service.EduSchoolFunctionConfigService;
import com.yida.data.school.dto.config.EduSchoolFunctionConfigDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "学校关键功能点配置")
@Slf4j
@RestController
@RequestMapping("/in/schoolFunctionConfig")
@RequiredArgsConstructor
public class EduSchoolFunctionConfigController {

    private final EduSchoolFunctionConfigService eduSchoolFunctionConfigService;

    private final RedisService redisService;

    @ApiOperation("获取当前学校关键功能点配置")
    @GetMapping("/getSchoolFunctionConfigListByCondition")
    public ResultBean<List<EduSchoolFunctionConfig>> getSchoolFunctionConfigListByCondition() {
        List<EduSchoolFunctionConfig> eduSchoolFunctionConfig = new ArrayList<>();

//        if (ObjectUtil.isNotNull(dto.getSchool_id())) {
//            if (redisService.hHasKey(CachePrefixConstant.SCHOOL_FUNCATION_CODE, dto.getSchool_id().toString())) {
//                List<EduSchoolFunctionConfig> eduSchoolFunctionConfigList = (List<EduSchoolFunctionConfig>) redisService
//                    .hget(CachePrefixConstant.SCHOOL_FUNCATION_CODE, dto.getSchool_id().toString());
//                eduSchoolFunctionConfig.addAll(eduSchoolFunctionConfigList);
//            }
//
//            if (CollUtil.isEmpty(eduSchoolFunctionConfig)) {
//                ResultBean<List<EduSchoolFunctionConfig>> listResultBean = ResultBean
//                    .buildSuccess(eduSchoolFunctionConfigService.list(Wrappers.lambdaQuery(new EduSchoolFunctionConfig())
//                        .eq(EduSchoolFunctionConfig::getSchool_id,
//                            dto.getSchool_id())));
//                eduSchoolFunctionConfig.addAll(listResultBean.getData());
//                redisService
//                    .hset(CachePrefixConstant.SCHOOL_FUNCATION_CODE, dto.getSchool_id().toString(), eduSchoolFunctionConfig);
//            }
//        } else {
//            ResultBean<List<EduSchoolFunctionConfig>> listResultBean = ResultBean
//                .buildSuccess(eduSchoolFunctionConfigService.list());
//            eduSchoolFunctionConfig.addAll(listResultBean.getData());
//        }

        ResultBean<List<EduSchoolFunctionConfig>> listResultBean = ResultBean
            .buildSuccess(eduSchoolFunctionConfigService.list());
        eduSchoolFunctionConfig.addAll(listResultBean.getData());

        return ResultBean.buildSuccess(eduSchoolFunctionConfig);
    }


    /**
     * 查询学校是否开始功能
     *
     * @param schoolId
     * @param code
     * @return
     */
    @ApiOperation("查询学校关键功能点配置信息")
    @GetMapping("/checkSchoolFuncationCode")
    private ResultBean<Boolean> checkSchoolFuncationCode(@Param("schoolId") Long schoolId, @Param("code") String code) {
        EduSchoolFunctionConfig eduSchoolFunctionConfig = null;
        if (redisService.hHasKey(CachePrefixConstant.SCHOOL_FUNCATION_CODE + schoolId, code)) {
            eduSchoolFunctionConfig = (EduSchoolFunctionConfig) redisService
                .hget(CachePrefixConstant.SCHOOL_FUNCATION_CODE + schoolId, code);
        } else {
            eduSchoolFunctionConfig = eduSchoolFunctionConfigService
                .getOne(Wrappers.lambdaQuery(new EduSchoolFunctionConfig())
                    .eq(EduSchoolFunctionConfig::getSchoolId, schoolId)
                    .eq(EduSchoolFunctionConfig::getFunctionCode, code));
            if (ObjectUtil.isNotNull(eduSchoolFunctionConfig)) {
                redisService
                    .hset(CachePrefixConstant.SCHOOL_FUNCATION_CODE + schoolId, code, eduSchoolFunctionConfig,
                        60 * 60L);
            }
        }
        if (ObjectUtil.isNotNull(eduSchoolFunctionConfig) && ObjectUtil
            .equals(EnableStatusEnum.ENABLE_STATUS.getStatus(), eduSchoolFunctionConfig.getState())) {
            return ResultBean.buildSuccess(true);
        }
        return ResultBean.buildSuccess(false);
    }

}
