package com.uas.platform.b2b.mobile.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.uas.platform.b2b.dao.UserBaseInfoDao;
import com.uas.platform.b2b.mobile.dao.AppFlowOperationDao;
import com.uas.platform.b2b.mobile.dao.ApprovalLogDao;
import com.uas.platform.b2b.mobile.dao.FeePleaseDao;
import com.uas.platform.b2b.mobile.dao.SignAppDao;
import com.uas.platform.b2b.mobile.dao.VacationDao;
import com.uas.platform.b2b.mobile.dao.WorkDailyDao;
import com.uas.platform.b2b.mobile.dao.WorkOvertimeDao;
import com.uas.platform.b2b.mobile.enumeration.MobileStatus;
import com.uas.platform.b2b.mobile.model.AppFlowOperation;
import com.uas.platform.b2b.mobile.model.ApprovalLog;
import com.uas.platform.b2b.mobile.model.FeePlease;
import com.uas.platform.b2b.mobile.model.SignApp;
import com.uas.platform.b2b.mobile.model.Vacation;
import com.uas.platform.b2b.mobile.model.WorkDaily;
import com.uas.platform.b2b.mobile.model.WorkOvertime;
import com.uas.platform.b2b.mobile.service.PublicInterForDetailService;

@Service
public class PublicInterForDetailServiceImpl implements PublicInterForDetailService {

	@Autowired
	private FeePleaseDao feePleaseDao;

	@Autowired
	private VacationDao vacationDao;

	@Autowired
	private WorkOvertimeDao workOvertimeDao;

	@Autowired
	private WorkDailyDao workDailyDao;

	@Autowired
	private ApprovalLogDao approvalLogDao;

	@Autowired
	private AppFlowOperationDao appFlowOperationDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Autowired
	private SignAppDao signAppDao;

	@Override
	public ModelMap getDocumentsDetail(Long id, Integer whichpage) {
		ModelMap map = new ModelMap();
		if (whichpage.equals(MobileStatus.FEEPLEASE.getValue())) {
			map.put("data", feePleaseDao.findOne(id));
		}
		if(whichpage.equals(MobileStatus.VACATION.getValue())){
			map.put("data", vacationDao.findOne(id));
		}
		if(whichpage.equals(MobileStatus.WORKDAILY.getValue())){
			map.put("data", workDailyDao.findOne(id));
		}
		if(whichpage.equals(MobileStatus.WORKOVERTIME.getValue())){
			map.put("data", workOvertimeDao.findOne(id));
		}
		if (whichpage.equals(MobileStatus.SIGNAPP.getValue())) {
			map.put("data", signAppDao.findOne(id));
		}
		return map;
	}

	@Override
	public ModelMap deleteDocuments(Long id, Integer whichpage) {
		ModelMap map = new ModelMap();
		String caller = null;
		if (whichpage.equals(MobileStatus.FEEPLEASE.getValue())) {
			caller = MobileStatus.FEEPLEASE.getPhrase();
		}
		if (whichpage.equals(MobileStatus.VACATION.getValue())) {
			caller = MobileStatus.VACATION.getPhrase();
		}
		if (whichpage.equals(MobileStatus.WORKDAILY.getValue())) {
			caller = MobileStatus.WORKDAILY.getPhrase();
		}
		if (whichpage.equals(MobileStatus.WORKOVERTIME.getValue())) {
			caller = MobileStatus.WORKOVERTIME.getPhrase();
		}
		if (whichpage.equals(MobileStatus.SIGNAPP.getValue())) {
			caller = MobileStatus.SIGNAPP.getPhrase();
		}
		// 先判断个单据的状态
		boolean flag = false;
		if (whichpage.equals(MobileStatus.FEEPLEASE.getValue())) {
			FeePlease fee = feePleaseDao.findOne(id);
			if (fee != null) {
				if (fee.getFp_statuscode().equals(MobileStatus.AUDITED.name())) {
					map.put("error", "已审核的单据不能删除");
				} else {
					feePleaseDao.delete(id);
					map.put("success", true);
					flag = true;
				}
			} else {
				map.put("error", "单据不存在，可能已被删除");
			}
		}
		if (whichpage.equals(MobileStatus.VACATION.getValue())) {
			Vacation va = vacationDao.findOne(id);
			if (va != null) {
				if (va.getVa_statuscode().equals(MobileStatus.AUDITED.name())) {
					map.put("error", "已审核的单据不能删除");
				} else {
					vacationDao.delete(id);
					map.put("success", true);
					flag = true;
				}
			} else {
				map.put("error", "单据不存在，可能已被删除");
			}
		}
		if (whichpage.equals(MobileStatus.WORKDAILY.getValue())) {
			WorkDaily wo = workDailyDao.findOne(id);
			if (wo != null) {
				if (wo.getWd_statuscode().equals(MobileStatus.AUDITED.name())) {
					map.put("error", "已审核的单据不能删除");
				} else {
					workDailyDao.delete(id);
					map.put("success", true);
					flag = true;
				}
			} else {
				map.put("error", "单据不存在，可能已被删除");
			}
		}
		if (whichpage.equals(MobileStatus.WORKOVERTIME.getValue())) {
			WorkOvertime wo = workOvertimeDao.findOne(id);
			if (wo != null) {
				if (wo.getWo_statuscode().equals(MobileStatus.AUDITED.name())) {
					map.put("error", "已审核的单据不能删除");
				} else {
					workOvertimeDao.delete(id);
					map.put("success", true);
					flag = true;
				}
			} else {
				map.put("error", "单据不存在，可能已被删除");
			}
		}
		if (whichpage.equals(MobileStatus.SIGNAPP.getValue())) {
			SignApp si = signAppDao.findOne(id);
			if (si != null) {
				if (si.getStatuscode().equals(MobileStatus.AUDITED.name())) {
					map.put("error", "已审核的单据不能删除");
				} else {
					signAppDao.delete(id);
					map.put("success", true);
					flag = true;
				}
			} else {
				map.put("error", "单据不存在，可能已被删除");
			}
		}
		if (flag) {
			List<ApprovalLog> logs = approvalLogDao.getByKeyvalueAndCaller(id, caller);
			if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
				for (ApprovalLog log : logs) {
					approvalLogDao.delete(log.getId());
				}
			}
			List<AppFlowOperation> operations = appFlowOperationDao.findByKeyvalue(id);
			if (!CollectionUtils.isEmpty(operations)) {
				for (AppFlowOperation op : operations) {
					appFlowOperationDao.delete(op.getId());
				}
			}
		}
		return map;
	}

	@Override
	public ModelMap resubmitDocuments(Long id, Integer whichpage) {
		ModelMap map = new ModelMap();
		if (whichpage.equals(MobileStatus.FEEPLEASE.getValue())) {
			FeePlease fee = feePleaseDao.findOne(id);
			if (fee != null) {
				if (fee.getFp_statuscode().equals(MobileStatus.COMMITED.name())) {// 已提交的单据才能反提交
					List<ApprovalLog> logs = approvalLogDao.getBySourceid(id);
					if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
						for (ApprovalLog log : logs) {
							approvalLogDao.delete(log.getId());
						}
					}
					List<AppFlowOperation> operations = appFlowOperationDao.findBySourceidAndCaller(id,
							MobileStatus.FEEPLEASE.getPhrase());
					if (!CollectionUtils.isEmpty(operations)) {
						for (AppFlowOperation op : operations) {
							appFlowOperationDao.delete(op.getId());
						}
					}
					fee.setFp_status(MobileStatus.ENTERING.getPhrase());
					fee.setFp_statuscode(MobileStatus.ENTERING.name());
					fee.setFp_recorddate(new Date());
					feePleaseDao.save(fee);
					map.put("success", true);
				} else {
					map.put("error", "只能对提交状态的单据进行反提交");
				}
			}

		}
		if (whichpage.equals(MobileStatus.VACATION.getValue())) {
			Vacation va = vacationDao.findOne(id);
			if (va != null) {
				if (va.getVa_statuscode().equals(MobileStatus.COMMITED.name())) {// 已提交的单据才能反提交
				List<ApprovalLog> logs = approvalLogDao.getBySourceid(id);
				if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
					for (ApprovalLog log : logs) {
						approvalLogDao.delete(log.getId());
					}
				}
				List<AppFlowOperation> operations = appFlowOperationDao.findBySourceidAndCaller(id,
						MobileStatus.VACATION.getPhrase());
				if (!CollectionUtils.isEmpty(operations)) {
					for (AppFlowOperation op : operations) {
						appFlowOperationDao.delete(op.getId());
					}
				}
				va.setVa_status(MobileStatus.ENTERING.getPhrase());
				va.setVa_statuscode(MobileStatus.ENTERING.name());
				va.setVa_date(new Date());
				vacationDao.save(va);
				map.put("success", true);
				} else {
					map.put("error", "只能对提交状态的单据进行反提交");
				}
			}
		}
		if (whichpage.equals(MobileStatus.WORKDAILY.getValue())) {
			WorkDaily wo = workDailyDao.findOne(id);
			if (wo != null) {
				if (wo.getWd_statuscode().equals(MobileStatus.COMMITED.name())) {// 已提交的单据才能反提交
				List<ApprovalLog> logs = approvalLogDao.getBySourceid(id);
				if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
					for (ApprovalLog log : logs) {
						approvalLogDao.delete(log.getId());
					}
				}
				List<AppFlowOperation> operations = appFlowOperationDao.findBySourceidAndCaller(id,
						MobileStatus.WORKDAILY.getPhrase());
				if (!CollectionUtils.isEmpty(operations)) {
					for (AppFlowOperation op : operations) {
						appFlowOperationDao.delete(op.getId());
					}
				}
				wo.setWd_status(MobileStatus.ENTERING.getPhrase());
				wo.setWd_statuscode(MobileStatus.ENTERING.name());
				wo.setWd_date(new Date());
				workDailyDao.save(wo);
				map.put("success", true);
				} else {
					map.put("error", "只能对提交状态的单据进行反提交");
				}
			}
		}
		if (whichpage.equals(MobileStatus.WORKOVERTIME.getValue())) {
			WorkOvertime wo = workOvertimeDao.findOne(id);
			if (wo != null) {
				if (wo.getWo_statuscode().equals(MobileStatus.COMMITED.name())) {// 已提交的单据才能反提交
				List<ApprovalLog> logs = approvalLogDao.getBySourceid(id);
				if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
					for (ApprovalLog log : logs) {
						approvalLogDao.delete(log.getId());
					}
				}
				List<AppFlowOperation> operations = appFlowOperationDao.findBySourceidAndCaller(id,
						MobileStatus.WORKOVERTIME.getPhrase());
				if (!CollectionUtils.isEmpty(operations)) {
					for (AppFlowOperation op : operations) {
						appFlowOperationDao.delete(op.getId());
					}
				}
				wo.setWo_status(MobileStatus.ENTERING.getPhrase());
				wo.setWo_statuscode(MobileStatus.ENTERING.name());
				wo.setWo_date(new Date());
				workOvertimeDao.save(wo);
				map.put("success", true);
				} else {
					map.put("error", "只能对提交状态的单据进行反提交");
				}
			}
		}

		if (whichpage.equals(MobileStatus.SIGNAPP.getValue())) {
			SignApp app = signAppDao.findOne(id);
			if (app != null) {
				if (app.getStatuscode().equals(MobileStatus.COMMITED.name())) {// 已提交的单据才能反提交
				List<ApprovalLog> logs = approvalLogDao.getBySourceid(id);
				if (!CollectionUtils.isEmpty(logs)) {// 先删除单据产生的审批相关记录
					for (ApprovalLog log : logs) {
						approvalLogDao.delete(log.getId());
					}
				}
				List<AppFlowOperation> operations = appFlowOperationDao.findBySourceidAndCaller(id,
						MobileStatus.SIGNAPP.getPhrase());
				if (!CollectionUtils.isEmpty(operations)) {
					for (AppFlowOperation op : operations) {
						appFlowOperationDao.delete(op.getId());
					}
				}
				app.setStatus(MobileStatus.ENTERING.getPhrase());
				app.setStatuscode(MobileStatus.ENTERING.name());
				signAppDao.save(app);
				map.put("success", true);
				} else {
					map.put("error", "只能对提交状态的单据进行反提交");
				}
			}
		}
		return map;
	}

	@Override
	public ModelMap changeDealMan(Long id, Long emcode, String reason) {
		ModelMap map = new ModelMap();
		AppFlowOperation operation = appFlowOperationDao.findOne(id);
		List<ApprovalLog> logs = approvalLogDao.getBySourceid(operation.getSourceid());
		for (ApprovalLog log : logs) {
			if (log.getNodedealman().equals(operation.getNodedealuu())) {
				log.setNodedealman(emcode);
				log.setNodedealmanname(userBaseInfoDao.findByUserUU(emcode).getUserName());
				log.setNodename(userBaseInfoDao.findByUserUU(emcode).getUserName() + "审批节点");
				approvalLogDao.save(log);
			}
		}
		operation.setStatus(MobileStatus.UNAUDIT.getPhrase());
		operation.setStatuscode(MobileStatus.UNAUDIT.name());
		operation.setNodedealuu(emcode);
		operation.setNodedealname(userBaseInfoDao.findByUserUU(emcode).getUserName());
		operation.setDescription(reason);
		operation.setNodename(userBaseInfoDao.findByUserUU(emcode).getUserName() + "审批节点");
		operation = appFlowOperationDao.save(operation);
		if (operation.getId() != null) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		return map;
	}

}
