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.EWorkCarRoofBo;
import org.dromara.elevator.domain.vo.EWorkCarRoofVo;
import org.dromara.elevator.domain.EWorkCarRoof;
import org.dromara.elevator.mapper.EWorkCarRoofMapper;
import org.dromara.elevator.service.IEWorkCarRoofService;

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

/**
 * 工作记录轿顶信息;Service业务层处理
 *
 * @author zy
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
public class EWorkCarRoofServiceImpl implements IEWorkCarRoofService {

    private final EWorkCarRoofMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<EWorkCarRoof> buildQueryWrapper(EWorkCarRoofBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkCarRoof> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EWorkCarRoof::getElevatorNum, bo.getElevatorNum());
        lqw.eq(Objects.nonNull(bo.getWorkId()), EWorkCarRoof::getWorkId, bo.getWorkId());
        lqw.eq(Objects.nonNull(bo.getMaintenancePlanId()), EWorkCarRoof::getMaintenancePlanId, bo.getMaintenancePlanId());
        lqw.eq(bo.getCarRoofState() != null, EWorkCarRoof::getCarRoofState, bo.getCarRoofState());
        lqw.eq(StringUtils.isNotBlank(bo.getCarRoofRemark()), EWorkCarRoof::getCarRoofRemark, bo.getCarRoofRemark());
        lqw.eq(bo.getOverhaulSwitchState() != null, EWorkCarRoof::getOverhaulSwitchState, bo.getOverhaulSwitchState());
        lqw.eq(StringUtils.isNotBlank(bo.getOverhaulSwitchRemark()), EWorkCarRoof::getOverhaulSwitchRemark, bo.getOverhaulSwitchRemark());
        lqw.eq(bo.getGuideShoeState() != null, EWorkCarRoof::getGuideShoeState, bo.getGuideShoeState());
        lqw.eq(StringUtils.isNotBlank(bo.getGuideShoeRemark()), EWorkCarRoof::getGuideShoeRemark, bo.getGuideShoeRemark());
        lqw.eq(bo.getPressingPlateState() != null, EWorkCarRoof::getPressingPlateState, bo.getPressingPlateState());
        lqw.eq(StringUtils.isNotBlank(bo.getPressingPlateRemark()), EWorkCarRoof::getPressingPlateRemark, bo.getPressingPlateRemark());
        lqw.eq(bo.getWellLightingState() != null, EWorkCarRoof::getWellLightingState, bo.getWellLightingState());
        lqw.eq(StringUtils.isNotBlank(bo.getWellLightingRemark()), EWorkCarRoof::getWellLightingRemark, bo.getWellLightingRemark());
        lqw.eq(bo.getRaceState() != null, EWorkCarRoof::getRaceState, bo.getRaceState());
        lqw.eq(StringUtils.isNotBlank(bo.getRaceRemark()), EWorkCarRoof::getRaceRemark, bo.getRaceRemark());
        lqw.eq(bo.getShoeGuideState() != null, EWorkCarRoof::getShoeGuideState, bo.getShoeGuideState());
        lqw.eq(StringUtils.isNotBlank(bo.getShoeGuideRemark()), EWorkCarRoof::getShoeGuideRemark, bo.getShoeGuideRemark());
        lqw.eq(bo.getElectricSafetyState() != null, EWorkCarRoof::getElectricSafetyState, bo.getElectricSafetyState());
        lqw.eq(StringUtils.isNotBlank(bo.getElectricSafetyRemark()), EWorkCarRoof::getElectricSafetyRemark, bo.getElectricSafetyRemark());
        lqw.eq(bo.getDriveState() != null, EWorkCarRoof::getDriveState, bo.getDriveState());
        lqw.eq(StringUtils.isNotBlank(bo.getDriveRemark()), EWorkCarRoof::getDriveRemark, bo.getDriveRemark());
        lqw.eq(bo.getGateGuideShoeState() != null, EWorkCarRoof::getGateGuideShoeState, bo.getGateGuideShoeState());
        lqw.eq(StringUtils.isNotBlank(bo.getGateGuideShoeRemark()), EWorkCarRoof::getGateGuideShoeRemark, bo.getGateGuideShoeRemark());
        lqw.eq(bo.getBearingUnitState() != null, EWorkCarRoof::getBearingUnitState, bo.getBearingUnitState());
        lqw.eq(StringUtils.isNotBlank(bo.getBearingUnitRemark()), EWorkCarRoof::getBearingUnitRemark, bo.getBearingUnitRemark());
        lqw.eq(bo.getSuspensionGearState() != null, EWorkCarRoof::getSuspensionGearState, bo.getSuspensionGearState());
        lqw.eq(StringUtils.isNotBlank(bo.getSuspensionGearRemark()), EWorkCarRoof::getSuspensionGearRemark, bo.getSuspensionGearRemark());
        lqw.eq(bo.getRopeFasteningState() != null, EWorkCarRoof::getRopeFasteningState, bo.getRopeFasteningState());
        lqw.eq(StringUtils.isNotBlank(bo.getRopeFasteningRemark()), EWorkCarRoof::getRopeFasteningRemark, bo.getRopeFasteningRemark());
        lqw.eq(bo.getWireropeState() != null, EWorkCarRoof::getWireropeState, bo.getWireropeState());
        lqw.eq(StringUtils.isNotBlank(bo.getWireropeRemark()), EWorkCarRoof::getWireropeRemark, bo.getWireropeRemark());
        lqw.eq(bo.getDoorLeafState() != null, EWorkCarRoof::getDoorLeafState, bo.getDoorLeafState());
        lqw.eq(StringUtils.isNotBlank(bo.getDoorLeafRemark()), EWorkCarRoof::getDoorLeafRemark, bo.getDoorLeafRemark());
        lqw.eq(bo.getDoorAstrictState() != null, EWorkCarRoof::getDoorAstrictState, bo.getDoorAstrictState());
        lqw.eq(StringUtils.isNotBlank(bo.getDoorAstrictRemark()), EWorkCarRoof::getDoorAstrictRemark, bo.getDoorAstrictRemark());
        lqw.eq(bo.getOverspeedState() != null, EWorkCarRoof::getOverspeedState, bo.getOverspeedState());
        lqw.eq(StringUtils.isNotBlank(bo.getOverspeedRemark()), EWorkCarRoof::getOverspeedRemark, bo.getOverspeedRemark());
        lqw.eq(bo.getAccidentMoveState() != null, EWorkCarRoof::getAccidentMoveState, bo.getAccidentMoveState());
        lqw.eq(StringUtils.isNotBlank(bo.getAccidentMoveRemark()), EWorkCarRoof::getAccidentMoveRemark, bo.getAccidentMoveRemark());
        lqw.eq(bo.getRailBracketState() != null, EWorkCarRoof::getRailBracketState, bo.getRailBracketState());
        lqw.eq(StringUtils.isNotBlank(bo.getRailBracketRemark()), EWorkCarRoof::getRailBracketRemark, bo.getRailBracketRemark());
        lqw.eq(bo.getGuideRailState() != null, EWorkCarRoof::getGuideRailState, bo.getGuideRailState());
        lqw.eq(StringUtils.isNotBlank(bo.getGuideRailRemark()), EWorkCarRoof::getGuideRailRemark, bo.getGuideRailRemark());
        lqw.eq(bo.getAccessoryState() != null, EWorkCarRoof::getAccessoryState, bo.getAccessoryState());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessoryRemark()), EWorkCarRoof::getAccessoryRemark, bo.getAccessoryRemark());
        lqw.eq(bo.getDeptId() != null, EWorkCarRoof::getDeptId, bo.getDeptId());
        return lqw;
    }

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

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

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