package com.hotwj.platform.resourceManagement.vehicleCertReissue.service.impl;

import cn.hutool.core.collection.CollUtil;
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.vehicleCertReissue.domain.HotVehicleCertReissue;
import com.hotwj.platform.resourceManagement.vehicleCertReissue.domain.bo.HotVehicleCertReissueBo;
import com.hotwj.platform.resourceManagement.vehicleCertReissue.domain.vo.HotVehicleCertReissueVo;
import com.hotwj.platform.resourceManagement.vehicleCertReissue.mapper.HotVehicleCertReissueMapper;
import com.hotwj.platform.resourceManagement.vehicleCertReissue.service.IHotVehicleCertReissueService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
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 HotVehicleCertReissueServiceImpl implements IHotVehicleCertReissueService {

    private final HotVehicleCertReissueMapper baseMapper;

    /**
     * 查询证件补领登记
     *
     * @param id 主键
     * @return 证件补领登记
     */
    @Override
    public HotVehicleCertReissueVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询证件补领登记列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 证件补领登记分页列表
     */
    @Override
    public TableDataInfo<HotVehicleCertReissueVo> queryPageList(HotVehicleCertReissueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotVehicleCertReissue> lqw = buildQueryWrapper(bo);
        Page<HotVehicleCertReissueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的证件补领登记列表
     *
     * @param bo 查询条件
     * @return 证件补领登记列表
     */
    @Override
    public List<HotVehicleCertReissueVo> queryList(HotVehicleCertReissueBo bo) {
        LambdaQueryWrapper<HotVehicleCertReissue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotVehicleCertReissue> buildQueryWrapper(HotVehicleCertReissueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotVehicleCertReissue> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotVehicleCertReissue::getId);
        lqw.eq(bo.getCompanyId() != null, HotVehicleCertReissue::getCompanyId, bo.getCompanyId());
        lqw.in(CollUtil.isNotEmpty(bo.getIds()), HotVehicleCertReissue::getId, bo.getIds());
        lqw.eq(bo.getVehicleId() != null, HotVehicleCertReissue::getVehicleId, bo.getVehicleId());
        lqw.like(StringUtils.isNotBlank(bo.getPlateNumber()), HotVehicleCertReissue::getPlateNumber, bo.getPlateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getReissueType()), HotVehicleCertReissue::getReissueType, bo.getReissueType());
        lqw.eq(bo.getRegisterTime() != null, HotVehicleCertReissue::getRegisterTime, bo.getRegisterTime());
        lqw.eq(bo.getPickupTime() != null, HotVehicleCertReissue::getPickupTime, bo.getPickupTime());
        lqw.eq(bo.getIsDeleted() != null, HotVehicleCertReissue::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getOperationStatus() != null, HotVehicleCertReissue::getOperationStatus, bo.getOperationStatus());
        return lqw;
    }

    /**
     * 新增证件补领登记
     *
     * @param bo 证件补领登记
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotVehicleCertReissueBo bo) {
        HotVehicleCertReissue add = MapstructUtils.convert(bo, HotVehicleCertReissue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改证件补领登记
     *
     * @param bo 证件补领登记
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotVehicleCertReissueBo bo) {
        HotVehicleCertReissue update = MapstructUtils.convert(bo, HotVehicleCertReissue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotVehicleCertReissue entity) {
        if (entity.getRegisterTime() != null
                && entity.getPickupTime() != null
                && !entity.getPickupTime().after(entity.getRegisterTime())) {
            throw new ServiceException("领取时间必须大于登记时间");
        }
    }

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