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.EWorkPitBo;
import org.dromara.elevator.domain.vo.EWorkPitVo;
import org.dromara.elevator.domain.EWorkPit;
import org.dromara.elevator.mapper.EWorkPitMapper;
import org.dromara.elevator.service.IEWorkPitService;

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 EWorkPitServiceImpl implements IEWorkPitService {

    private final EWorkPitMapper baseMapper;

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

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

    /**
     * 查询符合条件的工作记录底坑信息;列表
     *
     * @param bo 查询条件
     * @return 工作记录底坑信息;列表
     */
    @Override
    public List<EWorkPitVo> queryList(EWorkPitBo bo) {
        LambdaQueryWrapper<EWorkPit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EWorkPit> buildQueryWrapper(EWorkPitBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkPit> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EWorkPit::getElevatorNum, bo.getElevatorNum());
        lqw.eq(Objects.nonNull(bo.getWorkId()), EWorkPit::getWorkId, bo.getWorkId());
        lqw.eq(Objects.nonNull(bo.getMaintenancePlanId()), EWorkPit::getMaintenancePlanId, bo.getMaintenancePlanId());
        lqw.eq(bo.getEnvironmentState() != null, EWorkPit::getEnvironmentState, bo.getEnvironmentState());
        lqw.eq(StringUtils.isNotBlank(bo.getEnvironmentRemark()), EWorkPit::getEnvironmentRemark, bo.getEnvironmentRemark());
        lqw.eq(bo.getStopFeatureState() != null, EWorkPit::getStopFeatureState, bo.getStopFeatureState());
        lqw.eq(StringUtils.isNotBlank(bo.getStopFeatureRemark()), EWorkPit::getStopFeatureRemark, bo.getStopFeatureRemark());
        lqw.eq(bo.getTensioningWheelState() != null, EWorkPit::getTensioningWheelState, bo.getTensioningWheelState());
        lqw.eq(StringUtils.isNotBlank(bo.getTensioningWheelRemark()), EWorkPit::getTensioningWheelRemark, bo.getTensioningWheelRemark());
        lqw.eq(bo.getConditionMonitorState() != null, EWorkPit::getConditionMonitorState, bo.getConditionMonitorState());
        lqw.eq(StringUtils.isNotBlank(bo.getConditionMonitorRemark()), EWorkPit::getConditionMonitorRemark, bo.getConditionMonitorRemark());
        lqw.eq(bo.getTerminalState() != null, EWorkPit::getTerminalState, bo.getTerminalState());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalRemark()), EWorkPit::getTerminalRemark, bo.getTerminalRemark());
        lqw.eq(bo.getMeterState() != null, EWorkPit::getMeterState, bo.getMeterState());
        lqw.eq(StringUtils.isNotBlank(bo.getMeterRemark()), EWorkPit::getMeterRemark, bo.getMeterRemark());
        lqw.eq(bo.getLiftSafetyGearState() != null, EWorkPit::getLiftSafetyGearState, bo.getLiftSafetyGearState());
        lqw.eq(StringUtils.isNotBlank(bo.getLiftSafetyGearRemark()), EWorkPit::getLiftSafetyGearRemark, bo.getLiftSafetyGearRemark());
        lqw.eq(bo.getBoltState() != null, EWorkPit::getBoltState, bo.getBoltState());
        lqw.eq(StringUtils.isNotBlank(bo.getBoltRemark()), EWorkPit::getBoltRemark, bo.getBoltRemark());
        lqw.eq(bo.getAntiFluctuatorState() != null, EWorkPit::getAntiFluctuatorState, bo.getAntiFluctuatorState());
        lqw.eq(StringUtils.isNotBlank(bo.getAntiFluctuatorRemark()), EWorkPit::getAntiFluctuatorRemark, bo.getAntiFluctuatorRemark());
        lqw.eq(bo.getDeptId() != null, EWorkPit::getDeptId, bo.getDeptId());
        return lqw;
    }

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

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

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