package com.sundot.airport.ledger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.ledger.domain.ScoreEvent;
import com.sundot.airport.ledger.mapper.ScoreEventMapper;
import com.sundot.airport.ledger.service.IScoreEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ScoreEventServiceImpl
        extends ServiceImpl<ScoreEventMapper, ScoreEvent>
        implements IScoreEventService {

    @Autowired
    private ScoreEventMapper mapper;

    @Override
    public List<ScoreEvent> selectList(ScoreEvent query) {
        return mapper.selectList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<ScoreEvent> list) {
        list.forEach(item -> {
            item.setCreateTime(DateUtils.getNowDate());
            // 自动计算 totalScore
            BigDecimal score = item.getScoreValue() != null ? item.getScoreValue() : BigDecimal.ZERO;
            BigDecimal cascade = item.getCascadeScore() != null ? item.getCascadeScore() : BigDecimal.ZERO;
            item.setTotalScore(score.add(cascade));
        });
        saveBatch(list);
        return list.size();
    }
}
