package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.domain.reservRecord.ReservRecord;
import org.dromara.domain.reservRecord.bo.ReservRecordBo;
import org.dromara.domain.reservRecord.vo.ReservRecordVo;
import org.dromara.mapper.ReservRecordMapper;
import org.dromara.service.IReservRecordService;
import org.springframework.stereotype.Service;

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

/**
 * 预约记录Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class ReservRecordServiceImpl implements IReservRecordService {

    private final ReservRecordMapper baseMapper;

    /**
     * 查询预约记录
     *
     * @param reservRecordId 主键
     * @return 预约记录
     */
    @Override
    public ReservRecordVo queryById(Long reservRecordId){
        return baseMapper.selectVoById(reservRecordId);
    }

    /**
     * 分页查询预约记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 预约记录分页列表
     */
    @Override
    public TableDataInfo<ReservRecordVo> queryPageList(ReservRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReservRecord> lqw = buildQueryWrapper(bo);
        Page<ReservRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的预约记录列表
     *
     * @param bo 查询条件
     * @return 预约记录列表
     */
    @Override
    public List<ReservRecordVo> queryList(ReservRecordBo bo) {
        LambdaQueryWrapper<ReservRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReservRecord> buildQueryWrapper(ReservRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReservRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ReservRecord::getReservRecordId);
        lqw.eq(bo.getPortalId() != null, ReservRecord::getPortalId, bo.getPortalId());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseAddress()), ReservRecord::getHouseAddress, bo.getHouseAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailAddress()), ReservRecord::getDetailAddress, bo.getDetailAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLgt()), ReservRecord::getLgt, bo.getLgt());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), ReservRecord::getLat, bo.getLat());
        lqw.like(StringUtils.isNotBlank(bo.getShareName()), ReservRecord::getShareName, bo.getShareName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ReservRecord::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getNum()), ReservRecord::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), ReservRecord::getReason, bo.getReason());
        lqw.eq(StringUtils.isNotBlank(bo.getMobileNumber()), ReservRecord::getMobileNumber, bo.getMobileNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCar()), ReservRecord::getIsCar, bo.getIsCar());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), ReservRecord::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getReserveTime() != null, ReservRecord::getReserveTime, bo.getReserveTime());
        lqw.eq(bo.getVisitorTime() != null, ReservRecord::getVisitorTime, bo.getVisitorTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ReservRecord::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), ReservRecord::getPhoto, bo.getPhoto());
        return lqw;
    }

    /**
     * 新增预约记录
     *
     * @param bo 预约记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ReservRecordBo bo) {
        ReservRecord add = MapstructUtils.convert(bo, ReservRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReservRecordId(add.getReservRecordId());
        }
        return flag;
    }

    /**
     * 修改预约记录
     *
     * @param bo 预约记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ReservRecordBo bo) {
        ReservRecord update = MapstructUtils.convert(bo, ReservRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除预约记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
