package com.muyu.server.service.impl;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarMessage;
import com.muyu.domain.CarMessageType;
import com.muyu.domain.CarType;
import com.muyu.domain.resp.CarMessageResp;
import com.muyu.server.mapper.CarMessageMapper;
import com.muyu.server.service.CarMessageService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * 报文模板展示列表业务实现层
 */
@Service
public class CarMessageServiceImpl
        extends ServiceImpl<CarMessageMapper,CarMessage>
        implements CarMessageService {
    @Resource
    private CarMessageMapper carMessageMapper;

    /**
     * 根据所属车类别 解析 车辆报文模板
     * @param
     * @return
     */
    @Override
    public List<CarMessage> selectCarMessageList() {

        return carMessageMapper.selectJoinList(
                CarMessage.class
                ,new MPJLambdaWrapper<CarMessage>()
                        .select(CarMessage::getCarMessageId
                                ,CarMessage::getCarMessageCarType
                                ,CarMessage::getCarMessageType
                                ,CarMessage::getCarMessageStartIndex
                                ,CarMessage::getCarMessageEndIndex
                                ,CarMessage::getMessageTypeClass
                                ,CarMessage::getCarMessageState)
                        .select(CarMessageType::getMessageTypeId
                                ,CarMessageType::getMessageTypeName
                                ,CarMessageType::getMessageTypeBelongs)
                        .select(CarType::getCarTypeId
                                ,CarType::getCarTypeName)
                        .leftJoin(CarMessageType.class,CarMessageType::getMessageTypeId,CarMessage::getCarMessageType)
                        .leftJoin(CarType.class,CarType::getCarTypeId,CarMessage::getCarMessageCarType));
    }
    /**
     * 添加车辆报文规则
     */
    @Override
    public boolean insertCarMessage(CarMessage carMessage) {
        return this.save(CarMessage.carMessageAddBuilder(carMessage));
    }
    /**
     * 删除车辆报文规则
     */
    @Override
    public boolean delectByCarMessageId(Integer carMessageId) {
        return this.removeById(carMessageId);

    }
    /**
     * 修改车辆报文规则
     */
    @Override
    public boolean updateCarMessage(CarMessage carMessage) {
//        Integer integer = carMessageMapper.updateCarMessage(carMessage);
        return this.updateById(CarMessage.carMessageUpdBuilder(carMessage, carMessage::getMessageTypeId));
    }
    /**
     * 查询
     * @param id
     * @return
     */
    @Override
    public List<CarMessageResp> selectJoinList(Long id) {
        return carMessageMapper.selectJoinList(CarMessageResp.class, new MPJLambdaWrapper<CarMessage>()
                .select(CarMessage::getCarMessageId,
                        CarMessage::getCarMessageCarType,
                        CarMessage::getCarMessageType,
                        CarMessage::getCarMessageStartIndex,
                        CarMessage::getCarMessageEndIndex,
                        CarMessage::getCarMessageState)
                .select(CarMessageType::getMessageTypeName)
                .leftJoin(CarMessageType.class, CarMessageType::getMessageTypeId, CarMessage::getCarMessageType)
                .eq(StringUtils.isNotNull(id),CarMessage::getCarMessageCarType, id));
    }


}
