package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.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.CarFence;
import com.muyu.domain.CarInformation;
import com.muyu.domain.CarType;
import com.muyu.domain.req.CarInformationAddReq;
import com.muyu.domain.req.CarInformationListReq;
import com.muyu.domain.req.CarInformationUpdReq;
import com.muyu.domain.resp.CarInformationResp;
import com.muyu.server.mapper.CarInformationMapper;
import com.muyu.server.service.CarInformationService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆信息管理业务实现层
 * @author 17353
 */
@Service
public class CarInformationServiceImpl
        extends ServiceImpl<CarInformationMapper, CarInformation>
        implements CarInformationService {
    @Resource
    private CarInformationMapper carInformationMapper;
    @Autowired
    private HttpServletResponse response;


    /**
     * 企业车辆管理列表
     * 联查--> 车辆管理--车辆类型表--车辆电子围栏
     * 搜索-->(车辆唯一VIN carInformationVIN 精确查
     *        车辆类型ID  carTypeId  精确查
     *        车辆电子围栏外键ID carInformationFence 精确查
     *        启用状态(1.在线 2.离线 3.已断开 4.待连接 5.维修中)
     *        carInformationState  精确查
     *        车辆电机厂商 carInformationMotorManufacturer 模糊查
     *        车辆电机型号 carInformationMotorModel 精确查
     *        车辆电池厂商 carInformationBatteryManufacturer 模糊查
     *        车辆电池型号 carInformationBatteryModel 精确查
     *        )
     *        分页-->  分页页数 pageNum  分页条数 pageSize
     * @param carInformationListReq
     * @return
     */
    @Override
    public Page<CarInformationResp> selectCarInformationList(CarInformationListReq carInformationListReq) {
        return carInformationMapper.selectJoinPage(
                new Page<>(carInformationListReq.getPageNum(), carInformationListReq.getPageSize()),
                CarInformationResp.class,
                new MPJLambdaWrapper<CarInformation>()
                        .selectAll(CarInformation.class)
                        .select(CarType::getCarTypeId, CarType::getCarTypeName)
                        .select(CarFence::getId, CarFence::getName)
                        .leftJoin(CarFence.class, CarFence::getId, CarInformation::getCarInformationFence)
                        .leftJoin(CarType.class, CarType::getCarTypeId, CarInformation::getCarInformationType)

                        .eq(StringUtils.isNotEmpty(carInformationListReq.getCarInformationVIN())
                                ,CarInformation::getCarInformationVIN
                                , carInformationListReq.getCarInformationVIN())
                        .eq(StringUtils.isNotNull(carInformationListReq.getCarTypeId())
                                ,CarType::getCarTypeId, carInformationListReq.getCarTypeId())
                        .eq(StringUtils.isNotNull(carInformationListReq.getCarInformationFence())
                                ,CarInformation::getCarInformationFence
                                , carInformationListReq.getCarInformationFence())
                        .eq(StringUtils.isNotEmpty(carInformationListReq.getCarInformationVIN())
                                ,CarInformation::getCarInformationState
                                , carInformationListReq.getCarInformationState())
                        .like(StringUtils.isNotEmpty(carInformationListReq.getCarInformationVIN())
                                ,CarInformation::getCarInformationVIN
                                , carInformationListReq.getCarInformationVIN())
                        .eq(StringUtils.isNotEmpty( carInformationListReq.getCarInformationMotorModel())
                                ,CarInformation::getCarInformationMotorModel
                                ,carInformationListReq.getCarInformationMotorModel())
                        .like(StringUtils.isNotEmpty(carInformationListReq.getCarInformationBatteryManufacturer())
                                ,CarInformation::getCarInformationBatteryManufacturer
                                ,carInformationListReq.getCarInformationBatteryManufacturer())
                        .eq(StringUtils.isNotEmpty( carInformationListReq.getCarInformationBatteryModel())
                                ,CarInformation::getCarInformationBatteryModel
                                ,carInformationListReq.getCarInformationBatteryModel())
        );
    }

    /**
     * 企业车辆添加管理
     * @param carInformationAddReq
     * @return
     */
    @Override
    public boolean addCarInformation(CarInformationAddReq carInformationAddReq) {
        boolean addCarInformation = this.save(CarInformation.carInformationAddBuilder(carInformationAddReq));

        return addCarInformation;
    }

    /**
     * 企业车辆删除
     * @param carInformationId
     * @return
     */
    @Override
    public boolean delBycarInformationId(Integer carInformationId) {
        return this.removeById(carInformationId);
    }


    /**
     * 企业车辆修改管理
     * @param carInformationUpdReq
     * @return
     */
    @Override
    public boolean updatecarInformation(CarInformationUpdReq carInformationUpdReq) {
        Long carInformationId = carInformationUpdReq.getCarInformationId();
        boolean updatecarInformation = this.updateById(
                CarInformation.carInformationUpdBuilder(
                        carInformationUpdReq, ()->carInformationId));
        return updatecarInformation;
    }

    /**
     * To 电子围栏负责人
     * 查询企业车辆 carInformationID 和  carInformationLicensePlate
     * 无参
     * @return
     */
    @Override
    public List<CarInformationResp> selectBycarInformationIDAndLicensePlate() {
        List<CarInformation> list = this.list()
                .stream()
                .map(CarInformation::carInformationBuilder)
                .toList();
        ArrayList<CarInformationResp> carInformationResps = new ArrayList<>();
        CarInformationResp carInformation1 = new CarInformationResp();
        list.forEach(carInformation -> {
            carInformation1.setCarInformationId(carInformation.getCarInformationId());
            carInformation1.setCarInformationLicensePlate( carInformation.getCarInformationLicensePlate());
            carInformationResps.add(carInformation1);
        });
        return carInformationResps;
    }

    @Override
    public List<CarInformation> selectCarInformation(Long faulttypeId) {
        List<CarInformation> list = this.list(new LambdaQueryWrapper<CarInformation>()
                        .eq(CarInformation::getCarInformationType, faulttypeId))
                .stream()
                .map(CarInformation::carInformationListBuilder)
                .toList();
        return list;
    }

}
