package org.dromara.system.service.impl.reservat;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.reservat.XiaoyuanReservatConfig;
import org.dromara.system.domain.reservat.bo.XiaoyuanReservatConfigBo;
import org.dromara.system.domain.reservat.vo.XiaoyuanReservatConfigVo;
import org.dromara.system.mapper.reservat.XiaoyuanReservatConfigMapper;
import org.dromara.system.service.reservat.IXiaoyuanReservatConfigService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 校园预约配置Service业务层处理
 *
 * @author boman
 * @date 2023-09-12
 */
@RequiredArgsConstructor
@Service
public class XiaoyuanReservatConfigServiceImpl implements IXiaoyuanReservatConfigService {

    private final XiaoyuanReservatConfigMapper baseMapper;

    /**
     * 查询校园预约配置
     */
    @Override
    public XiaoyuanReservatConfigVo queryById(Long reservatConfigId){
        return baseMapper.selectVoById(reservatConfigId);
    }

    /**
     * 查询校园预约配置列表
     */
    @Override
    public TableDataInfo<XiaoyuanReservatConfigVo> queryPageList(XiaoyuanReservatConfigBo bo, PageQuery pageQuery) {
        bo.setTeacherPhone(LoginHelper.getUsername());
        LambdaQueryWrapper<XiaoyuanReservatConfig> lqw = buildQueryWrapper(bo);
        Page<XiaoyuanReservatConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询校园预约配置列表
     */
    @Override
    public List<XiaoyuanReservatConfigVo> queryList(XiaoyuanReservatConfigBo bo) {
        bo.setTeacherPhone(LoginHelper.getUsername());
        LambdaQueryWrapper<XiaoyuanReservatConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XiaoyuanReservatConfig> buildQueryWrapper(XiaoyuanReservatConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XiaoyuanReservatConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTeacherName()), XiaoyuanReservatConfig::getTeacherName, bo.getTeacherName());
        lqw.eq(bo.getTeacherId() != null, XiaoyuanReservatConfig::getTeacherId, bo.getTeacherId());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherPhone()), XiaoyuanReservatConfig::getTeacherPhone, bo.getTeacherPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getReservatConfigDate()), XiaoyuanReservatConfig::getReservatConfigDate, bo.getReservatConfigDate());
        lqw.eq(bo.getReservatConfigTimeAmBegin() != null, XiaoyuanReservatConfig::getReservatConfigTimeAmBegin, bo.getReservatConfigTimeAmBegin());
        lqw.eq(bo.getReservatConfigTimeAmEnd() != null, XiaoyuanReservatConfig::getReservatConfigTimeAmEnd, bo.getReservatConfigTimeAmEnd());
        lqw.eq(bo.getReservatConfigTimePmBegin() != null, XiaoyuanReservatConfig::getReservatConfigTimePmBegin, bo.getReservatConfigTimePmBegin());
        lqw.eq(bo.getReservatConfigTimePmEnd() != null, XiaoyuanReservatConfig::getReservatConfigTimePmEnd, bo.getReservatConfigTimePmEnd());
        lqw.eq(bo.getReservatConfigNum() != null, XiaoyuanReservatConfig::getReservatConfigNum, bo.getReservatConfigNum());
        return lqw;
    }

    /**
     * 新增校园预约配置
     */
    @Override
    public Boolean insertByBo(XiaoyuanReservatConfigBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setTeacherName(loginUser.getNickname());
        bo.setTeacherPhone(loginUser.getUsername());
        bo.setTeacherId(loginUser.getUserId());
        XiaoyuanReservatConfig add = MapstructUtils.convert(bo, XiaoyuanReservatConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReservatConfigId(add.getReservatConfigId());
        }
        return flag;
    }

    /**
     * 修改校园预约配置
     */
    @Override
    public Boolean updateByBo(XiaoyuanReservatConfigBo bo) {
        XiaoyuanReservatConfig update = MapstructUtils.convert(bo, XiaoyuanReservatConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XiaoyuanReservatConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除校园预约配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
