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

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.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.resource.controller.admin.cartype.vo.CarTypePageReqVO;
import com.yc.ship.module.resource.controller.admin.cartype.vo.CarTypeReqVO;
import com.yc.ship.module.resource.controller.admin.cartype.vo.CarTypeSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.cartype.CarTypeDO;
import com.yc.ship.module.resource.dal.mysql.cartype.CarTypeMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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


/**
 * 车辆类型管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CarTypeServiceImpl implements CarTypeService {

    @Resource
    private CarTypeMapper carTypeMapper;

    @Override
    public String createCarType(CarTypeSaveReqVO createReqVO) {
        // 插入
        CarTypeDO carType = BeanUtils.toBean(createReqVO, CarTypeDO.class);
        carType.setId(IdWorker.getIdStr(carType));
        carType.setDeleted(false);
        carTypeMapper.insert(carType);
        // 返回
        return carType.getId();
    }

    @Override
    public void updateCarType(CarTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateCarTypeExists(updateReqVO.getId());
        // 更新
        CarTypeDO updateObj = BeanUtils.toBean(updateReqVO, CarTypeDO.class);
        carTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCarType(String id) {
        // 校验存在
        validateCarTypeExists(id);
        // 删除
        carTypeMapper.deleteById(id);
    }

    private void validateCarTypeExists(String id) {
        if (carTypeMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.CAR_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public CarTypeDO getCarType(String id) {
        return carTypeMapper.selectById(id);
    }

    @Override
    public PageResult<CarTypeDO> getCarTypePage(CarTypePageReqVO pageReqVO) {
        return carTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CarTypeDO> getCarTypeList(CarTypeReqVO carTypeReqVO) {
        List<CarTypeDO> list = carTypeMapper.selectList(carTypeReqVO);
        return list;
    }

    @Override
    public List<CarTypeDO> getCarTypeList(List<String> idList) {
        return carTypeMapper.selectList(new LambdaQueryWrapperX<CarTypeDO>().in(CarTypeDO::getId, idList));
    }

    @Override
    public Map<String, String> getNameMap() {
        Map<String, CarTypeDO> CarTypeDOMap = carTypeMapper.selectNameMap(null);
        return CarTypeDOMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getName()));
    }

}