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.EWorkElevatorCabinBo;
import org.dromara.elevator.domain.vo.EWorkElevatorCabinVo;
import org.dromara.elevator.domain.EWorkElevatorCabin;
import org.dromara.elevator.mapper.EWorkElevatorCabinMapper;
import org.dromara.elevator.service.IEWorkElevatorCabinService;

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

/**
 * 工作记录轿厢信息Service业务层处理
 *
 * @author ym
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
public class EWorkElevatorCabinServiceImpl implements IEWorkElevatorCabinService {

    private final EWorkElevatorCabinMapper baseMapper;

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

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

    /**
     * 查询符合条件的工作记录轿厢信息列表
     *
     * @param bo 查询条件
     * @return 工作记录轿厢信息列表
     */
    @Override
    public List<EWorkElevatorCabinVo> queryList(EWorkElevatorCabinBo bo) {
        LambdaQueryWrapper<EWorkElevatorCabin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EWorkElevatorCabin> buildQueryWrapper(EWorkElevatorCabinBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkElevatorCabin> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EWorkElevatorCabin::getElevatorNum, bo.getElevatorNum());
        lqw.eq(bo.getWorkId() != null, EWorkElevatorCabin::getWorkId, bo.getWorkId());
        lqw.eq(bo.getMaintenancePlanId() != null, EWorkElevatorCabin::getMaintenancePlanId, bo.getMaintenancePlanId());
        lqw.eq(bo.getFanLightState() != null, EWorkElevatorCabin::getFanLightState, bo.getFanLightState());
        lqw.eq(StringUtils.isNotBlank(bo.getFanLightRemark()), EWorkElevatorCabin::getFanLightRemark, bo.getFanLightRemark());
        lqw.eq(bo.getSwitchDeviceState() != null, EWorkElevatorCabin::getSwitchDeviceState, bo.getSwitchDeviceState());
        lqw.eq(StringUtils.isNotBlank(bo.getSwitchDeviceRemark()), EWorkElevatorCabin::getSwitchDeviceRemark, bo.getSwitchDeviceRemark());
        lqw.eq(bo.getAlarmIntercomState() != null, EWorkElevatorCabin::getAlarmIntercomState, bo.getAlarmIntercomState());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmIntercomRemark()), EWorkElevatorCabin::getAlarmIntercomRemark, bo.getAlarmIntercomRemark());
        lqw.eq(bo.getDisplayButtonCardState() != null, EWorkElevatorCabin::getDisplayButtonCardState, bo.getDisplayButtonCardState());
        lqw.eq(StringUtils.isNotBlank(bo.getDisplayButtonCardRemark()), EWorkElevatorCabin::getDisplayButtonCardRemark, bo.getDisplayButtonCardRemark());
        lqw.eq(bo.getProtectorState() != null, EWorkElevatorCabin::getProtectorState, bo.getProtectorState());
        lqw.eq(StringUtils.isNotBlank(bo.getProtectorRemark()), EWorkElevatorCabin::getProtectorRemark, bo.getProtectorRemark());
        lqw.eq(bo.getDoorLockContactState() != null, EWorkElevatorCabin::getDoorLockContactState, bo.getDoorLockContactState());
        lqw.eq(StringUtils.isNotBlank(bo.getDoorLockContactRemark()), EWorkElevatorCabin::getDoorLockContactRemark, bo.getDoorLockContactRemark());
        lqw.eq(bo.getDoorOperationStete() != null, EWorkElevatorCabin::getDoorOperationStete, bo.getDoorOperationStete());
        lqw.eq(StringUtils.isNotBlank(bo.getDoorOperationRemark()), EWorkElevatorCabin::getDoorOperationRemark, bo.getDoorOperationRemark());
        lqw.eq(bo.getLevelingAccuracyState() != null, EWorkElevatorCabin::getLevelingAccuracyState, bo.getLevelingAccuracyState());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelingaccuracyRemark()), EWorkElevatorCabin::getLevelingaccuracyRemark, bo.getLevelingaccuracyRemark());
        lqw.eq(bo.getCallDisplayState() != null, EWorkElevatorCabin::getCallDisplayState, bo.getCallDisplayState());
        lqw.eq(StringUtils.isNotBlank(bo.getCallDisplayRemark()), EWorkElevatorCabin::getCallDisplayRemark, bo.getCallDisplayRemark());
        lqw.eq(bo.getLandingSillsState() != null, EWorkElevatorCabin::getLandingSillsState, bo.getLandingSillsState());
        lqw.eq(StringUtils.isNotBlank(bo.getLandingSillsRemark()), EWorkElevatorCabin::getLandingSillsRemark, bo.getLandingSillsRemark());
        lqw.eq(bo.getAutoCloseState() != null, EWorkElevatorCabin::getAutoCloseState, bo.getAutoCloseState());
        lqw.eq(StringUtils.isNotBlank(bo.getAutoCloseRemark()), EWorkElevatorCabin::getAutoCloseRemark, bo.getAutoCloseRemark());
        lqw.eq(bo.getDoorLockResetState() != null, EWorkElevatorCabin::getDoorLockResetState, bo.getDoorLockResetState());
        lqw.eq(StringUtils.isNotBlank(bo.getDoorLockResetRemark()), EWorkElevatorCabin::getDoorLockResetRemark, bo.getDoorLockResetRemark());
        lqw.eq(bo.getElectricalContactState() != null, EWorkElevatorCabin::getElectricalContactState, bo.getElectricalContactState());
        lqw.eq(StringUtils.isNotBlank(bo.getElectricalContactRemark()), EWorkElevatorCabin::getElectricalContactRemark, bo.getElectricalContactRemark());
        lqw.eq(bo.getComponentLengthState() != null, EWorkElevatorCabin::getComponentLengthState, bo.getComponentLengthState());
        lqw.eq(StringUtils.isNotBlank(bo.getComponentLengthRemark()), EWorkElevatorCabin::getComponentLengthRemark, bo.getComponentLengthRemark());
        lqw.eq(bo.getGearboxLubricantState() != null, EWorkElevatorCabin::getGearboxLubricantState, bo.getGearboxLubricantState());
        lqw.eq(StringUtils.isNotBlank(bo.getGearboxLubricantRemark()), EWorkElevatorCabin::getGearboxLubricantRemark, bo.getGearboxLubricantRemark());
        lqw.eq(bo.getBrakeLiningState() != null, EWorkElevatorCabin::getBrakeLiningState, bo.getBrakeLiningState());
        lqw.eq(StringUtils.isNotBlank(bo.getBrakeLiningRemark()), EWorkElevatorCabin::getBrakeLiningRemark, bo.getBrakeLiningRemark());
        lqw.eq(bo.getEncoderState() != null, EWorkElevatorCabin::getEncoderState, bo.getEncoderState());
        lqw.eq(StringUtils.isNotBlank(bo.getEncoderRemark()), EWorkElevatorCabin::getEncoderRemark, bo.getEncoderRemark());
        lqw.eq(bo.getMovementContactState() != null, EWorkElevatorCabin::getMovementContactState, bo.getMovementContactState());
        lqw.eq(StringUtils.isNotBlank(bo.getMovementContactRemark()), EWorkElevatorCabin::getMovementContactRemark, bo.getMovementContactRemark());
        lqw.eq(bo.getWheelGrooveSuspensionState() != null, EWorkElevatorCabin::getWheelGrooveSuspensionState, bo.getWheelGrooveSuspensionState());
        lqw.eq(StringUtils.isNotBlank(bo.getWheelGrooveSuspensionRemark()), EWorkElevatorCabin::getWheelGrooveSuspensionRemark, bo.getWheelGrooveSuspensionRemark());
        lqw.eq(bo.getCouplingState() != null, EWorkElevatorCabin::getCouplingState, bo.getCouplingState());
        lqw.eq(StringUtils.isNotBlank(bo.getCouplingRemark()), EWorkElevatorCabin::getCouplingRemark, bo.getCouplingRemark());
        lqw.eq(bo.getBearingState() != null, EWorkElevatorCabin::getBearingState, bo.getBearingState());
        lqw.eq(StringUtils.isNotBlank(bo.getBearingRemark()), EWorkElevatorCabin::getBearingRemark, bo.getBearingRemark());
        lqw.eq(bo.getTractionSheaveState() != null, EWorkElevatorCabin::getTractionSheaveState, bo.getTractionSheaveState());
        lqw.eq(StringUtils.isNotBlank(bo.getTractionSheaveRemark()), EWorkElevatorCabin::getTractionSheaveRemark, bo.getTractionSheaveRemark());
        lqw.eq(bo.getLubricationChangeState() != null, EWorkElevatorCabin::getLubricationChangeState, bo.getLubricationChangeState());
        lqw.eq(StringUtils.isNotBlank(bo.getLubricationChangeRemark()), EWorkElevatorCabin::getLubricationChangeRemark, bo.getLubricationChangeRemark());
        lqw.eq(bo.getRelayContactState() != null, EWorkElevatorCabin::getRelayContactState, bo.getRelayContactState());
        lqw.eq(StringUtils.isNotBlank(bo.getRelayContactRemark()), EWorkElevatorCabin::getRelayContactRemark, bo.getRelayContactRemark());
        lqw.eq(bo.getIronCoreState() != null, EWorkElevatorCabin::getIronCoreState, bo.getIronCoreState());
        lqw.eq(StringUtils.isNotBlank(bo.getIronCoreRemark()), EWorkElevatorCabin::getIronCoreRemark, bo.getIronCoreRemark());
        lqw.eq(bo.getBrakeCapacityState() != null, EWorkElevatorCabin::getBrakeCapacityState, bo.getBrakeCapacityState());
        lqw.eq(StringUtils.isNotBlank(bo.getBrakeCapacityRemark()), EWorkElevatorCabin::getBrakeCapacityRemark, bo.getBrakeCapacityRemark());
        lqw.eq(bo.getIsolationState() != null, EWorkElevatorCabin::getIsolationState, bo.getIsolationState());
        lqw.eq(StringUtils.isNotBlank(bo.getIsolationRemark()), EWorkElevatorCabin::getIsolationRemark, bo.getIsolationRemark());
        lqw.eq(bo.getSpeedLimiterState() != null, EWorkElevatorCabin::getSpeedLimiterState, bo.getSpeedLimiterState());
        lqw.eq(StringUtils.isNotBlank(bo.getSpeedLimiterRemark()), EWorkElevatorCabin::getSpeedLimiterRemark, bo.getSpeedLimiterRemark());
        lqw.eq(bo.getDeptId() != null, EWorkElevatorCabin::getDeptId, bo.getDeptId());
        return lqw;
    }

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

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

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