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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

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.SignAppDao;
import com.uas.platform.b2b.mobile.dao.SignCardDao;
import com.uas.platform.b2b.mobile.enumeration.MobileStatus;
import com.uas.platform.b2b.mobile.model.AppFlowOperation;
import com.uas.platform.b2b.mobile.model.AppLog;
import com.uas.platform.b2b.mobile.model.ApprovalLog;
import com.uas.platform.b2b.mobile.model.SignApp;
import com.uas.platform.b2b.mobile.model.SignCard;
import com.uas.platform.b2b.mobile.service.SignAppService;
import com.uas.platform.b2b.model.FlowNode;
import com.uas.platform.b2b.service.ApprovalFlowService;

@Service
public class SignAppServiceImpl implements SignAppService {

	@Autowired
	private SignAppDao signAppDao;

	@Autowired
	private ApprovalFlowService approvalFlowService;

	@Autowired
	private ApprovalLogDao approvalLogDao;

	@Autowired
	private AppFlowOperationDao appFlowOperationDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Autowired
	private SignCardDao signCardDao;

	@Override
	public ModelMap saveSignApp(SignApp signapp) {
		ModelMap map = new ModelMap();
		Long id = 0L;
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddss");
		SignApp app = new SignApp();
		if (signapp.getId() == null) {// 新增
			signapp.setCode("MS" + sdf.format(new Date()));
			signapp.setEmname(userBaseInfoDao.findByUserUU(signapp.getEmuu()).getUserName());
			signapp.setStatus(MobileStatus.ENTERING.getPhrase());
			signapp.setStatuscode(MobileStatus.ENTERING.name());
			signapp.setRecorderdate(new Date());
			app = signAppDao.save(signapp);
			id = app.getId();
		} else {// 更新
			app = signAppDao.findOne(signapp.getId());
			app.setAddress(signapp.getAddress());
			app.setCode(signapp.getCode());
			app.setRecorderdate(new Date());
			app.setRemark(signapp.getRemark());
			app.setSigntime(signapp.getSigntime());
			app.setMobile(signapp.getMobile());
			app.setStatus(MobileStatus.ENTERING.getPhrase());
			app.setStatuscode(MobileStatus.ENTERING.name());
			app = signAppDao.save(app);
			id = app.getId();
		}
		// 创建审批流
		String appName = MobileStatus.SIGNAPP.getPhrase();
		FlowNode node = approvalFlowService.getNodeInfoByEnuuAndEmcodeAndAppName(signapp.getUu(), signapp.getEmuu(),
				appName);
		Integer nodelevel = -1;
		if (node != null) {
			nodelevel = node.getNodelev();
		}
		Integer maxNode = approvalFlowService.getMaxNodeByEnuuAndAppName(signapp.getUu(), appName);
		if (maxNode != null) {
			if (nodelevel < maxNode) {
				AppLog log = new AppLog();
				log.setStatus(MobileStatus.ENTERING.getPhrase());
				log.setStatusCode(MobileStatus.ENTERING.name());
				log.setSourceid(id);
				log.setSourcedate(new Date());
				log.setKeyvalue(id);
				log.setCaller(appName);
				log.setEmcode(signapp.getEmuu());
				log.setEnuu(signapp.getUu());
				for (int i = nodelevel + 1; i <= maxNode; i++) {
					ApprovalLog applog = new ApprovalLog(log);
					applog.setNodedealman(approvalFlowService
							.getNodeInfoByEnuuAndAppNameAndNodelev(signapp.getUu(), appName, i).getEmcode());
					applog.setNodename(approvalFlowService
							.getNodeInfoByEnuuAndAppNameAndNodelev(signapp.getUu(), appName, i).getNodename());
					applog.setProcessid(approvalFlowService
							.getNodeInfoByEnuuAndAppNameAndNodelev(signapp.getUu(), appName, i).getId());
					applog = approvalLogDao.save(applog);
					node = approvalFlowService.getNodeInfoByEnuuAndAppNameAndNodelev(signapp.getUu(), appName,
							nodelevel + 1);
					if (applog.getId() != null && applog.getNodedealman()
							.equals(approvalFlowService
									.getNodeInfoByEnuuAndAppNameAndNodelev(signapp.getUu(), appName, nodelevel + 1)
									.getEmcode())) {
						// 新增待审批
						AppFlowOperation operation = new AppFlowOperation();
						operation.setCaller(appName);
						operation.setDealname(signapp.getEmname());
						operation.setDealuu(signapp.getEmuu());
						operation.setKeyvalue(applog.getId());
						operation.setNodedealname(node.getEmname());
						operation.setRecorddate(new Date());
						operation.setStatus(MobileStatus.UNAUDIT.getPhrase());
						operation.setStatuscode(MobileStatus.UNAUDIT.name());
						operation.setEnuu(signapp.getUu());
						operation.setNodedealuu(node.getEmcode());
						operation.setNodelevel(node.getNodelev());
						operation.setSourceid(id);
						operation = appFlowOperationDao.save(operation);
						if (operation.getId() != null) {
							if (id != null) {
								app = signAppDao.findOne(id);
								app.setStatus(MobileStatus.COMMITED.getPhrase());
								app.setStatuscode(MobileStatus.COMMITED.name());
								signAppDao.save(app);
							}
							map.put("success", true);
						} else {
							map.put("success", false);
						}
					}
				}
			}
			if (Objects.equals(nodelevel, maxNode)) {
				if (id != null) {
					app = signAppDao.findOne(id);
					app.setStatus(MobileStatus.AUDITED.getPhrase());
					app.setStatuscode(MobileStatus.AUDITED.name());
					signAppDao.save(app);

					// 插入数据到打卡记录中
					SignCard card = new SignCard();
					card.setCl_address(app.getAddress());
					card.setCl_date(app.getSigntime());
					card.setCl_emname(app.getEmname());
					card.setCl_phone(app.getMobile());
					card.setEmcode(app.getEmuu());
					card.setEnuu(app.getUu());
					card = signCardDao.save(card);
					if (card.getId() != null) {
						map.put("success", true);
					} else {
						map.put("success", false);
					}
				} else {
					map.put("success", false);
				}
			}
		} else {
			map.put("success", "单据保存成功，审批流未设置");
		}
		return map;
	}

}
