package org.dromara.elevator.service.impl;

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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EWorkMachineRoomBo;
import org.dromara.elevator.domain.vo.EWorkMachineRoomVo;
import org.dromara.elevator.domain.EWorkMachineRoom;
import org.dromara.elevator.mapper.EWorkMachineRoomMapper;
import org.dromara.elevator.service.IEWorkMachineRoomService;

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

/**
 * 工作记录机房信息Service业务层处理
 *
 * @author ym
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
public class EWorkMachineRoomServiceImpl implements IEWorkMachineRoomService {

    private final EWorkMachineRoomMapper baseMapper;

    /**
     * 查询工作记录机房信息
     *
     * @param id 主键
     * @return 工作记录机房信息
     */
    @Override
    public EWorkMachineRoomVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询工作记录机房信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 工作记录机房信息分页列表
     */
    @Override
    public TableDataInfo<EWorkMachineRoomVo> queryPageList(EWorkMachineRoomBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EWorkMachineRoom> lqw = buildQueryWrapper(bo);
        Page<EWorkMachineRoomVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的工作记录机房信息列表
     *
     * @param bo 查询条件
     * @return 工作记录机房信息列表
     */
    @Override
    public List<EWorkMachineRoomVo> queryList(EWorkMachineRoomBo bo) {
        LambdaQueryWrapper<EWorkMachineRoom> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EWorkMachineRoom> buildQueryWrapper(EWorkMachineRoomBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkMachineRoom> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EWorkMachineRoom::getElevatorNum, bo.getElevatorNum());
        lqw.eq(bo.getWorkId() != null, EWorkMachineRoom::getWorkId, bo.getWorkId());
        lqw.eq(bo.getMaintenancePlanId() != null, EWorkMachineRoom::getMaintenancePlanId, bo.getMaintenancePlanId());
        lqw.eq(bo.getEnvironmentState() != null, EWorkMachineRoom::getEnvironmentState, bo.getEnvironmentState());
        lqw.eq(StringUtils.isNotBlank(bo.getEnvironmentRemark()), EWorkMachineRoom::getEnvironmentRemark, bo.getEnvironmentRemark());
        lqw.eq(bo.getEmergencyOperationState() != null, EWorkMachineRoom::getEmergencyOperationState, bo.getEmergencyOperationState());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyOperationRemark()), EWorkMachineRoom::getEmergencyOperationRemark, bo.getEmergencyOperationRemark());
        lqw.eq(bo.getDriveHostState() != null, EWorkMachineRoom::getDriveHostState, bo.getDriveHostState());
        lqw.eq(StringUtils.isNotBlank(bo.getDriveHostRemark()), EWorkMachineRoom::getDriveHostRemark, bo.getDriveHostRemark());
        lqw.eq(bo.getBrakeShaftState() != null, EWorkMachineRoom::getBrakeShaftState, bo.getBrakeShaftState());
        lqw.eq(StringUtils.isNotBlank(bo.getBrakeShaftRemark()), EWorkMachineRoom::getBrakeShaftRemark, bo.getBrakeShaftRemark());
        lqw.eq(bo.getBrakeClearanceState() != null, EWorkMachineRoom::getBrakeClearanceState, bo.getBrakeClearanceState());
        lqw.eq(StringUtils.isNotBlank(bo.getBrakeClearanceRemark()), EWorkMachineRoom::getBrakeClearanceRemark, bo.getBrakeClearanceRemark());
        lqw.eq(bo.getShutdownSubsystemState() != null, EWorkMachineRoom::getShutdownSubsystemState, bo.getShutdownSubsystemState());
        lqw.eq(StringUtils.isNotBlank(bo.getShutdownSubsystemRemark()), EWorkMachineRoom::getShutdownSubsystemRemark, bo.getShutdownSubsystemRemark());
        lqw.eq(bo.getEncoderState() != null, EWorkMachineRoom::getEncoderState, bo.getEncoderState());
        lqw.eq(StringUtils.isNotBlank(bo.getEncoderRemark()), EWorkMachineRoom::getEncoderRemark, bo.getEncoderRemark());
        lqw.eq(bo.getSpeedLimiterState() != null, EWorkMachineRoom::getSpeedLimiterState, bo.getSpeedLimiterState());
        lqw.eq(StringUtils.isNotBlank(bo.getSpeedLimiterRemark()), EWorkMachineRoom::getSpeedLimiterRemark, bo.getSpeedLimiterRemark());
        lqw.eq(bo.getBypassDeviceState() != null, EWorkMachineRoom::getBypassDeviceState, bo.getBypassDeviceState());
        lqw.eq(StringUtils.isNotBlank(bo.getBypassDeviceRemark()), EWorkMachineRoom::getBypassDeviceRemark, bo.getBypassDeviceRemark());
        lqw.eq(bo.getEmergencyElectricalOperationState() != null, EWorkMachineRoom::getEmergencyElectricalOperationState, bo.getEmergencyElectricalOperationState());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyElectricalOperationRemark()), EWorkMachineRoom::getEmergencyElectricalOperationRemark, bo.getEmergencyElectricalOperationRemark());
        lqw.eq(bo.getFireSwitchState() != null, EWorkMachineRoom::getFireSwitchState, bo.getFireSwitchState());
        lqw.eq(StringUtils.isNotBlank(bo.getFireSwitchRemark()), EWorkMachineRoom::getFireSwitchRemark, bo.getFireSwitchRemark());
        lqw.eq(bo.getEnergyBufferState() != null, EWorkMachineRoom::getEnergyBufferState, bo.getEnergyBufferState());
        lqw.eq(StringUtils.isNotBlank(bo.getEnergyBufferRemark()), EWorkMachineRoom::getEnergyBufferRemark, bo.getEnergyBufferRemark());
        lqw.eq(bo.getBufferDistanceState() != null, EWorkMachineRoom::getBufferDistanceState, bo.getBufferDistanceState());
        lqw.eq(StringUtils.isNotBlank(bo.getBufferDistanceRemark()), EWorkMachineRoom::getBufferDistanceRemark, bo.getBufferDistanceRemark());
        lqw.eq(bo.getCompensationChainState() != null, EWorkMachineRoom::getCompensationChainState, bo.getCompensationChainState());
        lqw.eq(StringUtils.isNotBlank(bo.getCompensationChainRemark()), EWorkMachineRoom::getCompensationChainRemark, bo.getCompensationChainRemark());
        lqw.eq(bo.getLimitSwitchState() != null, EWorkMachineRoom::getLimitSwitchState, bo.getLimitSwitchState());
        lqw.eq(StringUtils.isNotBlank(bo.getLimitSwitchRemark()), EWorkMachineRoom::getLimitSwitchRemark, bo.getLimitSwitchRemark());
        lqw.eq(bo.getCableState() != null, EWorkMachineRoom::getCableState, bo.getCableState());
        lqw.eq(StringUtils.isNotBlank(bo.getCableRemark()), EWorkMachineRoom::getCableRemark, bo.getCableRemark());
        lqw.eq(bo.getLandingSillsState() != null, EWorkMachineRoom::getLandingSillsState, bo.getLandingSillsState());
        lqw.eq(StringUtils.isNotBlank(bo.getLandingSillsRemark()), EWorkMachineRoom::getLandingSillsRemark, bo.getLandingSillsRemark());
        lqw.eq(bo.getWeighDeviceState() != null, EWorkMachineRoom::getWeighDeviceState, bo.getWeighDeviceState());
        lqw.eq(StringUtils.isNotBlank(bo.getWeighDeviceRemark()), EWorkMachineRoom::getWeighDeviceRemark, bo.getWeighDeviceRemark());
        lqw.eq(bo.getDeptId() != null, EWorkMachineRoom::getDeptId, bo.getDeptId());
        return lqw;
    }

    /**
     * 新增工作记录机房信息
     *
     * @param bo 工作记录机房信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EWorkMachineRoomBo bo) {
        EWorkMachineRoom add = MapstructUtils.convert(bo, EWorkMachineRoom.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工作记录机房信息
     *
     * @param bo 工作记录机房信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EWorkMachineRoomBo bo) {
        EWorkMachineRoom update = MapstructUtils.convert(bo, EWorkMachineRoom.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EWorkMachineRoom 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 EWorkMachineRoomVo queryByWorId(Long workId) {
        return this.baseMapper.selectVoOne(Wrappers.<EWorkMachineRoom>lambdaQuery()
            .eq(EWorkMachineRoom::getWorkId, workId)
            .last(" limit 1 "));
    }
}
