package com.xintong.visualinspection.service.impl;

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

import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.pojo.TaskListByDeptAppeal;
import com.xintong.visualinspection.util.AuthorUtil;
import com.xintong.visualinspection.util.Constants;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import com.xintong.visualinspection.dao.master.LogDao;
import com.xintong.visualinspection.dao.master.TaskDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.TaskService;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.util.CodeUtil;

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

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private LogDao logDao;
	@Override
	public List<Task> getAll() {
		// TODO Auto-generated method stub
		return taskDao.getAll();
	}

	@Override
	public Task getById(Long id) {
		// TODO Auto-generated method stub
		Task t = taskDao.getOne(id);
		if(t.getCheckman() != null){
			t.setCheckman_name(CacheUtil.getUserTurenameFromMap(t.getCheckman()));
		}
		if(t.getChecked_person() != null){
			t.setChecked_person_name(CacheUtil.getUserTurenameFromMap(t.getChecked_person()));
		}
		if(t.getChecked_dept() != null){
			if(CacheUtil.deptMap.get(t.getChecked_dept())!=null){
				t.setChecked_dept_name(CacheUtil.getOrgannameFromMap(t.getChecked_dept()));
			}
		}
		t.setRecheckman_name(CacheUtil.getUserTurenameFromMap(t.getRecheckman()));
		t.setMonitor_user_name(CacheUtil.getUserTurenameFromMap(t.getMonitor_user_id()));
		return t;
	}


	@Override
	public List<Task> getTaskList(Task task) {
		// TODO Auto-generated method stub
		List<Task> taskList;
		if(task.getStatus_search_type()!=null && task.getStatus_search_type()==1){
			taskList = taskDao.getListByChecked(task);
		}else if(task.getStatus_search_type()!=null && task.getStatus_search_type()==2){
			taskList = taskDao.getListBySubmited(task);
		}else if(task.getCheck_status_arr()!=null){
			taskList = taskDao.getListInStatus(task);
		}else{
			taskList = taskDao.getList(task);
		}
		for(Task t : taskList) {
			if(t.getCheckman() != null){
				t.setCheckman_name(CacheUtil.getUserTurenameFromMap(t.getCheckman()));
			}
			if(t.getChecked_person() != null){
				t.setChecked_person_name(CacheUtil.getUserTurenameFromMap(t.getChecked_person()));
			}
			if(t.getChecked_dept() != null){
				if(CacheUtil.deptMap.get(t.getChecked_dept())!=null){
					t.setChecked_dept_name(CacheUtil.getOrgannameFromMap(t.getChecked_dept()));
				}
			}
			t.setRecheckman_name(CacheUtil.getUserTurenameFromMap(t.getRecheckman()));
			t.setMonitor_user_name(CacheUtil.getUserTurenameFromMap(t.getMonitor_user_id()));
			t.setAppeal_result_name(CodeUtil.getCodeNameByFlagAndValue("appeal_result", t.getAppeal_result()+""));
		}
		return taskList;
	}


	@Override
	public void insert(Task task) {
		// TODO Auto-generated method stub
		taskDao.insert(task);
		TaskStatus status = new TaskStatus();
		status.setTask_id(commonDao.getLastId());
		status.setUpdate_time(new Date());
		status.setUpdate_user(task.getCheckman());
		status.setUpdate_username("");
		status.setCheck_status(task.getCheck_status());
		String name = (constantDao.getByFlagAndValue(new Constant("task_status",task.getCheck_status()))).getCode_name();
		status.setCheck_status_name(name);
		taskDao.insertStatus(status);
	}


	@Override
	public void update(Task task) {
		// TODO Auto-generated method stub
		Task oldTask = taskDao.getOne(task.getId());
		taskDao.update(task);
		
		//状态不一致才保存或者强制保存
		if(task.getCheck_status()!=null && (!oldTask.getCheck_status().equals(task.getCheck_status()) || !StringUtils.isNullOrEmpty(task.getRecord_status()))){
			TaskStatus status = new TaskStatus();
			status.setTask_id(task.getId());
			status.setUpdate_time(new Date());
			status.setUpdate_user(task.getUpdate_user());
			status.setUpdate_username(CacheUtil.getUserTurenameFromMap(task.getUpdate_user()));
			status.setCheck_status(task.getCheck_status());
			String name = task.getCheck_status_name();
			if(StringUtils.isNullOrEmpty(name)){
				name = (constantDao.getByFlagAndValue(new Constant("task_status",task.getCheck_status()))).getCode_name();
			}
			status.setCheck_status_name(name);
			taskDao.insertStatus(status);
		}
		
		//考核人员不一致保存日志
		if(task.getChecked_person()!=null && (!oldTask.getChecked_person().equals(task.getChecked_person()))){
			Log log = new Log();
			log.setTask_id(task.getId());
			log.setUpdate_user(task.getUpdate_user());
			log.setDept_id(oldTask.getChecked_dept());
			log.setContent("更改考核人员："+CacheUtil.getUserTurenameFromMap(oldTask.getChecked_person())+"-->"+CacheUtil.getUserTurenameFromMap(task.getChecked_person()));
			logDao.insert(log);
		}
	}


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


	@Override
	public List<TaskStatus> getTaskStatusList(Long id) {
		// TODO Auto-generated method stub
		return taskDao.getTaskStatusList(id);
	}


	@Override
	public void dispatch(Task task) {
		// TODO Auto-generated method stub
		taskDao.dispatch(task);
	}

	@Override
	public void dispatchById(Task task) {
		// TODO Auto-generated method stub
		taskDao.dispatchById(task);
	}


	@Override
	public void insertStatus(TaskStatus taskStatus) {
		// TODO Auto-generated method stub
		taskDao.insertStatus(taskStatus);
	}

	@Override
	public void insertStatusBatch(List<TaskStatus> taskStatusList) {
		// TODO Auto-generated method stub
		taskDao.insertStatusBatch(taskStatusList);
	}

	@Override
	public void deleteInvalidTaskCount(Long task_id) {
		// TODO Auto-generated method stub
		taskDao.deleteInvalidTaskCount(task_id);
	}

	@Override
	public void updateInvalidTaskCount(Task task) {
		// TODO Auto-generated method stub
		taskDao.updateInvalidTaskCount(task);
	}

	@Override
	public List<UserClassCount> getUserCount(Task task) {
		// TODO Auto-generated method stub
		return taskDao.getUserCount(task);
	}

    @Override
    public List<Task> getTaskListByDept(TaskListByDeptAppeal taskListByDeptAppeal,User user){
	    List<Task> list = taskDao.getTaskListByDept(taskListByDeptAppeal);
	    if(list!=null && list.size()>0){
            for (Task t:list
                 ) {
                t.setChecked_person_name(CacheUtil.getUserTurenameFromMap(t.getChecked_person()));
                if(AuthorUtil.hasRole(user, Constants.ROLE_JICHA_ADMIN)
                        ||AuthorUtil.hasRole(user, Constants.ROLE_JICHA)||AuthorUtil.hasRole(user, Constants.ROLE_MANAGER)
                        ){
                    t.setCheckman_name(CacheUtil.getUserTurenameFromMap(t.getCheckman()));
                }else{
                    t.setCheckman_name("-");
                }

                t.setChecked_dept_name(CacheUtil.getOrgannameFromMap(t.getChecked_dept()));
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                SimpleDateFormat df1 = new SimpleDateFormat("hh:mm");
                String name = df.format(t.getStart_time())+"~"+df1.format(t.getEnd_time());

                t.setCheck_period(name);
            }
        }


        return list;
    }
}
