package com.lesingle.creation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lesingle.creation.common.exception.BusinessException;
import com.lesingle.creation.dto.school.CreateSchoolDTO;
import com.lesingle.creation.dto.school.UpdateSchoolDTO;
import com.lesingle.creation.entity.School;
import com.lesingle.creation.mapper.SchoolMapper;
import com.lesingle.creation.service.SchoolService;
import com.lesingle.creation.vo.school.SchoolVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学校服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

    private final SchoolMapper schoolMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SchoolVO create(CreateSchoolDTO dto, Long tenantId, Long creatorId) {
        log.info("开始创建学校，租户 ID: {}", tenantId);

        // 检查租户是否已有学校信息
        School existingSchool = schoolMapper.selectByTenantId(tenantId);
        if (existingSchool != null) {
            throw new BusinessException("该租户已存在学校信息");
        }

        // 创建学校
        School school = new School();
        school.setTenantId(tenantId);
        school.setAddress(dto.getAddress());
        school.setPhone(dto.getPhone());
        school.setPrincipal(dto.getPrincipal());
        school.setEstablished(dto.getEstablished());
        school.setDescription(dto.getDescription());
        school.setLogo(dto.getLogo());
        school.setWebsite(dto.getWebsite());
        school.setValidState(1);
        school.setCreateBy(String.valueOf(creatorId));

        schoolMapper.insert(school);
        log.info("学校创建成功，学校 ID: {}", school.getId());

        return convertToVO(school);
    }

    @Override
    public SchoolVO getByTenantId(Long tenantId) {
        log.info("查询学校信息，租户 ID: {}", tenantId);

        School school = schoolMapper.selectByTenantId(tenantId);
        if (school == null) {
            throw new BusinessException("学校信息不存在");
        }

        return convertToVO(school);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SchoolVO update(UpdateSchoolDTO dto, Long tenantId, Long modifierId) {
        log.info("更新学校信息，租户 ID: {}", tenantId);

        School existingSchool = schoolMapper.selectByTenantId(tenantId);
        if (existingSchool == null) {
            throw new BusinessException("学校信息不存在");
        }

        // 更新学校信息
        School school = new School();
        school.setId(existingSchool.getId());
        school.setAddress(dto.getAddress());
        school.setPhone(dto.getPhone());
        school.setPrincipal(dto.getPrincipal());
        school.setEstablished(dto.getEstablished());
        school.setDescription(dto.getDescription());
        school.setLogo(dto.getLogo());
        school.setWebsite(dto.getWebsite());
        school.setUpdateBy(String.valueOf(modifierId));

        schoolMapper.updateById(school);
        log.info("学校更新成功，学校 ID: {}", school.getId());

        return convertToVO(schoolMapper.selectById(school.getId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long tenantId) {
        log.info("删除学校信息，租户 ID: {}", tenantId);

        School school = schoolMapper.selectByTenantId(tenantId);
        if (school == null) {
            throw new BusinessException("学校信息不存在");
        }

        // 删除学校（逻辑删除）
        schoolMapper.deleteById(school.getId());
        log.info("学校删除成功，学校 ID: {}", school.getId());
    }

    /**
     * 转换为 VO
     */
    private SchoolVO convertToVO(School school) {
        SchoolVO vo = new SchoolVO();
        vo.setId(school.getId());
        vo.setTenantId(school.getTenantId());
        vo.setAddress(school.getAddress());
        vo.setPhone(school.getPhone());
        vo.setPrincipal(school.getPrincipal());
        vo.setEstablished(school.getEstablished());
        vo.setDescription(school.getDescription());
        vo.setLogo(school.getLogo());
        vo.setWebsite(school.getWebsite());
        vo.setValidState(school.getValidState());
        vo.setCreateBy(school.getCreateBy());
        vo.setCreateTime(school.getCreateTime());
        vo.setUpdateBy(school.getUpdateBy());
        vo.setUpdateTime(school.getUpdateTime());
        return vo;
    }
}
