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

import com.google.common.base.Strings;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.service.WorkClientInfoService;
import com.jeeplus.modules.workfullmanage.entity.WorkFullManage;
import com.jeeplus.modules.workfullmanage.service.WorkFullManageService;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 全过程管理Controller
 * @author mapengbo
 * @version 2017-10-24
 */
@Controller
@RequestMapping(value = "${adminPath}/workfullmanage/workMonitor")
public class WorkFullMonitorController extends BaseController {

	@Autowired
	private WorkFullManageService workFullManageService;

	@Autowired
	private WorkClientInfoService workClientInfoService;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private SystemService systemService;


	
	@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("workfullmanage:workMonitor:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkFullManage workFullManage, HttpServletRequest request, HttpServletResponse response, Model model) {
		/*List<String> roleIdList = UserUtils.getUser().getRoleIdList();
		if(roleIdList.size() == 1){ //多个角色不处理
			Role role = UserUtils.getSelectRole();
			if("8".equals(role.getDataScope()) || "4".equals(role.getDataScope())){
				String id = UserUtils.getUser().getId();
				workFullManage.setQueryCon(id);
				workFullManage.setMasterId(UserUtils.getUser().getId());
			}
		}*/
		if(!UserUtils.getSelectRoleInfo()){
			String id = UserUtils.getUser().getId();
			workFullManage.setQueryCon(id);
			workFullManage.setMasterId(UserUtils.getUser().getId());
		}
		//查询项目归档的数据
//		workFullManage.setFullProjectStatus("5");
		//添加只查询当前公司的数据  no
		Page<WorkFullManage> page = workFullManageService.findPage(new Page<WorkFullManage>(request, response), workFullManage);
		List<WorkFullManage> list = page.getList();
		for (WorkFullManage workFull:list){
			if(!Strings.isNullOrEmpty(workFull.getHostMaster())){
				String[] split = workFull.getHostMaster().split(",");
				String hostMasterName = workFullManageService.getPeople(split);
				workFull.setHostMasterName(hostMasterName);
			}
			if(!Strings.isNullOrEmpty(workFull.getFollowMaster())){
				String[] split1 = workFull.getFollowMaster().split(",");
				String followMasterName = workFullManageService.getPeople(split1);
				workFull.setFollowMasterName(followMasterName);
			}
			if(!Strings.isNullOrEmpty(workFull.getHostMember())){
				String[] split2 = workFull.getHostMember().split(",");
				String hostMemberName = workFullManageService.getPeople(split2);
				workFull.setHostMemberName(hostMemberName);
			}
			if(!Strings.isNullOrEmpty(workFull.getFollowMember())){
				String[] split3 = workFull.getFollowMember().split(",");
				String followMemberName = workFullManageService.getPeople(split3);
				workFull.setFollowMemberName(followMemberName);
			}
		}
		page.setList(list);
		model.addAttribute("page", page);
		return "modules/workfullmanage/workFullMonitorList";
	}

	/**
	 * 查看，增加，编辑项目登记表单页面
	 */
//	@RequiresPermissions(value={"workfullmanage:workMonitor:view","workfullmanage:workMonitor:add","workfullmanage:workMonitor:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkFullManage workFullManage, Model model) {
		model.addAttribute("workFullManage", workFullManage);
		return "modules/workfullmanage/workFullRecordForm";
	}

	/**
	 * 修改项目状态
	 * @param workFullManage
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("/updStatusById")
	public String updStatusById(WorkFullManage workFullManage,RedirectAttributes redirectAttributes){
		String status = request.getParameter("status");
		workFullManage.setFullProjectStatus(status); //将项目状态改为项目准备
		int i = workFullManageService.updStatusById(workFullManage);
		String s = (i > 0) ? "保存成功" : "保存失败";
		addMessage(redirectAttributes, s);
		return "redirect:"+Global.getAdminPath()+"/workfullmanage/workMonitor/?repage";
	}

}