package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.RankMapper;
import com.qxgmat.data.dao.entity.Rank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class PayService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(PayService.class);

    @Resource
    private RankMapper rankMapper;

    public List<Rank> all(){
        return select(rankMapper);
    }

    /**
     * 计算考分
     * @param totalScore
     * @param quantScore
     * @return
     */
    public Rank compute(Number totalScore, Number quantScore){
        Example example = new Example(Rank.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("totalScore", totalScore)
                        .andEqualTo("quantScore", quantScore)
        );

        return one(rankMapper, example);
    }

    public Rank add(Rank rank){
        int result = insert(rankMapper, rank);
        rank = one(rankMapper, rank.getId());
        if(rank == null){
            throw new SystemException("添加失败");
        }
        return rank;
    }

    public Rank edit(Rank rank){
        Rank in = one(rankMapper, rank.getId());
        if(in == null){
            throw new ParameterException("排名不存在");
        }
        int result = update(rankMapper, rank);
        return rank;
    }

    public boolean delete(Number id){
        Rank in = one(rankMapper, id);
        if(in == null){
            throw new ParameterException("排名不存在");
        }
        int result = delete(rankMapper, id);
        return result > 0;
    }

    public Rank get(Number id){
        Rank in = one(rankMapper, id);

        if(in == null){
            throw new ParameterException("排名不存在");
        }
        return in;
    }

    public Page<Rank> select(int page, int pageSize){
        return select(rankMapper, page, pageSize);
    }

    public List<Rank> select(Collection ids){
        return select(rankMapper, ids);
    }

}
