package com.xintong.visualinspection.visuallnspection_fjq.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xintong.visualinspection.bean.FwqScore;
import com.xintong.visualinspection.bean.Item;
import com.xintong.visualinspection.service.CommonService;
import com.xintong.visualinspection.service.ItemService;
import com.xintong.visualinspection.service.ScoreService_F;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.visuallnspection_fjq.bean.Log;
import com.xintong.visualinspection.visuallnspection_fjq.dao.LogDao;
import com.xintong.visualinspection.visuallnspection_fjq.dao.ScoreDao;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 文件名：ScoreServiceImpl
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@com.alibaba.dubbo.config.annotation.Service(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
@Service
public class ScoreServiceImpl  implements ScoreService_F {

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


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

    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}")
    private ItemService itemService;

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

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

	@Override
	public List<FwqScore> getScoreList(FwqScore score) {
		// TODO Auto-generated method stub
		List<FwqScore> scoreList = scoreDao.getScoreList(score);
        List<Item> items = itemService.getAll();
        for (FwqScore s:scoreList
                ) {
            for (Item i:items
                 ) {
                if(s.getCheck_item_id().equals(i.getId())){
                    s.setCheck_item_name(i.getName());
                    s.setCheck_item_score(i.getScore());
                }

            }

        }
		return scoreList;
	}

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

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

	@Override
	public void update(FwqScore score) {
        FwqScore oldScore = getOne(score.getId());
		// TODO Auto-generated method stub
		scoreDao.update(score);

		//扣分不一致保存日志
		if(score.getCheck_item_score()!=null && !oldScore.getCheck_item_score().equals(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<FwqScore> getTopRemarkList(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return scoreDao.getTopRemarkList(param);
	}

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

	@Override
	public void backoutScore(FwqScore score) {
		// TODO Auto-generated method stub
        FwqScore 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);
	}
}
