package com.xiaoniu.system.platformusers.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.resdata.RowsData;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.vo.LabelAndValueVO;
import com.xiaoniu.system.platformsettings.carbrand.domain.TbCarBrand;
import com.xiaoniu.system.platformsettings.carbrand.mapper.TbCarBrandMapper;
import com.xiaoniu.system.platformusers.vehicle.domain.TbVehicleInfo;
import com.xiaoniu.system.platformusers.vehicle.dto.UserCarQueryDTO;
import com.xiaoniu.system.platformusers.vehicle.mapper.TbVehicleInfoMapper;
import com.xiaoniu.system.platformusers.vehicle.service.ITbVehicleInfoService;
import com.xiaoniu.system.platformusers.vehicle.vo.UserCarListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 我的车辆信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-15 10:21:02
 */
@Service
public class TbVehicleInfoServiceImpl extends ServiceImpl<TbVehicleInfoMapper, TbVehicleInfo> implements ITbVehicleInfoService {

    @Resource
    private TbCarBrandMapper tbCarBrandMapper;

    @Override
    public RowsData<UserCarListVO> getUserCarList(UserCarQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            if (StringUtils.isNotEmpty(dto.getSortType())) {
                if ("1".equals(dto.getSortType())) {
                    dto.setSortType("ASC");
                } else if ("2".equals(dto.getSortType())) {
                    dto.setSortType("DESC");
                } else {
                    dto.setSortType("DESC");
                }
            }
            List<UserCarListVO> list = baseMapper.getUserCarList(dto);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public List<LabelAndValueVO> getCarBrandList(Long carBrandId) {
        LambdaQueryWrapper<TbCarBrand> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNull(carBrandId)) {
            wrapper.eq(TbCarBrand::getParentId, 0L);
        } else {
            wrapper.eq(TbCarBrand::getParentId, carBrandId);
        }
        wrapper.eq(TbCarBrand::getDelFlag, Constants.ZERO);
        wrapper.orderByAsc(TbCarBrand::getName);
        List<TbCarBrand> carBrandList = tbCarBrandMapper.selectList(wrapper);
        return carBrandList.stream().map(car -> {
            LabelAndValueVO labelAndValueVO = new LabelAndValueVO();
            labelAndValueVO.setLabel(car.getName());
            labelAndValueVO.setValue(String.valueOf(car.getId()));
            return labelAndValueVO;
        }).toList();
    }

}
