package com.qmth.exam.reserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.exam.reserve.bean.org.OrgInfo;
import com.qmth.exam.reserve.dao.OrgDao;
import com.qmth.exam.reserve.entity.OrgEntity;
import com.qmth.exam.reserve.service.OrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class OrgServiceImpl extends ServiceImpl<OrgDao, OrgEntity> implements OrgService {

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

    @Override
    public OrgInfo currentOrg() {
        // 未明确指定学校，默认取一个学校
        LambdaQueryWrapper<OrgEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(OrgEntity::getId, OrgEntity::getName, OrgEntity::getEnable);
        wrapper.eq(OrgEntity::getEnable, Boolean.TRUE);
        wrapper.last("LIMIT 1");

        OrgEntity org = this.getOne(wrapper);
        if (org == null) {
            return null;
        }

        OrgInfo vo = new OrgInfo();
        vo.setOrgId(org.getId());
        vo.setOrgName(org.getName());
        vo.setEnable(org.getEnable());
        return vo;
    }

    @Override
    public OrgInfo findInfoById(Long orgId) {
        OrgEntity org = this.getById(orgId);
        if (org == null) {
            return null;
        }

        OrgInfo vo = new OrgInfo();
        vo.setOrgId(org.getId());
        vo.setEnable(org.getEnable());
        vo.setOrgName(org.getName());
        return vo;
    }

}
