package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.PersonScore;
import com.hssx.pcbms.entity.Score;
import com.hssx.pcbms.entity.ScoreSetting;
import com.hssx.pcbms.mapper.PersonScoreMapper;
import com.hssx.pcbms.mapper.ScoreMapper;
import com.hssx.pcbms.mapper.ScoreSettingMapper;
import com.hssx.pcbms.service.PersonScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-12-10
 */
@Service
public class PersonScoreServiceImpl extends ServiceImpl<PersonScoreMapper, PersonScore> implements PersonScoreService {
    @Resource
    private PersonScoreMapper personScoreMapper;
    @Resource
    private ScoreSettingMapper scoreSettingMapper;
    @Resource
    private ScoreMapper scoreMapper;
    @Override
    public HttpRespMsg addOrUpdate(PersonScore personScore) {
        HttpRespMsg msg = new HttpRespMsg();
        Integer personScoreInit = 0;
        if (personScore.getId() == null) {
            //添加操作
            personScoreMapper.insert(personScore);
            Score score = new Score();
            score.setScoreId(personScore.getScoreId());
            score.setPersonalScore(personScoreInit+personScore.getCan()+personScore.getDiligence()+personScore.getVirtue()+personScore.getPerformance());
            scoreMapper.update(score,new QueryWrapper<Score>().eq("score_id",personScore.getScoreId()));
        }else{
            personScoreMapper.updateById(personScore);
            Score score = new Score();
            score.setScoreId(personScore.getScoreId());
            score.setPersonalScore(personScoreInit+personScore.getCan()+personScore.getDiligence()+personScore.getVirtue()+personScore.getPerformance());
            scoreMapper.update(score,new QueryWrapper<Score>().eq("score_id",personScore.getScoreId()));
        }
        return msg;
    }

    @Override
    public HttpRespMsg fiveList() {
        HttpRespMsg msg = new HttpRespMsg();
        msg.data = scoreSettingMapper.selectList(new QueryWrapper<>());
        return msg;
    }
}
