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

/**
 * 文件名：TaskServiceImpl_F
 * 版本信息：日期：2017/4/7 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 *
 * @author wenhongquan
 * @date 2018/04/11
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.pojo.fwq.CheckAreaData;
import com.xintong.visualinspection.pojo.fwq.TaskData_F;
import com.xintong.visualinspection.service.CommonService;
import com.xintong.visualinspection.service.TaskService_F;
import com.xintong.visualinspection.util.Constants;
import com.xintong.visualinspection.visuallnspection_qzd.dao.FwqTaskDao;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@com.alibaba.dubbo.config.annotation.Service(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class TaskServiceImpl implements TaskService_F {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);

    @Autowired
    private FwqTaskDao fwqTaskDao;


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

    @Override
    public Object getAllTask(Integer page, Integer size, FwqCheckTask fwqCheckTasktemp) {
        if (!(page.equals(0) && size.equals(0))) {
            PageHelper.startPage(page, size);
        }
        if(fwqCheckTasktemp.getCheck_status_arr()!=null){
            fwqCheckTasktemp.setCheck_status(null);
        }

        List<FwqCheckTask> fwqCheckTaskList = fwqTaskDao.getall(fwqCheckTasktemp == null ? new FwqCheckTask() : fwqCheckTasktemp);

        Map<Long, Organ> deptMap = commonService.getDeptMap();
        Map<Long, User> userMap = commonService.getUserMap();
        Map<String, Constant> codeMap = commonService.getCodeMap();

        for (FwqCheckTask fwqCheckTask : fwqCheckTaskList
                ) {
            if (deptMap.get(fwqCheckTask.getChecked_dept()) != null) {
                fwqCheckTask.setChecked_dept_name(deptMap.get(fwqCheckTask.getChecked_dept()).getOrganname());
            }
            if (userMap.get(fwqCheckTask.getCheckman()) != null) {
                fwqCheckTask.setCheckman_name(userMap.get(fwqCheckTask.getCheckman()).getTruename());
            }
            if (fwqCheckTask.getCheckman() != null) {
                fwqCheckTask.setCheckman_name(userMap.get(fwqCheckTask.getCheckman()).getTruename());
            }

            if (fwqCheckTask.getChecked_dept() != null) {
                if (deptMap.get(fwqCheckTask.getChecked_dept()) != null) {
                    fwqCheckTask.setChecked_dept_name(deptMap.get(fwqCheckTask.getChecked_dept()).getOrganname());
                }
            }
            if(fwqCheckTask.getRecheckman()!=null){
                fwqCheckTask.setRecheckman_name(userMap.get(fwqCheckTask.getRecheckman()).getTruename());
            }
            if(fwqCheckTask.getAppeal_result()!=null){
                fwqCheckTask.setAppeal_result_name(codeMap.get("appeal_result" + "_" + fwqCheckTask.getAppeal_result() + "").getCode_name());
            }
        }

        if (!(page.equals(0) && size.equals(0))) {
            return new PageInfo<FwqCheckTask>(fwqCheckTaskList);
        }
        return fwqCheckTaskList;
    }

    @Override
    public void addTask(FwqCheckTask fwqCheckTask) {
        fwqTaskDao.addtask(fwqCheckTask);
    }

    @Override
    public void updateStatus(FwqCheckTask fwqCheckTask) {
        fwqTaskDao.updateStatus(fwqCheckTask);
        //添加状态
        if (fwqCheckTask.getCheck_status() != null) {
            TaskStatus taskStatus = new TaskStatus();
            taskStatus.setCheck_status(fwqCheckTask.getCheck_status());
            taskStatus.setCheck_status_name((commonService.getCodeMap().get("task_status_" + fwqCheckTask.getCheck_status())).getCode_name());
            User u = commonService.getUserMap().get((long) fwqCheckTask.getUpdate_user());
            if (u != null) {
                taskStatus.setUpdate_username(u.getTruename());
            }
            taskStatus.setTask_id((long) fwqCheckTask.getId());
            fwqTaskDao.insertStatus(taskStatus);


        }
    }

    @Override
    public FwqCheckTask getTask(FwqCheckTask fwqCheckTask) {
        return fwqTaskDao.getone(fwqCheckTask);
    }

    @Override
    public void addTask(TaskData_F taskData_f) throws Exception {
        List<CheckAreaData> checkAreaData = taskData_f.getCheckAreaData();
        List<User> users = taskData_f.getUsers();

        CheckTaskPeriod p = commonService.getCheckTaskPeriod(checkAreaData.get(0).getStart_time().getTime(), checkAreaData.get(0).getEnd_time().getTime());
        if (p == null) {
            throw new Exception("所选任务超出当前稽查周期内！");
        }

        try {
            //获取列表

            List<FwqCheckTask> tasks = new ArrayList<>();
            int count = checkAreaData.size() / users.size();
            int u_index = 0;
            int index = 0;
            for (CheckAreaData s : checkAreaData
                    ) {
                if (index++ == count) {
                    u_index++;
                    if (u_index == users.size()) {
                        u_index--;
                    }
                }
                try {
                    FwqCheckTask t = new FwqCheckTask();
                    t.setName(s.getDeptInfo().getName() + s.getAreaInfo().getName());
                    t.setCheck_status(Constants.STATUS_ASSIGN);
                    t.setArea_id(s.getAreaInfo().getId());
                    t.setStart_time(s.getStart_time());
                    t.setEnd_time(s.getEnd_time());
                    t.setCheckman((long) users.get(u_index).getId());
                    t.setCheckman_name(users.get(u_index).getTruename());
                    t.setChecked_dept((long) s.getAreaInfo().getDept_id());
                    t.setUpdate_user((long) taskData_f.getUser_id());
                    //服务区视频稽查考核办法 id
                    t.setRule_id(10);
                    if (p != null && p.getEndtime().getTime() > s.getStart_time().getTime()) {
                        t.setPeriod_id(p.getId());
                    }
                    fwqTaskDao.addtask(t);
                    TaskStatus taskStatus = new TaskStatus();
                    taskStatus.setCheck_status(t.getCheck_status());
                    taskStatus.setCheck_status_name((commonService.getCodeMap().get("task_status_" + t.getCheck_status())).getCode_name());
                    User u = commonService.getUserMap().get((long) taskData_f.getUser_id());
                    if (u != null) {
                        taskStatus.setUpdate_username(u.getTruename());
                    }
                    taskStatus.setTask_id((long) t.getId());
                    fwqTaskDao.insertStatus(taskStatus);

                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    @Override
    public List<TaskStatus> getTaskStatusList(Long id) {
        return fwqTaskDao.getTaskStatusList(id);
    }


}
