package com.njzscloud.supervisory.money.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.money.mapper.MoneyChangeDetailMapper;
import com.njzscloud.supervisory.money.pojo.entity.MoneyChangeDetailEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资金变动明细
 */
@Slf4j
@Service
public class MoneyChangeDetailService extends ServiceImpl<MoneyChangeDetailMapper, MoneyChangeDetailEntity> implements IService<MoneyChangeDetailEntity> {

    /**
     * 新增
     */
    public void add(MoneyChangeDetailEntity moneyChangeDetailEntity) {
        moneyChangeDetailEntity.setUserId(SecurityUtil.currentUserId());
        moneyChangeDetailEntity.setNewMoney(moneyChangeDetailEntity.getOldMoney().add(moneyChangeDetailEntity.getDelta()));
        this.save(moneyChangeDetailEntity);
    }

    /**
     * 修改
     */
    public void modify(MoneyChangeDetailEntity moneyChangeDetailEntity) {
        this.updateById(moneyChangeDetailEntity);
    }

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

    /**
     * 详情
     */
    public MoneyChangeDetailEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<MoneyChangeDetailEntity> paging(PageParam pageParam, MoneyChangeDetailEntity moneyChangeDetailEntity) {
        Page<MoneyChangeDetailEntity> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        IPage<MoneyChangeDetailEntity> result = baseMapper.page(page, moneyChangeDetailEntity);
        return PageResult.of(result);
    }

}
