/*
 * Copyright (c) 2021, orioc and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.bonus.sharedstation.app.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.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.sharedstation.app.domain.CarServiceDTO;
import com.bonus.sharedstation.app.dto.CarPageDTO;
import com.bonus.sharedstation.app.service.ICarServiceService;
import com.bonus.sharedstation.web.mapper.CarServiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Service实现
 *
 * @author 云归
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class CarServiceServiceImpl extends ServiceImpl<CarServiceMapper, CarServiceDTO> implements ICarServiceService {

    @Resource
    private CarServiceMapper carServiceMapper;

    @Override
    public AjaxResult insert(CarServiceDTO dto) {
        log.info("新增汽车服务:{} ", dto);
        //得到登录人
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = save(dto);
        if (!b) {
            return AjaxResult.error("新增汽车服务失败");
        }
        return AjaxResult.success("新增汽车服务成功");
    }

    @Override
    public AjaxResult updateInfoById(CarServiceDTO dto) {
        log.info("修改 入参: {}", dto);
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = updateById(dto);
        if (!b) {
            return AjaxResult.error("汽车服务修改失败");
        }
        return AjaxResult.success( "汽车服务修改成功");
    }

    @Override
    public AjaxResult getListPage(CarPageDTO dto) {
        // 创建分页对象
        Page<CarServiceDTO> result = getCarServicePage(dto);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getListPageApp(CarPageDTO dto) {
        // 创建分页对象
        Page<CarServiceDTO> result = getCarServicePage(dto);
        //处理参数问题
        return AjaxResult.success(result);
    }

    private Page<CarServiceDTO> getCarServicePage(CarPageDTO dto) {
        Page<CarServiceDTO> page = new Page<>(dto.getCurrentPage(), dto.getLimit());

        return carServiceMapper.selectPage(page, new LambdaQueryWrapper<CarServiceDTO>()
                .like(CarServiceDTO::getServiceType, dto.getServiceType())
                .eq(CarServiceDTO::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CarServiceDTO::getCreatedTime));
    }

    @Override
    public AjaxResult delete(CarServiceDTO carService) {
        return AjaxResult.success(carServiceMapper.deleteById(carService.getId()));
    }

    @Override
    public AjaxResult selectAll(CarServiceDTO carService) {
        log.info("查询 入参: {}", carService);
        LambdaQueryWrapper<CarServiceDTO> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .like(CarServiceDTO::getProviderName, carService.getProviderName())
                .eq(CarServiceDTO::getIsDeleted, carService.getIsDeleted())
                .orderByDesc(CarServiceDTO::getCreatedTime);

        List<CarServiceDTO> result = carServiceMapper.selectList(queryWrapper);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult selectInfoById(CarServiceDTO carService) {
        CarServiceDTO result = carServiceMapper.selectById(carService.getId());
        if (StringUtils.isNull(result)) {
            return AjaxResult.error( "此汽车服务不存在");
        }
        //处理参数问题
        return AjaxResult.success(result);
    }
}
