package com.jeeplus.modules.sg.managementcenter.reserveManagement.service;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.managementcenter.project.service.ProjectService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ExportScheme;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.PersonnelTurnover;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveManagement;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveProcess;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.PersonnelTurnoverMapper;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.ReserveManagementMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 人员流转 Service
 */
@Service
@Transactional(readOnly = true)
public class PersonnelTurnoverService extends CrudService<PersonnelTurnoverMapper, PersonnelTurnover> {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ActProcessService actProcessService;

    @Autowired
    private ActTaskService actTaskService;

    @Autowired
    private ReserveProcessService reserveProcessService;

    @Override
    public PersonnelTurnover get(PersonnelTurnover entity) {
        return super.get(entity);
    }

    @Override
    public List<PersonnelTurnover> findList(PersonnelTurnover entity) {
        return super.findList(entity);
    }

    @Override
    public Page<PersonnelTurnover> findPage(Page<PersonnelTurnover> page, PersonnelTurnover entity) {
        return super.findPage(page, entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(PersonnelTurnover entity) {
        PersonnelTurnover byProjectId = mapper.findByProjectId(entity);
        if (null == byProjectId) {
            entity.preInsert();
            entity.setOpenProcessStatus("1");
            mapper.insert(entity);
            //添加流程数据条数
            ReserveProcess reserveProcess = new ReserveProcess();
            reserveProcess.preInsert();
            reserveProcess.setProjectReserveId(entity.getProjectReserveId());
            //更新
            reserveProcessService.insert(reserveProcess);
            //发送流程，更改项目状态
            Map<String, Object> vars = Maps.newHashMap();
            vars.put("manager", UserUtils.getByUserName(entity.getDesignUnit()).getLoginName());
            String procDefId = projectService.findDefId("cbry");
            ProcessDefinition p = actProcessService.getProcessDefinition(procDefId);
            String title = entity.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
            actTaskService.startProcess(p.getKey(), "cb_personnel_activiti", reserveProcess.getId(), title, vars);
        } else {
            entity.preUpdate();
            entity.setOpenProcessStatus("1");
            mapper.update(entity);
            //添加流程数据条数
            ReserveProcess reserveProcess = new ReserveProcess();
            reserveProcess.preInsert();
            reserveProcess.setProjectReserveId(entity.getProjectReserveId());
            //更新
            reserveProcessService.insert(reserveProcess);
            //发送流程，更改项目状态
            Map<String, Object> vars = Maps.newHashMap();
            vars.put("manager", UserUtils.getByUserName(entity.getDesignUnit()).getLoginName());
            String procDefId = projectService.findDefId("cbry");
            ProcessDefinition p = actProcessService.getProcessDefinition(procDefId);
            String title = entity.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
            actTaskService.startProcess(p.getKey(), "cb_personnel_activiti", reserveProcess.getId(), title, vars);
        }
    }

    @Transactional(readOnly = false)
    public void insert(PersonnelTurnover entity) {
        mapper.insert(entity);
    }

    @Transactional(readOnly = false)
    public void update(PersonnelTurnover entity) {
        mapper.update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteAll(Collection<PersonnelTurnover> entitys) {
        super.deleteAll(entitys);
    }

    /**
     * 通过储备项目编号查询人员流转信息
     */
    public PersonnelTurnover findByProjectId(PersonnelTurnover personnelTurnover) {
        return mapper.findByProjectId(personnelTurnover);
    }

    /**
     * 通过储备项目编号修改开启流程状态
     */
    @Transactional(readOnly = false)
    public void updateOpenStatus(String projectReserveId, String openProcessStatus) {
        mapper.updateOpenStatus(projectReserveId, openProcessStatus);
    }

    /**
     * 通过储备项目编号清空人员流转信息
     */
    @Transactional(readOnly = false)
    public void deletePerson(String projectReserveId) {
        mapper.deletePerson(projectReserveId);
    }


}
