package com.yc.ship.module.resource.service.car;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.car.vo.*;
import com.yc.ship.module.resource.dal.dataobject.car.CarDO;
import com.yc.ship.module.resource.dal.dataobject.cartype.CarTypeDO;
import com.yc.ship.module.resource.dal.dataobject.company.CompanyDO;
import com.yc.ship.module.resource.dal.dataobject.driver.DriverDO;
import com.yc.ship.module.resource.dal.mysql.car.CarMapper;
import com.yc.ship.module.resource.dal.mysql.cartype.CarTypeMapper;
import com.yc.ship.module.resource.dal.mysql.company.CompanyMapper;
import com.yc.ship.module.resource.dal.mysql.driver.DriverMapper;
import com.yc.ship.module.resource.enums.CarStatusEnum;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 车辆管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CarServiceImpl implements CarService {

    @Resource
    private CarMapper carMapper;
    @Resource
    private CarTypeMapper carTypeMapper;
    @Resource
    private DriverMapper driverMapper;

    @Resource
    private CompanyMapper companyMapper;


    @Override
    public String createCar(CarSaveReqVO createReqVO) {
        // 插入
        CarDO car = BeanUtils.toBean(createReqVO, CarDO.class);
        car.setId(IdWorker.getIdStr(car));
        car.setDeleted(false);
        carMapper.insert(car);
        // 返回
        return car.getId();
    }

    @Override
    public void updateCar(CarSaveReqVO updateReqVO) {
        // 校验存在
        validateCarExists(updateReqVO.getId());
        // 更新
        CarDO updateObj = BeanUtils.toBean(updateReqVO, CarDO.class);
        carMapper.updateById(updateObj);
    }

    @Override
    public void deleteCar(String id) {
        // 校验存在
        validateCarExists(id);
        // 删除
        carMapper.deleteById(id);
    }

    private void validateCarExists(String id) {
        if (carMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.CAR_NOT_EXISTS);
        }
    }

    @Override
    public CarDO getCar(String id) {
        return carMapper.selectById(id);
    }

    @Override
    public PageResult<CarDO> getCarPage(CarPageReqVO pageReqVO) {
        return carMapper.selectPage(pageReqVO);
    }

    @Override
    public void supplyInfo(CarListRespVO respVO) {
        if (respVO != null) {
            // 车辆类型
            if (StrUtil.isNotBlank(respVO.getCarTypeId())) {
                CarTypeDO carDO = carTypeMapper.selectById(respVO.getCarTypeId());
                if (carDO != null) {
                    respVO.setCarTypeName(carDO.getName());
                }
            }
            // 驾驶员信息
            if (StrUtil.isNotBlank(respVO.getDriverId())) {
                DriverDO driverDO = driverMapper.selectById(respVO.getDriverId());
                if (driverDO != null) {
                    respVO.setDriverName(driverDO.getName());
                    respVO.setPhone(driverDO.getPhone());
                }
            }
            // 所属供应商信息
            if (StrUtil.isNotBlank(respVO.getCompanyId())) {
                CompanyDO cdo = companyMapper.selectById(respVO.getCompanyId());
                if (cdo != null) {
                    respVO.setCompanyName(StringUtils.isEmpty(cdo.getShortName()) ? cdo.getCompanyName() : cdo.getShortName());
                }
            }

        }
    }

    @Override
    public List<CarDO> getSimpleCarList(CarPageReqVO pageReqVO) {
        return carMapper.selectSimpleList(pageReqVO);
    }


    @Override
    public List<CarDO> getCarList() {
        return this.getCarList(new CarReqVO());
    }
    @Override
    public List<CarDO> getCarList(CarReqVO carReqVO) {
        if (carReqVO.getCarStatus() == null) {
            carReqVO.setCarStatus(CarStatusEnum.ENABLE.getStatus());
        }
        return carMapper.getCarList(carReqVO);
    }


    @Override
    public List<CarTypeNumRespVO> getstatiscisCarTypeNum(){
        return carMapper.getstatiscisCarTypeNum();
    };


}