package com.hotwj.platform.resourceManagement.vehicleTerminalInstall.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 com.hotwj.platform.resourceManagement.vehicleTerminalInstall.domain.HotVehicleTerminalInstall;
import com.hotwj.platform.resourceManagement.vehicleTerminalInstall.domain.bo.HotVehicleTerminalInstallBo;
import com.hotwj.platform.resourceManagement.vehicleTerminalInstall.domain.vo.HotVehicleTerminalInstallVo;
import com.hotwj.platform.resourceManagement.vehicleTerminalInstall.mapper.HotVehicleTerminalInstallMapper;
import com.hotwj.platform.resourceManagement.vehicleTerminalInstall.service.IHotVehicleTerminalInstallService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.springframework.stereotype.Service;

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

/**
 * 定位终端安装记录Service业务层处理
 *
 * @author shihongwei
 * @date 2025-12-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotVehicleTerminalInstallServiceImpl implements IHotVehicleTerminalInstallService {

    private final HotVehicleTerminalInstallMapper baseMapper;

    /**
     * 查询定位终端安装记录
     *
     * @param id 主键
     * @return 定位终端安装记录
     */
    @Override
    public HotVehicleTerminalInstallVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询定位终端安装记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 定位终端安装记录分页列表
     */
    @Override
    public TableDataInfo<HotVehicleTerminalInstallVo> queryPageList(HotVehicleTerminalInstallBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotVehicleTerminalInstall> lqw = buildQueryWrapper(bo);
        Page<HotVehicleTerminalInstallVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<HotVehicleTerminalInstallVo> queryPageListForVehiclePage(HotVehicleTerminalInstallBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotVehicleTerminalInstall> lqw = buildVehiclePageQueryWrapper(bo);
        Page<HotVehicleTerminalInstallVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的定位终端安装记录列表
     *
     * @param bo 查询条件
     * @return 定位终端安装记录列表
     */
    @Override
    public List<HotVehicleTerminalInstallVo> queryList(HotVehicleTerminalInstallBo bo) {
        LambdaQueryWrapper<HotVehicleTerminalInstall> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotVehicleTerminalInstall> buildQueryWrapper(HotVehicleTerminalInstallBo bo) {
        Map<String, Object> params = bo.getParams();
        Long isDeleted = bo.getIsDeleted() == null ? 0L : bo.getIsDeleted();
        LambdaQueryWrapper<HotVehicleTerminalInstall> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(HotVehicleTerminalInstall::getId);
        lqw.eq(bo.getCompanyId() != null, HotVehicleTerminalInstall::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getVehicleId() != null, HotVehicleTerminalInstall::getVehicleId, bo.getVehicleId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), HotVehicleTerminalInstall::getPlateNumber, bo.getPlateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceProvider()), HotVehicleTerminalInstall::getServiceProvider, bo.getServiceProvider());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentNo()), HotVehicleTerminalInstall::getDocumentNo, bo.getDocumentNo());
        lqw.eq(StringUtils.isNotBlank(bo.getRecorder()), HotVehicleTerminalInstall::getRecorder, bo.getRecorder());
        lqw.eq(bo.getInstallDate() != null, HotVehicleTerminalInstall::getInstallDate, bo.getInstallDate());
        lqw.eq(bo.getServiceEndDate() != null, HotVehicleTerminalInstall::getServiceEndDate, bo.getServiceEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterImageUrls()), HotVehicleTerminalInstall::getRegisterImageUrls, bo.getRegisterImageUrls());
        lqw.eq(HotVehicleTerminalInstall::getIsDeleted, isDeleted);
        return lqw;
    }

    private LambdaQueryWrapper<HotVehicleTerminalInstall> buildVehiclePageQueryWrapper(HotVehicleTerminalInstallBo bo) {
        Long isDeleted = bo.getIsDeleted() == null ? 0L : bo.getIsDeleted();
        LambdaQueryWrapper<HotVehicleTerminalInstall> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(HotVehicleTerminalInstall::getId);
        lqw.eq(bo.getCompanyId() != null, HotVehicleTerminalInstall::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getVehicleId() != null, HotVehicleTerminalInstall::getVehicleId, bo.getVehicleId());
        lqw.like(StringUtils.isNotBlank(bo.getServiceProvider()), HotVehicleTerminalInstall::getServiceProvider, bo.getServiceProvider());
        lqw.like(StringUtils.isNotBlank(bo.getDocumentNo()), HotVehicleTerminalInstall::getDocumentNo, bo.getDocumentNo());
        lqw.eq(bo.getInstallDate() != null, HotVehicleTerminalInstall::getInstallDate, bo.getInstallDate());
        lqw.eq(bo.getServiceEndDate() != null, HotVehicleTerminalInstall::getServiceEndDate, bo.getServiceEndDate());
        lqw.eq(HotVehicleTerminalInstall::getIsDeleted, isDeleted);
        return lqw;
    }

    /**
     * 新增定位终端安装记录
     *
     * @param bo 定位终端安装记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotVehicleTerminalInstallBo bo) {
        HotVehicleTerminalInstall add = MapstructUtils.convert(bo, HotVehicleTerminalInstall.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定位终端安装记录
     *
     * @param bo 定位终端安装记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotVehicleTerminalInstallBo bo) {
        HotVehicleTerminalInstall update = MapstructUtils.convert(bo, HotVehicleTerminalInstall.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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