package com.usky.meeting.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.meeting.domain.MeetingFloor;
import com.usky.meeting.domain.MeetingRoom;
import com.usky.meeting.mapper.MeetingFloorMapper;
import com.usky.meeting.repository.MeetingRoomRepository;
import com.usky.meeting.service.MeetingFloorService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.meeting.service.vo.MeetingFloorRequestVO;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 楼层表 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2024-03-08
 */
@Service
public class MeetingFloorServiceImpl extends AbstractCrudService<MeetingFloorMapper, MeetingFloor> implements MeetingFloorService {

    @Autowired
    private MeetingRoomRepository meetingRoomRepository;

    @Override
    public CommonPage<MeetingFloor> meetingFloorList(MeetingFloorRequestVO requestVO){
        Integer current = requestVO.getCurrent();
        Integer size = requestVO.getSize();
        IPage<MeetingFloor> page = new Page<>(current,size);
        LambdaQueryWrapper<MeetingFloor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(requestVO.getFloorName()),MeetingFloor::getFloorName,requestVO.getFloorName())
                .like(StringUtils.isNotBlank(requestVO.getFloorPic()),MeetingFloor::getFloorPic,requestVO.getFloorPic())
                .like(StringUtils.isNotBlank(requestVO.getContacts()),MeetingFloor::getContacts,requestVO.getContacts())
                .eq(MeetingFloor::getTenantId,SecurityUtils.getTenantId())
                .orderByAsc(MeetingFloor::getSortIndex);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),size,current);

    }

    @Override
    public void add(MeetingFloor meetingFloor){
        if(checkNameUnique(meetingFloor)){
            throw new BusinessException("新增楼层信息失败，"+meetingFloor.getFloorName()+"已经存在");
        }
//        Snowflake snowflake = IdUtil.createSnowflake(1,1);
//        meetingFloor.setFloorId(snowflake.nextId());
        meetingFloor.setCreateBy(SecurityUtils.getUsername());
        meetingFloor.setCreateTime(LocalDateTime.now());
        meetingFloor.setTenantId(SecurityUtils.getTenantId());

        this.save(meetingFloor);
    }

    @Override
    public void edit(MeetingFloor meetingFloor){
        if(checkNameUnique(meetingFloor)){
            throw new BusinessException("修改楼层信息失败，"+meetingFloor.getFloorName()+"已经存在");
        }
        meetingFloor.setUpdateBy(SecurityUtils.getUsername());
        meetingFloor.setUpdateTime(LocalDateTime.now());

        this.updateById(meetingFloor);
    }

    @Override
    public void remove(Long floorId){
        List<MeetingRoom> list = meetingRoomRepository.getMeetingRoomList(floorId);
        if(list.size() > 0){
            throw new BusinessException("当前楼层有关联会议室，不可删除");
        }

        MeetingFloor meetingFloor = this.getById(floorId);
        Optional.ofNullable(meetingFloor).orElseThrow(() -> new BusinessException("该楼层信息不存在"));

        this.removeById(floorId);
    }

    public boolean checkNameUnique(MeetingFloor meetingFloor){
        Long id = null == meetingFloor.getFloorId()?-1:meetingFloor.getFloorId();
        LambdaQueryWrapper<MeetingFloor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MeetingFloor::getFloorName,meetingFloor.getFloorName())
                .eq(MeetingFloor::getTenantId, SecurityUtils.getTenantId());
        MeetingFloor one = this.getOne(queryWrapper);
        return null != one && !Objects.equals(one.getFloorId(),id);

    }
}
