/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.projectcontroltable.service;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectcontentinfoDao;
import com.jeeplus.modules.projectcontentinfo.entity.Projectcontentinfo;
import com.jeeplus.modules.projectcontroltable.dao.ProjectControlTableDao;
import com.jeeplus.modules.projectcontroltable.entity.ProjectControlTable;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.service.WorkattachmentService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workcommunicaterecord.dao.WorkCommunicateContentDao;
import com.jeeplus.modules.workcommunicaterecord.dao.WorkCommunicateRecordDao;
import com.jeeplus.modules.workcommunicaterecord.entity.WorkCommunicateContent;
import com.jeeplus.modules.workcommunicaterecord.entity.WorkCommunicateRecord;
import com.jeeplus.modules.workcommunicaterecord.service.WorkCommunicateContentService;
import com.jeeplus.modules.workcommunicaterecord.service.WorkCommunicateRecordService;
import com.jeeplus.modules.workdevicerecord.dao.WorkDeviceRecordDao;
import com.jeeplus.modules.workdevicerecord.dao.WorkDeviceXjRecordDao;
import com.jeeplus.modules.workdevicerecord.entity.WorkDeviceRecord;
import com.jeeplus.modules.workdevicerecord.entity.WorkDeviceXjRecord;
import com.jeeplus.modules.workdevicerecord.service.WorkDeviceRecordService;
import com.jeeplus.modules.workdevicerecord.service.WorkDeviceXjRecordService;
import com.jeeplus.modules.workexplore.dao.WorkExploreDao;
import com.jeeplus.modules.workexplore.entity.WorkExplore;
import com.jeeplus.modules.workexplore.service.WorkExploreService;
import com.jeeplus.modules.workhandbill.dao.WorkHandBillDao;
import com.jeeplus.modules.workhandbill.dao.WorkHandBillDetailDao;
import com.jeeplus.modules.workhandbill.entity.WorkHandBill;
import com.jeeplus.modules.workhandbill.entity.WorkHandBillDetail;
import com.jeeplus.modules.workhandbill.service.WorkHandBillDetailService;
import com.jeeplus.modules.workhandbill.service.WorkHandBillService;
import com.jeeplus.modules.workinfolist.dao.WorkInfoListDao;
import com.jeeplus.modules.workinfolist.entity.WorkInfoList;
import com.jeeplus.modules.workinfolist.service.WorkInfoListService;
import com.jeeplus.modules.worklog.dao.WorkLogDao;
import com.jeeplus.modules.worklog.dao.WorkMeetingDao;
import com.jeeplus.modules.worklog.entity.WorkLog;
import com.jeeplus.modules.worklog.entity.WorkMeeting;
import com.jeeplus.modules.workprojectcalc.dao.WorkProjectCalcBuildDao;
import com.jeeplus.modules.workprojectcalc.dao.WorkProjectCalcDecorDao;
import com.jeeplus.modules.workprojectcalc.entity.WorkProjectCalcBuild;
import com.jeeplus.modules.workprojectcalc.entity.WorkProjectCalcDecor;
import com.jeeplus.modules.workprojectcalc.service.WorkProjectCalcBuildService;
import com.jeeplus.modules.workprojectcalc.service.WorkProjectCalcDecorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 过程控制表Service
 * @author 马鹏波
 * @version 2018-06-08
 */
@Service
@Transactional(readOnly = true)
public class ProjectControlTableService extends CrudService<ProjectControlTableDao, ProjectControlTable> {
	@Autowired
	private ProjectControlTableDao projectControlTableDao;
	@Autowired
	private WorkHandBillDetailDao workHandBillDetailDao;
	@Autowired
	private WorkHandBillDao workHandBillDao;
	@Autowired
	private WorkExploreDao workExploreDao;
	@Autowired
	private WorkInfoListDao workInfoListDao;
	@Autowired
	private WorkDeviceXjRecordDao workDeviceXjRecordDao;
	@Autowired
	private WorkProjectCalcDecorDao workProjectCalcDecorDao;
	@Autowired
	private WorkProjectCalcBuildDao workProjectCalcBuildDao;
	@Autowired
	private WorkDeviceRecordDao workDeviceRecordDao;
	@Autowired
	private WorkCommunicateRecordDao workCommunicateRecordDao;
	@Autowired
	private WorkCommunicateContentDao workCommunicateContentDao;
	@Autowired
	private WorkattachmentDao workattachmentDao;
	@Autowired
	private WorkattachmentService workattachmentService;
    @Autowired
    private ProjectcontentinfoDao projectcontentinfoDao;
    @Autowired
    private WorkHandBillService workHandBillService;
    @Autowired
    private WorkHandBillDetailService workHandBillDetailService;
    @Autowired
    private WorkDeviceXjRecordService workDeviceXjRecordService;
    @Autowired
    private WorkDeviceRecordService workDeviceRecordService;
    @Autowired
    private WorkExploreService workExploreService;
    @Autowired
    private WorkInfoListService workInfoListService;
    @Autowired
    private WorkCommunicateRecordService workCommunicateRecordService;
    @Autowired
    private WorkCommunicateContentService workCommunicateContentService;
    @Autowired
    private WorkProjectCalcDecorService workProjectCalcDecorService;
    @Autowired
    private WorkProjectCalcBuildService workProjectCalcBuildService;
    @Autowired
	private WorkLogDao workLogDao;
    @Autowired
	private WorkMeetingDao workMeetingDao;



	public ProjectControlTable get(String id) {
		return super.get(id);
	}
	
	public List<ProjectControlTable> findList(ProjectControlTable projectControlTable) {
		return super.findList(projectControlTable);
	}
	
	public Page<ProjectControlTable> findPage(Page<ProjectControlTable> page, ProjectControlTable projectControlTable) {
		return super.findPage(page, projectControlTable);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectControlTable projectControlTable) {
		super.save(projectControlTable);
		String s = projectControlTable.gettType();
		if ("01".equals(s)) {
			execDeviceRecord(projectControlTable);
		} else if ("02".equals(s)) {
			execInfoList(projectControlTable);
		} else if ("03".equals(s)) {
			execExplore(projectControlTable);
		} else if ("04".equals(s)) {
			execCommunicate(projectControlTable);
		} else if ("05".equals(s)) {
			execproject(projectControlTable);
		} else if ("06".equals(s)) {
			execHandBill(projectControlTable);
		} else if("07".equals(s)){
            execLog(projectControlTable);
		}  else if("08".equals(s)){
            execMeeting(projectControlTable);
		}

		List<Workattachment> workAttachments = projectControlTable.getWorkAttachments();
		if(workAttachments != null) {
			//附件信息
			for (Workattachment workattachment : workAttachments) {
				if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workattachment.getDelFlag())) {
					workattachment.setAttachmentId(projectControlTable.getId());
					if ("01".equals(s)) {	//设备询价记录
						workattachment.setAttachmentFlag("88");
					} else if ("02".equals(s)) {	//补充资料
						workattachment.setAttachmentFlag("89");
					} else if ("03".equals(s)) {	//现场踏勘
						workattachment.setAttachmentFlag("90");
					} else if ("04".equals(s)) {	//沟通记录
						workattachment.setAttachmentFlag("91");
					} else if ("05".equals(s)) {	//工程量计算底稿
						workattachment.setAttachmentFlag("92");
					} else if ("06".equals(s)) {	// 交接单
						workattachment.setAttachmentFlag("93");
					} else if("07".equals(s)){      //日志
						workattachment.setAttachmentFlag("93");
					} else if("08".equals(s)){      //会议
						workattachment.setAttachmentFlag("93");
					}
					workattachment.setAttachmentUser(UserUtils.getUser().getId());
					if (Strings.isNullOrEmpty(workattachment.getId()) || "null".equals(workattachment.getId())) {
						workattachment.preInsert();
						workattachmentDao.insert(workattachment);
					} else {
						workattachment.preUpdate();
						workattachmentDao.update(workattachment);
					}
				} else {
					workattachmentDao.delete(workattachment);
				}
			}
		}
	}

	/**
	 * 执行设备询价记录
	 * @param projectControlTable
	 */
	public void execDeviceRecord(ProjectControlTable projectControlTable){
		WorkDeviceXjRecord workDeviceXjRecord = projectControlTable.getWorkDeviceXjRecord();
		if(workDeviceXjRecord != null){
			if(WorkDeviceXjRecord.DEL_FLAG_NORMAL.equals(workDeviceXjRecord.getDelFlag())){
				if(workDeviceXjRecord.getIsNewRecord()){
					workDeviceXjRecord.setProjectId(projectControlTable.getId());
					workDeviceXjRecord.preInsert();
					workDeviceXjRecordDao.insert(workDeviceXjRecord);
				}else{
					workDeviceXjRecord.preUpdate();
					workDeviceXjRecordDao.update(workDeviceXjRecord);
				}
			}else{
				workDeviceXjRecordDao.delete(workDeviceXjRecord);
			}
			List<WorkDeviceRecord> workDeviceRecordList = workDeviceXjRecord.getWorkDeviceRecordList();
			if(workDeviceRecordList != null){
				for(WorkDeviceRecord workDeviceRecord:workDeviceRecordList){
					if(WorkDeviceRecord.DEL_FLAG_NORMAL.equals(workDeviceRecord.getDelFlag())){
						workDeviceRecord.setDeviceXjId(workDeviceXjRecord.getId());
						if(workDeviceRecord.getIsNewRecord()){
							workDeviceRecord.preInsert();
							workDeviceRecordDao.insert(workDeviceRecord);
						}else{
							workDeviceRecord.preUpdate();
							workDeviceRecordDao.update(workDeviceRecord);
						}
					}else{
						workDeviceRecordDao.delete(workDeviceRecord);
					}
				}
			}
		}
	}

	/**
	 * 执行补充资料清单
	 * @param projectControlTable
	 */
	public void execInfoList(ProjectControlTable projectControlTable){
		WorkInfoList workInfoList = projectControlTable.getWorkInfoList();
		if(workInfoList != null){
			if(workInfoList.DEL_FLAG_NORMAL.equals(workInfoList.getDelFlag())){
				if(workInfoList.getIsNewRecord()){
					workInfoList.setProjectId(projectControlTable.getId());
					workInfoList.preInsert();
					workInfoListDao.insert(workInfoList);
				}else{
					workInfoList.preUpdate();
					workInfoListDao.update(workInfoList);
				}
			}else{
				workInfoListDao.delete(workInfoList);
			}
		}
	}

	/**
	 * 现场踏勘记录
	 * @param projectControlTable
	 */
	public void execExplore(ProjectControlTable projectControlTable){
		WorkExplore workExplore = projectControlTable.getWorkExplore();
		if(workExplore != null){
			if(WorkExplore.DEL_FLAG_NORMAL.equals(workExplore.getDelFlag())){
				if(workExplore.getIsNewRecord()){
					workExplore.setProjectId(projectControlTable.getId());
					workExplore.preInsert();
					workExploreDao.insert(workExplore);
				}else{
					workExplore.preUpdate();
					workExploreDao.update(workExplore);
				}
			}else{
				workExploreDao.delete(workExplore);
			}
		}
	}

	/**
	 *	沟通记录表
	 * @param projectControlTable
	 */
	public void execCommunicate(ProjectControlTable projectControlTable){
		WorkCommunicateRecord workCommunicateRecord = projectControlTable.getWorkCommunicateRecord();
		if(workCommunicateRecord != null){
			if(WorkCommunicateRecord.DEL_FLAG_NORMAL.equals(workCommunicateRecord.getDelFlag())){
				if(workCommunicateRecord.getIsNewRecord()){
					workCommunicateRecord.setProjectId(projectControlTable.getId());
					workCommunicateRecord.preInsert();
					workCommunicateRecordDao.insert(workCommunicateRecord);
				}else{
					workCommunicateRecord.preUpdate();
					workCommunicateRecordDao.update(workCommunicateRecord);
				}
			}else{
				workCommunicateRecordDao.delete(workCommunicateRecord);
			}
			List<WorkCommunicateContent> workCommunicateContentList = workCommunicateRecord.getWorkCommunicateContentList();
			if(workCommunicateContentList != null){
				for(WorkCommunicateContent workCommunicateContent:workCommunicateContentList){
					if(WorkCommunicateContent.DEL_FLAG_NORMAL.equals(workCommunicateContent.getDelFlag())){
						workCommunicateContent.setCommunicateId(workCommunicateRecord.getId());
						if(workCommunicateContent.getIsNewRecord()){
							workCommunicateContent.preInsert();
							workCommunicateContentDao.insert(workCommunicateContent);
						}else{
							workCommunicateContent.preUpdate();
							workCommunicateContentDao.update(workCommunicateContent);
						}
					}else{
						workCommunicateContentDao.delete(workCommunicateContent);
					}
				}
			}
		}
	}
	/**
	 *	工程量计算底稿
	 * @param projectControlTable
	 */
	public void execproject(ProjectControlTable projectControlTable){
		List<WorkProjectCalcDecor> workProjectCalcDecorList = projectControlTable.getWorkProjectCalcDecorList();
		if(workProjectCalcDecorList != null){
			for(WorkProjectCalcDecor workProjectCalcDecor:workProjectCalcDecorList){
				if(WorkProjectCalcDecor.DEL_FLAG_NORMAL.equals(workProjectCalcDecor.getDelFlag())){
					workProjectCalcDecor.setProjectId(projectControlTable.getId());
					if(workProjectCalcDecor.getIsNewRecord()){
						workProjectCalcDecor.preInsert();
						workProjectCalcDecorDao.insert(workProjectCalcDecor);
					}else{
						workProjectCalcDecor.preUpdate();
						workProjectCalcDecorDao.update(workProjectCalcDecor);
					}
				}else{
					workProjectCalcDecorDao.delete(workProjectCalcDecor);
				}
			}
		}
		List<WorkProjectCalcBuild> workProjectCalcBuildList = projectControlTable.getWorkProjectCalcBuildList();
		if(workProjectCalcBuildList != null){
			for(WorkProjectCalcBuild workProjectCalcBuild:workProjectCalcBuildList){
				if(WorkProjectCalcBuild.DEL_FLAG_NORMAL.equals(workProjectCalcBuild.getDelFlag())){
					workProjectCalcBuild.setProjectId(projectControlTable.getId());
					if(workProjectCalcBuild.getIsNewRecord()){
						workProjectCalcBuild.preInsert();
						workProjectCalcBuildDao.insert(workProjectCalcBuild);
					}else{
						workProjectCalcBuild.preUpdate();
						workProjectCalcBuildDao.update(workProjectCalcBuild);
					}
				}else{
					workProjectCalcBuildDao.delete(workProjectCalcBuild);
				}
			}
		}
	}
	/**
	 *	日志
	 * @param projectControlTable
	 */
	public void execLog(ProjectControlTable projectControlTable){
		WorkLog workLog = projectControlTable.getWorkLog();
		if(workLog != null){
			if(WorkLog.DEL_FLAG_NORMAL.equals(workLog.getDelFlag())){
				if(workLog.getIsNewRecord()){
					workLog.setProjectId(projectControlTable.getId());
					workLog.preInsert();
					workLogDao.insert(workLog);
				}else{
					workLog.preUpdate();
					workLogDao.update(workLog);
				}
			}else{
				workLogDao.delete(workLog);
			}
		}
	}

	/**
	 *	会议
	 * @param projectControlTable
	 */
	public void execMeeting(ProjectControlTable projectControlTable){
		WorkMeeting workMeeting = projectControlTable.getWorkMeeting();

		if(workMeeting != null){
			if(WorkLog.DEL_FLAG_NORMAL.equals(workMeeting.getDelFlag())){
				if(workMeeting.getIsNewRecord()){
					workMeeting.setProjectId(projectControlTable.getId());
					workMeeting.preInsert();
					workMeetingDao.insert(workMeeting);
				}else{
					workMeeting.preUpdate();
					workMeetingDao.update(workMeeting);
				}
			}else{
				workMeetingDao.delete(workMeeting);
			}
		}
	}

	/**
	 *	交接单
	 * @param projectControlTable
	 */
	public void execHandBill(ProjectControlTable projectControlTable){
		WorkHandBill workHandBill = projectControlTable.getWorkHandBill();
		if(workHandBill != null){
			if(WorkHandBill.DEL_FLAG_NORMAL.equals(workHandBill.getDelFlag())){
				if(workHandBill.getIsNewRecord()){
					workHandBill.setProjectId(projectControlTable.getId());
					workHandBill.preInsert();
					workHandBillDao.insert(workHandBill);
				}else{
					workHandBill.preUpdate();
					workHandBillDao.update(workHandBill);
				}
			}else{
				workHandBillDao.delete(workHandBill);
			}
			List<WorkHandBillDetail> workHandBillDetailList = workHandBill.getWorkHandBillDetailList();
			if(workHandBillDetailList != null){
				for(WorkHandBillDetail workHandBillDetail:workHandBillDetailList){
					if(WorkHandBillDetail.DEL_FLAG_NORMAL.equals(workHandBillDetail.getDelFlag())){
						workHandBillDetail.setBillId(workHandBill.getId());
						if(workHandBillDetail.getIsNewRecord()){
							workHandBillDetail.preInsert();
							workHandBillDetailDao.insert(workHandBillDetail);
						}else{
							workHandBillDetail.preUpdate();
							workHandBillDetailDao.update(workHandBillDetail);
						}
					}else{
						workHandBillDetailDao.delete(workHandBillDetail);
					}
				}
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(ProjectControlTable projectControlTable) {
		super.delete(projectControlTable);
	}

	public List<ProjectControlTable> getControlData(String projectId,String projectContentDataId){
			ProjectControlTable projectControlTable = new ProjectControlTable();
			projectControlTable.setPfId(projectId);
			List<ProjectControlTable> controlData = projectControlTableDao.getControlData(projectControlTable);
			projectControlTable.setPfId(projectContentDataId);
			List<ProjectControlTable> controlData1 = projectControlTableDao.getControlData(projectControlTable);
			controlData.addAll(controlData1);
			return controlData;
	}

	@Transactional(readOnly = false)
	public boolean deleteAllData(String projectId,String projectContentDataId){
		ProjectControlTable projectControlTable = new ProjectControlTable();
		ProjectControlTable existRecord = null;
		//表格控制临时数据
		List<ProjectControlTable> controlData = Lists.newArrayList();
		if(!Strings.isNullOrEmpty(projectContentDataId)){
			projectControlTable.setId(projectContentDataId);
			controlData = projectControlTableDao.findListById(projectControlTable);
            existRecord = controlData.get(0);
		}else{
			projectControlTable.setPfId(projectId);
			controlData = projectControlTableDao.getControlData(projectControlTable);
		}


		boolean flag = false;
		for (ProjectControlTable projectControlTables:controlData){
			String tableId = projectControlTables.getId();
			String s = projectControlTables.gettType();
			//删除临时的表格控制数据
			int i = projectControlTableDao.delete(projectControlTables);
			if(i > 0) {
				if ("06".equals(s)) {
					//删除交接单数据
					WorkHandBill workHandBill = new WorkHandBill();
					workHandBill.setProjectId(tableId);
					WorkHandBill listByProjectId = workHandBillDao.findListByProjectId(workHandBill);
					int i1 = workHandBillDao.deleteByProId(workHandBill);
					if(i1 > 0){
						//交接单详情数据
						WorkHandBillDetail workHandBillDetail = new WorkHandBillDetail();
						workHandBillDetail.setBillId(listByProjectId.getId());
						int i2 = workHandBillDetailDao.deleteByBillId(workHandBillDetail);
						if(i2 >= 0){
							flag = true;
						}else {
							break;
						}
					}else {
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "93");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("02".equals(s)){
					//删除补充资料清单
					WorkInfoList workInfoList = new WorkInfoList();
					workInfoList.setProjectId(tableId);
					int i3 = workInfoListDao.deleteByProId(workInfoList);
					if(i3 > 0){
						flag = true;
					}else{
						break;
					}

					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "89");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("03".equals(s)){
					//现场踏勘记录
					WorkExplore workExplore = new WorkExplore();
					workExplore.setProjectId(tableId);
					int i4 = workExploreDao.deleteByProId(workExplore);
					if(i4 > 0){
						flag = true;
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "90");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("04".equals(s)){
					//删除沟通记录表
					WorkCommunicateRecord workCommunicateRecord = new WorkCommunicateRecord();
					workCommunicateRecord.setProjectId(tableId);
					WorkCommunicateRecord workRecord = workCommunicateRecordDao.findListByProId(workCommunicateRecord);
					int i5 = workCommunicateRecordDao.deleteByProId(workCommunicateRecord);
					if(i5 > 0){
						WorkCommunicateContent workCommunicateContent = new WorkCommunicateContent();
						workCommunicateContent.setCommunicateId(workRecord.getId());
						int i2 = workCommunicateContentDao.deleteByProId(workCommunicateContent);
						if(i2 >= 0){
							flag = true;
						}else {
							break;
						}
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "91");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("05".equals(s)){
					//删除工程量计算底稿
					boolean f = false;
					boolean f1 = false;
					WorkProjectCalcBuild workProjectCalcBuild = new WorkProjectCalcBuild();
					workProjectCalcBuild.setProjectId(tableId);
					int i6 = workProjectCalcBuildDao.deleteByProId(workProjectCalcBuild);
					if(i6 >= 0){
						f = true;
					}else{
						break;
					}
					WorkProjectCalcDecor workProjectCalcDecor = new WorkProjectCalcDecor();
					workProjectCalcDecor.setProjectId(tableId);
					int i7 = workProjectCalcDecorDao.deleteByProId(workProjectCalcDecor);
					if(i7 >= 0){
						f1 = true;
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "92");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
					if(f && f1){
						flag = true;
					}
				}else if("01".equals(s)){
					//删除设备询价记录
					WorkDeviceXjRecord workDeviceXjRecord = new WorkDeviceXjRecord();
					workDeviceXjRecord.setProjectId(tableId);
					WorkDeviceXjRecord listByXjId = workDeviceXjRecordDao.findListByXjId(workDeviceXjRecord);
					int i8 = workDeviceXjRecordDao.deleteByProId(workDeviceXjRecord);
					if(i8 > 0){
						WorkDeviceRecord workDeviceRecord = new WorkDeviceRecord();
						workDeviceRecord.setDeviceXjId(listByXjId.getId());
						int i2 = workDeviceRecordDao.deleteByProId(workDeviceRecord);
						if(i2 >= 0){
							flag = true;
						}else {
							break;
						}
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "88");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("07".equals(s)){
					//日志
					WorkLog workLog = new WorkLog();
					workLog.setProjectId(tableId);
					int i4 = workLogDao.deleteByProId(workLog);
					if(i4 > 0){
						flag = true;
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "93");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}else if("08".equals(s)){
					//会议
					WorkMeeting workMeeting = new WorkMeeting();
					workMeeting.setProjectId(tableId);
					int i4 = workMeetingDao.deleteByProId(workMeeting);
					if(i4 > 0){
						flag = true;
					}else{
						break;
					}
					List<Workattachment> listOfAttachment = workattachmentDao.getListOfAttachment(tableId, "93");
					for (Workattachment workattachment:listOfAttachment){
						workattachmentService.deleteFileFromAliyun(workattachment,workattachment.getUrl());
					}
				}
			}

		}
		if(existRecord!=null){
		    this.deleteCtrlMenu(existRecord);
        }
		return flag;
	}

    public ProjectControlTable getById(String infoId) {
        ProjectControlTable projectControlTable = this.get(infoId);
        if(projectControlTable!=null) {
            projectControlTable.setWorkAttachments(workattachmentService.getListByAttachmentIdAndFlag(infoId, ""));
        }
        return projectControlTable;
    }

    private void deleteCtrlMenu(ProjectControlTable controlTable){
        //删除菜单
        Projectcontentinfo parent = projectcontentinfoDao.getByInfoId(controlTable.getPfId());
        if(parent==null)return;
        Projectcontentinfo select2 = new Projectcontentinfo();
        select2.setParent(parent);
        select2.setProject(parent.getProject());
        select2.setName(controlTable.getProcessName());
        List<Projectcontentinfo> infos = projectcontentinfoDao.findListByParentSelective(select2);
        Projectcontentinfo projectcontentinfo = infos.get(0);
        select2.setParent(projectcontentinfo);
        select2.setName(null);
        select2.setInfoId(controlTable.getId());
        //删除当前菜单
        projectcontentinfoDao.deleteByParentAndLinkId(select2);
        //是否删除父级菜单
        select2.setInfoId(null);
        List<Projectcontentinfo> sonList = projectcontentinfoDao.findListByParentSelective(select2);
        if(sonList==null || sonList.isEmpty()){
            projectcontentinfoDao.delete(projectcontentinfo);
        }
    }

    public String getDetailInfo(ProjectControlTable projectControlTable) {
	    if(projectControlTable==null){
	        return null;
        }
        String s = projectControlTable.gettType();
        String flag = "";
        if ("06".equals(s)) {
            WorkHandBill workHandBill = new WorkHandBill();
            workHandBill.setProjectId(projectControlTable.getId());
            workHandBill = workHandBillService.findListByProjectId(workHandBill);
            WorkHandBillDetail workHandBillDetail = new WorkHandBillDetail();
            workHandBillDetail.setBillId(workHandBill.getId());
            List<WorkHandBillDetail> listByBillId = workHandBillDetailService.findListByBillId(workHandBillDetail);
            workHandBill.setWorkHandBillDetailList(listByBillId);
            projectControlTable.setWorkHandBill(workHandBill);
            flag = "93";
        } else if ("02".equals(s)) {
            WorkInfoList workInfoList = new WorkInfoList();
            workInfoList.setProjectId(projectControlTable.getId());
            WorkInfoList listByProId = workInfoListService.findListByProId(workInfoList);
            projectControlTable.setWorkInfoList(listByProId);
            flag = "89";
        } else if ("03".equals(s)) {
            WorkExplore workExplore = new WorkExplore();
            workExplore.setProjectId(projectControlTable.getId());
            WorkExplore listByProId = workExploreService.findListByProId(workExplore);
            projectControlTable.setWorkExplore(listByProId);
            flag = "90";
        } else if ("04".equals(s)) {
            WorkCommunicateRecord workCommunicateRecord = new WorkCommunicateRecord();
            workCommunicateRecord.setProjectId(projectControlTable.getId());
            workCommunicateRecord = workCommunicateRecordService.findListByProId(workCommunicateRecord);
            WorkCommunicateContent workCommunicateContent = new WorkCommunicateContent();
            workCommunicateContent.setCommunicateId(workCommunicateRecord.getId());
            List<WorkCommunicateContent> listByCommunicateId = workCommunicateContentService.findListByCommunicateId(workCommunicateContent);
            workCommunicateRecord.setWorkCommunicateContentList(listByCommunicateId);
            projectControlTable.setWorkCommunicateRecord(workCommunicateRecord);
            flag = "91";
        } else if ("05".equals(s)) {
            WorkProjectCalcBuild workProjectCalcBuild = new WorkProjectCalcBuild();
            workProjectCalcBuild.setProjectId(projectControlTable.getId());
            List<WorkProjectCalcBuild> list1 = workProjectCalcBuildService.findList(workProjectCalcBuild);
            projectControlTable.setWorkProjectCalcBuildList(list1);
            WorkProjectCalcDecor workProjectCalcDecor = new WorkProjectCalcDecor();
            workProjectCalcDecor.setProjectId(projectControlTable.getId());
            List<WorkProjectCalcDecor> list = workProjectCalcDecorService.findList(workProjectCalcDecor);
            projectControlTable.setWorkProjectCalcDecorList(list);
            flag = "92";
        } else if ("01".equals(s)) {
            WorkDeviceXjRecord workDeviceXjRecord = new WorkDeviceXjRecord();
            workDeviceXjRecord.setProjectId(projectControlTable.getId());
            workDeviceXjRecord = workDeviceXjRecordService.findListByXjId(workDeviceXjRecord);
            WorkDeviceRecord workDeviceRecord = new WorkDeviceRecord();
            workDeviceRecord.setDeviceXjId(workDeviceXjRecord.getId());
            List<WorkDeviceRecord> listByXjId1 = workDeviceRecordService.findListByXjId(workDeviceRecord);
            workDeviceXjRecord.setWorkDeviceRecordList(listByXjId1);
            projectControlTable.setWorkDeviceXjRecord(workDeviceXjRecord);
            flag = "88";
        }else if ("07".equals(s)) {
			WorkLog workLog = new WorkLog();
			workLog.setProjectId(projectControlTable.getId());
			WorkLog listByProId = workLogDao.findListByProId(workLog);
			projectControlTable.setWorkLog(listByProId);
			flag = "93";
		}else if("08".equals(s)){
			WorkMeeting workMeeting = new WorkMeeting();
			workMeeting.setProjectId(projectControlTable.getId());
			WorkMeeting listByProId = workMeetingDao.findListByProId(workMeeting);
			projectControlTable.setWorkMeeting(listByProId);
			flag = "93";

		}
		return flag;
    }
}