package com.dcsoft.system.car.service.impl;

import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.api.model.LoginUser;
import com.dcsoft.system.car.domain.TbCar;
import com.dcsoft.system.car.mapper.TbCarMapper;
import com.dcsoft.system.car.service.ISysPmsService;
import com.dcsoft.system.car.service.ITbCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆信息Service业务层处理
 *
 * @author nichun
 * @date 2023-07-24
 */
@Service
public class TbCarServiceImpl implements ITbCarService
{
    @Autowired
    private TbCarMapper tbCarMapper;

    @Autowired
    private ISysPmsService pmsService;

    /**
     * 查询车辆信息
     *
     * @param id 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public TbCar selectTbCarById(Long id)
    {
        return tbCarMapper.selectTbCarById(id);
    }

    /**
     * 查询车辆信息列表
     *
     * @param tbCar 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<TbCar> selectTbCarList(TbCar tbCar)
    {
        return tbCarMapper.selectTbCarList(tbCar);
    }

    /**
     * 查询我的车辆信息列表
     *
     * @param tbCar 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<TbCar> selectAppTbCarList(TbCar tbCar)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbCar.setUserId(loginUser.getUserid());
        return tbCarMapper.selectTbCarList(tbCar);
    }

    /**
     * 新增车辆信息
     *
     * @param tbCar 车辆信息
     * @return 结果
     */
    @Override
    public int insertTbCar(TbCar tbCar)
    {
        //白名单车辆新增或修改
//        String reponse = pmsService.addInnerVehicle(tbCar);
//        JSONObject json= JSONObject.parseObject(reponse);
//        if("1".equals(json.get("result")+"")) {
//            tbCar.setSync("1");
//        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbCar.setUserId(loginUser.getUserid());
        return tbCarMapper.insertTbCar(tbCar);
    }

    /**
     * 修改车辆信息
     *
     * @param tbCar 车辆信息
     * @return 结果
     */
    @Override
    public int updateTbCar(TbCar tbCar)
    {
        //白名单车辆新增或修改
//        String reponse=pmsService.addInnerVehicle(tbCar);
//        JSONObject json= JSONObject.parseObject(reponse);
//        if("1".equals(json.get("result")+"")) {
//            tbCar.setSync("1");
//        }
        return tbCarMapper.updateTbCar(tbCar);
    }

    /**
     * 批量删除车辆信息
     *
     * @param ids 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTbCarByIds(Long[] ids)
    {
//        for(Long id:ids){
//            TbCar car=tbCarMapper.selectTbCarById(id);
//            String reponse=pmsService.delInnerVehicle(car);
//        }
        return tbCarMapper.deleteTbCarByIds(ids);
    }

    /**
     * 删除车辆信息信息
     *
     * @param id 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTbCarById(Long id)
    {
        //根据车牌删除白名单车辆
        TbCar car=tbCarMapper.selectTbCarById(id);
        String reponse=pmsService.delInnerVehicle(car);
        return tbCarMapper.deleteTbCarById(id);
    }
}
