package com.ssyf.module.custom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssyf.module.custom.domain.entity.CustomTable;
import com.ssyf.module.custom.domain.query.CustomTableQuery;
import com.ssyf.module.custom.service.CustomTableService;
import com.ssyf.module.custom.mapper.CustomTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author admin
* @description 针对表【custom_table(自定义表)】的数据库操作Service实现
* @createDate 2024-08-13 09:29:55
*/
@Service
public class CustomTableServiceImpl extends ServiceImpl<CustomTableMapper, CustomTable> implements CustomTableService{

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CustomTable addOne(CustomTableQuery query) {
        CustomTable customTable = BeanUtil.toBean(query , CustomTable.class);
        customTable.setUid(IdUtil.simpleUUID());
        save(customTable);
        return customTable;
    }

    @Override
    public void updateByUid(String uid, CustomTableQuery query) {
        CustomTable customTable = BeanUtil.toBean(query , CustomTable.class);
        customTable.setUid(uid);
        update(customTable ,new LambdaQueryWrapper<CustomTable>().eq(CustomTable::getUid ,uid));
    }

    @Override
    public void removeByUid(String uid) {
        remove(new LambdaQueryWrapper<CustomTable>().eq(CustomTable::getUid ,uid));
    }

    @Override
    public Object getByUid(String uid) {
        return getOne(new LambdaQueryWrapper<CustomTable>().eq(CustomTable::getUid ,uid));
    }

    @Override
    public PageInfo<CustomTable> getPage(CustomTableQuery query) {
        PageHelper.startPage(query.getPageNum() ,query.getPageSize());
        return PageInfo.of(getAll(query));
    }

    @Override
    public List<CustomTable> getAll(CustomTableQuery query) {
        LambdaQueryWrapper<CustomTable> queryWrapper = createWrapper(query);
        return list(queryWrapper);
    }

    private LambdaQueryWrapper<CustomTable> createWrapper(CustomTableQuery query) {
        LambdaQueryWrapper<CustomTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(query.getUid()),CustomTable::getUid ,query.getUid());
        queryWrapper.like(StrUtil.isNotBlank(query.getName()),CustomTable::getName ,query.getName());
        return queryWrapper;
    }
}




