/*
 * 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.CarService;
import com.bonus.sharedstation.app.dto.CarServiceDTO;
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 hyq
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class CarServiceServiceImpl extends ServiceImpl<CarServiceMapper, CarService> implements ICarServiceService {


    /**
     * Dao
     */
    @Resource
    private CarServiceMapper carServiceMapper;

    @Override
    @Transactional(readOnly = false)
    public AjaxResult insert(CarService 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
    @Transactional(readOnly = false)
    public AjaxResult updateInfoById(CarService dto) {
        log.info("修改 入参: {}", dto);
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = updateById(dto);
        if (!b) {
            return AjaxResult.error("汽车服务修改失败");
        }
        return AjaxResult.success( "汽车服务修改成功");
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPage(CarServiceDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<CarService> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<CarService> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(CarService::getServiceType, dto.getServiceType())
                .eq(CarService::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CarService::getCreatedTime);

        Page<CarService> result = carServiceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPageApp(CarServiceDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<CarService> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<CarService> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(CarService::getServiceType, dto.getServiceType())
                .eq(CarService::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CarService::getCreatedTime);

        Page<CarService> result = carServiceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    @Transactional(readOnly = false)
    public AjaxResult delete(CarService carService) {
        return AjaxResult.success(carServiceMapper.deleteById(carService.getId()));
    }

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

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


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