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.filing.constructionStage.entity.ProjectSchedule;
import com.jeeplus.modules.sg.managementcenter.materialinfo.mapper.MaterialInfoMapper;
import com.jeeplus.modules.sg.managementcenter.project.service.ProjectService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.PersonnelTurnover;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ProgressSchedule;
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.ProgressScheduleMapper;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.ReserveProcessMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 人员流转 Service
 */
@Service
@Transactional(readOnly = true)
public class ProgressScheduleService extends CrudService<ProgressScheduleMapper, ProgressSchedule> {

    @Autowired
    private PersonnelTurnoverMapper turnoverMapper;

    @Autowired
    private MaterialInfoMapper materialInfoMapper;

    @Autowired
    private ReserveProcessMapper reserveProcessMapper;

    @Autowired
    private ProgressScheduleMapper scheduleMapper;

    @Autowired
    private RuntimeService runtimeService;


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

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

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

    @Override
    public void save(ProgressSchedule entity) {
        super.save(entity);
    }

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

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

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

    /**
     * 更新流程对应节点的完成时间
     */
    @Transactional(readOnly = false)
    public void updateTimeNode(ProgressSchedule entity) {
        mapper.updateTimeNode(entity);
    }

    /**
     * 通过项目id重置项目
     * */
    @Transactional(readOnly = false)
    public String reset(ProgressSchedule entity){
        String projectReserveId = entity.getProjectReserveId();
        String projectName = entity.getProjectName();
        // 判断项目是否发起了储备项目流程，未发起无需重置
        PersonnelTurnover byReserveId = turnoverMapper.findByReserveId(projectReserveId);

        String openProcessStatus = byReserveId.getOpenProcessStatus();
        if ("0".equals(openProcessStatus)) {
            return "项目无需重置，未发起储备流程";
        }
        //判断项目是否发起施工交底流程，如果发起，不可以重置，
        String status = mapper.findStatus(projectReserveId);
        if (null != status && !"0".equals(status)) {
            return "项目已发起施工交底流程，无法重置";
        }
        String addWareHousing = byReserveId.getAddWareHousing();
        if (!"0".equals(addWareHousing)) {
            turnoverMapper.updateAddWareHousingStatus(projectReserveId,"2");
        }
        if (!"2".equals(byReserveId.getOpenProcessStatus())) {
            String proc = reserveProcessMapper.findProc(projectReserveId);
            //根据流程定义号删除流程
            runtimeService.deleteProcessInstance(proc, " ");
        }

        // 重置年度物资上报统计/明细，
        materialInfoMapper.deleteByPro(projectName);
        materialInfoMapper.deleteMaterial(projectName);
        // 删除储备流程对应数据，
        reserveProcessMapper.deleteAct(projectReserveId);
        // 重置项目流程进度时间，
        scheduleMapper.resetTime(projectReserveId);
        // 重置人员流转信息-清空人员流转信息，修改流程开启状态，甲供材清册状态
        turnoverMapper.resetStatus(projectReserveId);
        //修改状态入库退回
        //删除甲供材信息
        reserveProcessMapper.deleteMaterial(projectName);

        return "重置成功";
    }
}
