package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.illegalParking.IllegalParking;
import com.ruoyi.system.mapper.IllegalParkingMapper;
import com.ruoyi.system.service.IIllegalParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 违章停车登记Service业务层处理
 * 
 * @author boman
 * @date 2025-02-14
 */
@Service
public class IllegalParkingServiceImpl implements IIllegalParkingService 
{
    @Autowired
    private IllegalParkingMapper illegalParkingMapper;

    /**
     * 查询违章停车登记
     * 
     * @param illegalParkingId 违章停车登记主键
     * @return 违章停车登记
     */
    @Override
    public IllegalParking selectIllegalParkingByIllegalParkingId(Long illegalParkingId)
    {

        IllegalParking illegalParking = illegalParkingMapper.selectIllegalParkingByIllegalParkingId(illegalParkingId);
        String plateNumber = illegalParking.getPlateNumber();
        List<IllegalParking> illegalParkings = illegalParkingMapper.selectIllegalParkingByPlateNumber(plateNumber);
        if (illegalParkings != null && illegalParkings.size() > 0){
            illegalParking.setChildren(illegalParkings);
        }
        return illegalParking;
    }

    /**
     * 查询违章停车登记列表
     * 
     * @param illegalParking 违章停车登记
     * @return 违章停车登记
     */
    @Override
    public List<IllegalParking> selectIllegalParkingList(IllegalParking illegalParking)
    {
        List<IllegalParking> illegalParkings = illegalParkingMapper.selectIllegalParkingList(illegalParking);
        if (illegalParkings != null && illegalParkings.size() > 0){
            for (IllegalParking parking : illegalParkings) {
                String plateNumber = parking.getPlateNumber();
                List<IllegalParking> illegalParkingC = illegalParkingMapper.selectIllegalParkingByPlateNumber(plateNumber);
                if (illegalParkingC != null && illegalParkingC.size() > 0){
                    illegalParking.setChildren(illegalParkingC);
                }
            }
        }
        return illegalParkings;
    }

    /**
     * 新增违章停车登记
     * 
     * @param illegalParking 违章停车登记
     * @return 结果
     */
    @Override
    public int insertIllegalParking(IllegalParking illegalParking)
    {
        //todo 需要判断是否是小区车辆，关联房号，判断是否需要进入黑名单
        illegalParking.setCreateTime(DateUtils.getNowDate());
        return illegalParkingMapper.insertIllegalParking(illegalParking);
    }

    /**
     * 修改违章停车登记
     * 
     * @param illegalParking 违章停车登记
     * @return 结果
     */
    @Override
    public int updateIllegalParking(IllegalParking illegalParking)
    {
        illegalParking.setUpdateTime(DateUtils.getNowDate());
        return illegalParkingMapper.updateIllegalParking(illegalParking);
    }

    /**
     * 批量删除违章停车登记
     * 
     * @param illegalParkingIds 需要删除的违章停车登记主键
     * @return 结果
     */
    @Override
    public int deleteIllegalParkingByIllegalParkingIds(Long[] illegalParkingIds)
    {
        return illegalParkingMapper.deleteIllegalParkingByIllegalParkingIds(illegalParkingIds);
    }

    /**
     * 删除违章停车登记信息
     * 
     * @param illegalParkingId 违章停车登记主键
     * @return 结果
     */
    @Override
    public int deleteIllegalParkingByIllegalParkingId(Long illegalParkingId)
    {
        return illegalParkingMapper.deleteIllegalParkingByIllegalParkingId(illegalParkingId);
    }

    /**
     * 解除黑名单
     * @param illegalParking
     * @return
     */
    @Override
    public AjaxResult cancel(IllegalParking illegalParking) {
        illegalParking.setIsBlack("N");
        illegalParkingMapper.updateIllegalParking(illegalParking);
        //todo 给道闸下发解除黑名单
        return AjaxResult.success();
    }
}
