package com.xintong.visualinspection.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.Log;
import com.xintong.visualinspection.bean.Score;
import com.xintong.visualinspection.dao.master.LogDao;
import com.xintong.visualinspection.dao.master.ScoreDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.ScoreService;
import com.xintong.visualinspection.util.CacheUtil;

/**
 * 文件名：ScoreServiceImpl
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@Service
public class ScoreServiceImpl extends BaseService implements ScoreService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ScoreServiceImpl.class);


    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private LogDao logDao;

	@Override
	public List<Score> getAll() {
		// TODO Auto-generated method stub
		return scoreDao.getAll();
	}

	@Override
	public Score getOne(Long id) {
		// TODO Auto-generated method stub
		return scoreDao.getOne(id);
	}

	@Override
	public List<Score> getScoreList(Score score) {
		// TODO Auto-generated method stub
		List<Score> scoreList = scoreDao.getScoreList(score);
		for(Score s:scoreList) {
			s.setChecked_person_name(CacheUtil.getUserTurenameFromMap(s.getChecked_person()));
		}
		return scoreList;
	}

	@Override
	public List<Score> getScoreListByTaskId(Long task_id) {
		// TODO Auto-generated method stub
		return scoreDao.getScoreListByTaskId(task_id);
	}

	@Override
	public void insert(Score score) {
		// TODO Auto-generated method stub
		score.setUpdate_time(new Date());
		scoreDao.insert(score);
	}

	@Override
	public void update(Score score) {
		Score oldScore = getOne(score.getId());
		// TODO Auto-generated method stub
		scoreDao.update(score);
		
		//扣分人员不一致保存日志
		if(score.getChecked_person()!=null && (!oldScore.getChecked_person().equals(score.getChecked_person()))){
			Log log = new Log();
			log.setTask_id(oldScore.getTask_id());
			log.setUpdate_user(score.getUpdate_user());
			log.setDept_id(oldScore.getChecked_dept());
			log.setScore_id(score.getId());
			log.setContent("更改扣分人员："+CacheUtil.getUserTurenameFromMap(oldScore.getChecked_person())+"-->"+CacheUtil.getUserTurenameFromMap(score.getChecked_person()));
			logDao.insert(log);
		}
		//扣分不一致保存日志
		if(score.getCheck_item_score()!=null && oldScore.getCheck_item_score()!=score.getCheck_item_score()){
			Log log = new Log();
			log.setTask_id(oldScore.getTask_id());
			log.setUpdate_user(score.getUpdate_user());
			log.setDept_id(oldScore.getChecked_dept());
			log.setScore_id(score.getId());
			log.setContent("更改扣分："+oldScore.getCheck_item_score()+"-->"+score.getCheck_item_score());
			logDao.insert(log);
		}
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		scoreDao.delete(id);
	}

	@Override
	public List<Score> getTopRemarkList(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return scoreDao.getTopRemarkList(param);
	}

	@Override
	public void updateByTaskIdAndPerson(Score score) {
		// TODO Auto-generated method stub
		scoreDao.updateByTaskIdAndPerson(score);
	}

	@Override
	public void backoutScore(Score score) {
		// TODO Auto-generated method stub
		Score oldScore = getOne(score.getId());
		//记录之前扣分
		scoreDao.updateFormerScore(score);
		//之前扣分撤销
		scoreDao.updateNowScore(score);
		
		//保存日志
		Log log = new Log();
		log.setTask_id(oldScore.getTask_id());
		log.setUpdate_user(score.getUpdate_user());
		log.setDept_id(oldScore.getChecked_dept());
		log.setScore_id(score.getId());
		log.setContent("撤销扣分："+oldScore.getCheck_item_score()+"-->"+score.getCheck_item_score());
		logDao.insert(log);
	}
}
