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

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

import com.google.common.collect.Maps;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.advence.activiti.entity.Advance;
import com.jeeplus.modules.sg.advence.activiti.service.AdvanceService;
import com.jeeplus.modules.sg.managementcenter.activiti.service.ConstructionService;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.managementcenter.project.service.ProjectService;
import com.jeeplus.modules.sg.picking.activiti.entity.PickApplication;
import com.jeeplus.modules.sg.picking.activiti.service.ShowListService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;

/**
 * 表单Controller
 * @author lgf
 * @version 2018-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/advance/project")
public class AdvanceController extends BaseController {

	@Autowired
	private AdvanceService advanceService;

	@Autowired
	private ActTaskService actTaskService;

	@Autowired
	private ActProcessService actProcessService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private ShowListService showListService;

	@Autowired
	private ConstructionService constructionService;

	@ModelAttribute
	public Advance get(@RequestParam(required=false) String id) {
		Advance entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = advanceService.get(id);
		}
		if (entity == null){
			entity = new Advance();
		}
		return entity;
	}
	
	/**
	 * 工程管理表单列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Advance advance, Model model) {
		model.addAttribute("advance", advance);
		return "modules/sg/managementcenter/project/projectList";
	}

	    /**
     * 提前施工
     */
    @RequestMapping(value = "constructionAdvanceForm")
    public String constructionAdvanceForm(Advance advance, Model model) {
        model.addAttribute("advance", advance);
        return "modules/sg/advence/activiti/constructionAdvanceForm";
    }
	/**
	 * 项目经理审核
	 * 政策处理专职发起项目，交与项目经理审核页面
	 */
	@RequestMapping(value = "constructionManager")
	public String applicationAudit(PickApplication pickApplication, Model model) {
		String id = pickApplication.getId();
		String projectId = showListService.selectProjectId(id);
//		Project project1 = projectService.get(projectId);
		model.addAttribute("pickApplication", pickApplication);
//		model.addAttribute("project", project1);
		return "modules/sg/advence/activiti/constructionManager";
	}

	/**
	 * 提前施工
	 */
	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxJson save(PickApplication pickApplication, Model model) throws Exception {
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(pickApplication);
		if (StringUtils.isNotBlank(errMsg)) {
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		String projectId = pickApplication.getProjectId();
		if (projectId == null||"".equals(projectId)) {
			j.setSuccess(false);
			j.setMsg("请选择项目");
			return j;
		}
		showListService.newSave(pickApplication);
		// 启动流程
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("noPass", false);
		vars.put("pass", false);
		String loginName = UserUtils.getUser().getLoginName();
		//判断登录的人员是什么角色，决定是否发送流程是否需要
		List<String> strings = constructionService.nameFindOffice(loginName);
		boolean flag = false;
		for (String a :strings) {
			if ("施工经理".equals(a)) {
				flag = true;
			}
		}
		if (flag) {
			vars.put("noPass", true);
			Project project = projectService.get(pickApplication.getProjectId());
			Project assByName = projectService.findAssByName(project.getReserveProjectName());
			vars.put("manager", UserUtils.getByUserName(assByName.getAssignment().getOwnerManager()).getLoginName());
			j.setMsg("发起流程审批成功!");
		}else {
			vars.put("pass", true);
			j.setMsg("发起提前施工成功!");
		}
		Advance advance = new Advance();
		advance.setId(pickApplication.getProjectId());
		advance.setPreConstruction(1);
		//修改项目提前施工状态
		advanceService.updatePreConstruction(advance);
		ProcessDefinition p = actProcessService.getProcessDefinition(pickApplication.getAct().getProcDefId());
		String title = pickApplication.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
		actTaskService.startProcess(p.getKey(), "md_picking_application", pickApplication.getId(), title, vars);
		j.getBody().put("targetUrl", "/act/task/process/");
		return j;
	}
}