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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.mapper.JsonMapper;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
import com.jeeplus.modules.workclientinfo.service.WorkClientInfoService;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractInfo;
import com.jeeplus.modules.workcontractinfo.service.WorkContractInfoService;
import com.jeeplus.modules.workfullmanage.entity.WorkFullManage;
import com.jeeplus.modules.workfullmanage.service.WorkFullManageService;
import com.jeeplus.modules.workvisa.entity.WorkVisa;
import com.jeeplus.modules.workvisa.service.WorkVisaService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 工程签证管理Controller
 * @author
 * @version
 */
@Controller
@RequestMapping(value = "${adminPath}/workvisamanage/workVisaManage")
public class WorkVisaManageController extends BaseController {

	@Autowired
	private WorkFullManageService workFullManageService;
	@Autowired
	private ActTaskService actTaskService;

	@Autowired
	private WorkVisaService workVisaService;
	@ModelAttribute
	public WorkFullManage get(@RequestParam(required=false) String id) {
		WorkFullManage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workFullManageService.get(id);
		}
		if (entity == null){
			entity = new WorkFullManage();
		}
		return entity;
	}
	
	/**
	 * 项目列表页面
	 */
	@RequiresPermissions("workvisamanage:workVisaManage:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkFullManage workFullManage, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(!UserUtils.getSelectRoleInfo()){
			String id = UserUtils.getUser().getId();
			workFullManage.setQueryCon(id);
			workFullManage.setMasterId(UserUtils.getUser().getId());
		}
		Page<WorkFullManage> page = workFullManageService.findPage(new Page<WorkFullManage>(request, response), workFullManage);
		List<WorkFullManage> list = page.getList();

		if(list.size()!=0) {
			for (WorkFullManage w : list) {
				StringBuffer sb =new StringBuffer();
				String hostM = w.getHostMaster();
				String[] hostMaster = hostM.split("\\,");
				for (int i = 0; i < hostMaster.length; i++) {
					String hostMasterName = UserUtils.get(hostMaster[i]).getName();
					if(sb.length()==0){
						sb.append(hostMasterName);
					}else{
						sb.append(","+hostMasterName);
					}
				}
				w.setWorkVisaCount(workVisaService.findCount(w.getId())+"");
				w.setHostMasterName(new String(sb));
			}
		}
		model.addAttribute("page", page);
		return "modules/workvisa/workVisaManageList";
	}
	/**
	 * 查看
	 */
	//@RequiresPermissions(value={"workfullmanage:workFullManage:view"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkFullManage workFullManage, Model model) {
		String view = "workFullManageFormDetail";
		model.addAttribute("workFullManage", workFullManage);
		return "modules/workvisa/"+view;
	}
	/**
	 * 工程签证列表
	 */
	@RequestMapping(value = "workVisaList")
	public String workVisaList(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		String wf=request.getParameter("wf");
		if(wf==null){
			wf=session.getAttribute("wf").toString();
		}
		session.setAttribute("wf",wf);
		WorkFullManage workFullManage = new WorkFullManage();
		workFullManage.setId(wf);
		WorkVisa workVisa = new WorkVisa();
		workVisa.setFullManage(workFullManage);
		Page<WorkVisa> page = workVisaService.findPageByfullMg(new Page<WorkVisa>(request, response),workVisa);
		List<WorkVisa> list = page.getList();
		for (WorkVisa entity :list){
			boolean state = true;
			if(StringUtils.isNotBlank(entity.getProcessInstanceId())){
				state = actTaskService.isProcessEnd(entity.getProcessInstanceId());
			}else{
				state = false;
			}
			entity.setProcessEnded(state);
		}
		model.addAttribute("page", page);
		return "modules/workvisa/workVisaList";
	}

}