/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.car.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.UserClent;
import com.tofly.gpsboot.mapper.car.CarMapper;
import com.tofly.gpsboot.service.car.CarService;
import com.tofly.gpsboot.service.car.CarUserService;
import com.tofly.pojo.User;
import com.tofly.pojo.car.CarUser;
import com.tofly.pojo.car.vo.CarTrackVo;
import com.tofly.pojo.maintain.dto.CarTrackDto;
import com.tofly.pojo.maintain.vo.UserViewVo;
import com.tofly.pojo.plan.Car;
import com.tofly.pojo.plan.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author admin
 * @date Mon May 09 00:00:00 CST 2022
 */
@Service("carService")
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private CarUserService carGpsService;
    @Autowired
    private CarUserService carUserService;
    @Autowired
    private UserClent userClent;
    @Override
    public Car updateWithCondition(Car car) {
        Car old = this.baseMapper.selectById(car.getId());
        if(!old.getCarNo().equals(car.getCarNo())){
            int count = carGpsService.count(Wrappers.<CarUser>lambdaQuery()
                    .eq(CarUser::getCarId, car.getId()));
            Assert.isTrue(count>0,"该车辆存在绑定记录,不支持修改");
        }
        baseMapper.updateById(car);
        return  car;
    }

    @Override
    public Page<CarTrackVo> getCarTrackPage(Page page, CarTrackDto carTrackDto) {
        return baseMapper.getCarTrackPage(page, carTrackDto);
    }

    @Override
    public CarVo entityToVo(Car car) {
        CarVo carVo = JSONObject.parseObject(JSONObject.toJSONString(car),CarVo.class);
        List<CarUser> carUserList =
                carUserService.list(Wrappers.<CarUser>lambdaQuery().
                        eq(CarUser::getCarId, car.getId()));
        List<Long> userIds = carUserList.stream().map(CarUser::getUserId).collect(Collectors.toList());
        List<UserViewVo>  carUsers=new ArrayList<>();
        userIds.forEach(userId->{
            User result = userClent.getById(userId).getResult();
            UserViewVo userViewVo = new UserViewVo();
            userViewVo.setWorknumber(result.getWorknumber());
            userViewVo.setPhone(result.getPhone());
            userViewVo.setId(result.getId());
            userViewVo.setUserName(result.getRealName());
            carUsers.add(userViewVo);
        });
        if(carUsers!=null &&carUsers.size()>0){
            carVo.setIsUsed(Boolean.TRUE);
        }
        carVo.setCarUser(carUsers);

            try {
                String realName = userClent.getById(car.getChargePerson()).getResult().getRealName();
                carVo.setChargePersonName(realName);
            }catch (Exception e){

            }
        return carVo;
    }
}
