package com.njzscloud.common.sn;

import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 递增编码表
 */
@Slf4j
public class SysIncSnService extends ServiceImpl<SysIncSnMapper, SysIncSnEntity> implements IService<SysIncSnEntity> {

    /**
     * 新增
     *
     * @param sysIncSnEntity 数据
     */
    public void add(SysIncSnEntity sysIncSnEntity) {
        this.save(sysIncSnEntity);
    }

    /**
     * 修改
     *
     * @param sysIncSnEntity 数据
     */
    public void modify(SysIncSnEntity sysIncSnEntity) {
        this.updateById(sysIncSnEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysIncCodeEntity 结果
     */
    public SysIncSnEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysIncSnEntity 筛选条件
     * @param pageParam      分页参数
     * @return PageResult&lt;SysIncCodeEntity&gt; 分页结果
     */
    public PageResult<SysIncSnEntity> paging(PageParam pageParam, SysIncSnEntity sysIncSnEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysIncSnEntity>query(sysIncSnEntity)));
    }

    @Transactional(rollbackFor = Exception.class)
    public Long inc(String code) {
        SysIncSnEntity incCodeEntity = this.getOne(Wrappers.<SysIncSnEntity>lambdaQuery()
                .eq(SysIncSnEntity::getCode, code));
        Long val = incCodeEntity.getVal();
        val += incCodeEntity.getStep();
        this.updateById(incCodeEntity.setVal(val));
        return val;
    }
}
