package org.dromara.elevator.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.DateUtils;
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.elevator.domain.EElevatorExamination;
import org.dromara.elevator.domain.bo.EElevatorExaminationBo;
import org.dromara.elevator.domain.vo.EElevatorExaminationVo;
import org.dromara.elevator.mapper.EElevatorExaminationMapper;
import org.dromara.elevator.service.IEElevatorExaminationService;
import org.springframework.stereotype.Service;

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

/**
 * 电梯体检Service业务层处理
 *
 * @author ym
 * @date 2024-10-11
 */
@RequiredArgsConstructor
@Service
public class EElevatorExaminationServiceImpl implements IEElevatorExaminationService {

    private final EElevatorExaminationMapper baseMapper;

    /**
     * 查询电梯体检
     *
     * @param id 主键
     * @return 电梯体检
     */
    @Override
    public EElevatorExaminationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询电梯体检列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 电梯体检分页列表
     */
    @Override
    public TableDataInfo<EElevatorExaminationVo> queryPageList(EElevatorExaminationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EElevatorExamination> lqw = buildQueryWrapper(bo);
        Page<EElevatorExaminationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的电梯体检列表
     *
     * @param bo 查询条件
     * @return 电梯体检列表
     */
    @Override
    public List<EElevatorExaminationVo> queryList(EElevatorExaminationBo bo) {
        LambdaQueryWrapper<EElevatorExamination> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EElevatorExamination> buildQueryWrapper(EElevatorExaminationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EElevatorExamination> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EElevatorExamination::getElevatorNum, bo.getElevatorNum());
        lqw.eq(StringUtils.isNotBlank(bo.getExaminationTime()), EElevatorExamination::getExaminationTime, bo.getExaminationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getExaminationNum()), EElevatorExamination::getExaminationNum, bo.getExaminationNum());
        lqw.eq(bo.getSafetyIndex() != null, EElevatorExamination::getSafetyIndex, bo.getSafetyIndex());
        lqw.eq(bo.getMaintenanceIndex() != null, EElevatorExamination::getMaintenanceIndex, bo.getMaintenanceIndex());
        lqw.eq(bo.getPeakDate() != null, EElevatorExamination::getPeakDate, bo.getPeakDate());
        lqw.eq(bo.getPeakRunNum() != null, EElevatorExamination::getPeakRunNum, bo.getPeakRunNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPeakHourData()), EElevatorExamination::getPeakHourData, bo.getPeakHourData());
        lqw.eq(bo.getBusyFloor() != null, EElevatorExamination::getBusyFloor, bo.getBusyFloor());
        lqw.eq(bo.getBusyFloorNum() != null, EElevatorExamination::getBusyFloorNum, bo.getBusyFloorNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStatisticsDayData()), EElevatorExamination::getStatisticsDayData, bo.getStatisticsDayData());
        lqw.eq(StringUtils.isNotBlank(bo.getStatisticsWeekData()), EElevatorExamination::getStatisticsWeekData, bo.getStatisticsWeekData());
        lqw.eq(StringUtils.isNotBlank(bo.getStatisticsMonthData()), EElevatorExamination::getStatisticsMonthData, bo.getStatisticsMonthData());
        lqw.eq(StringUtils.isNotBlank(bo.getProblemDetail()), EElevatorExamination::getProblemDetail, bo.getProblemDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkDetail()), EElevatorExamination::getWorkDetail, bo.getWorkDetail());
        lqw.eq(bo.getDeptId() != null, EElevatorExamination::getDeptId, bo.getDeptId());
        return lqw;
    }

    /**
     * 新增电梯体检
     *
     * @param bo 电梯体检
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EElevatorExaminationBo bo) {
        EElevatorExamination add = MapstructUtils.convert(bo, EElevatorExamination.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改电梯体检
     *
     * @param bo 电梯体检
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EElevatorExaminationBo bo) {
        EElevatorExamination update = MapstructUtils.convert(bo, EElevatorExamination.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public EElevatorExaminationVo queryExaminationInfo(EElevatorExaminationBo bo) {
        return this.baseMapper.selectVoOne(Wrappers.<EElevatorExamination>lambdaQuery()
            .eq(EElevatorExamination::getElevatorNum, bo.getElevatorNum())
            .ge(EElevatorExamination::getPeakDate, DateUtils.toLocalDate(bo.getStartTime()))
            .le(EElevatorExamination::getPeakDate, DateUtils.toLocalDate(bo.getEndTime()))
            .last(" limit 1 "));
    }

    @Override
    public Boolean insertOrupdate(EElevatorExaminationBo bo) {
        return this.baseMapper.insertOrUpdate(MapstructUtils.convert(bo, EElevatorExamination.class));
    }
}
