package com.cmvr.ti.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.core.domain.entity.SysDictData;
import com.cmvr.system.service.ISysDictDataService;
import com.cmvr.system.service.ISysDictTypeService;
import com.cmvr.ti.mapper.TiVehicleFunctionMapper;
import com.cmvr.ti.model.domain.TiVehicleFunction;
import com.cmvr.ti.model.vo.TiVehicleFunctionVO;
import com.cmvr.ti.service.ITiVehicleFunctionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 车机功能Service业务层处理
 */
@Service
@RequiredArgsConstructor
public class TiVehicleFunctionServiceImpl extends ServiceImpl<TiVehicleFunctionMapper, TiVehicleFunction>
        implements ITiVehicleFunctionService {

    private final ISysDictDataService sysDictDataService;
    @Override
    public TiVehicleFunction selectTiVehicleFunctionById(Long id) {


        return this.getById(id);
    }

    @Override
    public JSONObject selectTiVehicleFunctionDetailById(Long id) {
        JSONObject jsonObject = JSONObject.from(this.getById(id));
        jsonObject.put("funcName", sysDictDataService.selectDictLabel("ti_function_config", jsonObject.getString("funcKey")));
        return jsonObject;
    }

    @Override
    public List<TiVehicleFunction> queryByIds(List<Long> ids) {
        return this.listByIds(ids);
    }

    @Override
    public List<TiVehicleFunction> selectTiVehicleFunctionList(TiVehicleFunctionVO tiVehicleFunctionVO) {
        LambdaQueryWrapper<TiVehicleFunction> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TiVehicleFunction::getVehicleConfigId, tiVehicleFunctionVO.getVehicleConfigId())
                .in(CollUtil.isNotEmpty(tiVehicleFunctionVO.getFuncIds()),TiVehicleFunction::getId, tiVehicleFunctionVO.getFuncIds())
                .like(StrUtil.isNotEmpty(tiVehicleFunctionVO.getFuncKey()), TiVehicleFunction::getFuncKey, tiVehicleFunctionVO.getFuncKey())
                .like(StrUtil.isNotEmpty(tiVehicleFunctionVO.getRemark()), TiVehicleFunction::getRemark, tiVehicleFunctionVO.getRemark());
        return this.list(wrapper);
    }

    @Override
    public int insertTiVehicleFunction(TiVehicleFunction TiVehicleFunction) {
        return this.baseMapper.insert(TiVehicleFunction);
    }

    @Override
    public int updateTiVehicleFunction(TiVehicleFunction TiVehicleFunction) {
        return this.baseMapper.updateById(TiVehicleFunction);
    }

    @Override
    public int deleteTiVehicleFunctionByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
