/*
 * Copyright (c) 2021, orioc and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.bonus.sharedstation.web.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.web.domain.SharingTools;
import com.bonus.sharedstation.web.dto.SharingToolsDTO;
import com.bonus.sharedstation.web.mapper.SharingToolsMapper;
import com.bonus.sharedstation.web.service.ISharingToolsService;
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 SharingToolsServiceImpl extends ServiceImpl<SharingToolsMapper, SharingTools> implements ISharingToolsService {

    @Resource
    private SharingToolsMapper sharingToolsMapper;

    @Override
    public AjaxResult insert(SharingTools 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(SharingTools 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(SharingToolsDTO dto) {
        LambdaQueryWrapper<SharingTools> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<SharingTools> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(SharingTools::getSharingAddress, dto.getSharingAddress())
                .eq(SharingTools::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(SharingTools::getCreatedTime);

        Page<SharingTools> result = sharingToolsMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }
    @Override
    public AjaxResult getListPageApp(SharingToolsDTO  dto) {
        LambdaQueryWrapper<SharingTools> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<SharingTools> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(SharingTools::getSharingAddress, dto.getSharingAddress())
                .eq(SharingTools::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(SharingTools::getCreatedTime);

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

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

    @Override
    public AjaxResult selectAll(SharingTools sharingTools) {
        LambdaQueryWrapper<SharingTools> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .like(SharingTools::getSharingAddress, sharingTools.getSharingAddress())
                .eq(SharingTools::getIsDeleted, sharingTools.getIsDeleted())
                .orderByDesc(SharingTools::getCreatedTime);

        List<SharingTools> result = sharingToolsMapper.selectList(queryWrapper);
        return AjaxResult.success(result);
    }


    @Override
    public AjaxResult selectInfoById(SharingTools carService) {
        SharingTools result = sharingToolsMapper.selectById(carService.getId());
        if (StringUtils.isNull(result)) {
            return AjaxResult.error("此共享工具暂无具体内容！");
        }
        //处理参数问题
        return AjaxResult.success(result);
    }

}
