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

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.work.entity.report.WorkReport;
import com.jeeplus.modules.work.entity.review.WorkReportReview;
import com.jeeplus.modules.work.service.review.WorkReportReviewService;

/**
 * 工作报告的评论与回复Controller
 * @author 许凯
 * @version 2017-04-28
 */
@Controller
@RequestMapping(value = "${adminPath}/work/review/workReportReview")
public class WorkReportReviewController extends BaseController {

	@Autowired
	private WorkReportReviewService workReportReviewService;
	
	@ModelAttribute
	public WorkReportReview get(@RequestParam(required=false) String id) {
		WorkReportReview entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workReportReviewService.get(id);
		}
		if (entity == null){
			entity = new WorkReportReview();
		}
		return entity;
	}
	
	/**
	 * 工作报告的评论与回复列表页面
	 */
	@RequiresPermissions("work:review:workReportReview:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkReportReview workReportReview, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		String companyId = user.getComId();
		Page<WorkReportReview> page = workReportReviewService.findPageByCompany(new Page<WorkReportReview>(request,response),workReportReview,companyId);
		model.addAttribute("page", page);
		return "modules/work/review/workReportReviewListBy";
	}
	
	
	/**
	 * 
	 */
	@RequestMapping(value = "listBy")
	public String listBy(WorkReportReview workReportReview, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkReportReview> page = workReportReviewService.findListByReportId(new Page<WorkReportReview>(request, response), workReportReview); 
		model.addAttribute("page", page);
		return "modules/work/review/workReportReviewListBy";
	}

	/**
	 * 查看，增加，编辑工作报告的评论与回复表单页面
	 */
	@RequiresPermissions(value={"work:review:workReportReview:view","work:review:workReportReview:add","work:review:workReportReview:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(@RequestParam(value = "readAttr",required = false)String readAttr,WorkReportReview workReportReview, Model model) {
		if (workReportReview.getParent()!=null && StringUtils.isNotBlank(workReportReview.getParent().getId())){
			workReportReview.setParent(workReportReviewService.get(workReportReview.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(workReportReview.getId())){
				WorkReportReview workReportReviewChild = new WorkReportReview();
				workReportReviewChild.setParent(new WorkReportReview(workReportReview.getParent().getId()));
				List<WorkReportReview> list = workReportReviewService.findList(workReportReview); 
				if (list.size() > 0){
					workReportReview.setSort(list.get(list.size()-1).getSort());
					if (workReportReview.getSort() != null){
						workReportReview.setSort(workReportReview.getSort() + 30);
					}
				}
			}
		}
		if (workReportReview.getSort() == null){
			workReportReview.setSort(30);
		}
		model.addAttribute("workReportReview", workReportReview);
		model.addAttribute("disabled",(readAttr==null||"".equals(readAttr))?"false":"true");
		return "modules/work/review/workReportReviewForm";
	}

	/**
	 * 保存工作报告的评论与回复
	 */
	@RequiresPermissions(value={"work:review:workReportReview:add","work:review:workReportReview:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WorkReportReview workReportReview, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, workReportReview)){
			return form("",workReportReview, model);
		}
		if(!workReportReview.getIsNewRecord()){//编辑表单保存
			WorkReportReview t = workReportReviewService.get(workReportReview.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(workReportReview, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			workReportReviewService.save(t);//保存
		}else{//新增表单保存
			workReportReviewService.save(workReportReview);//保存
		}
		addMessage(redirectAttributes, "保存工作报告的评论与回复成功");
		return "redirect:"+Global.getAdminPath()+"/work/review/workReportReview/?repage";
	}
	
	/**
	 * 删除工作报告的评论与回复
	 */
	@RequiresPermissions("work:review:workReportReview:del")
	@RequestMapping(value = "delete")
	public String delete(WorkReportReview workReportReview, RedirectAttributes redirectAttributes) {
		workReportReviewService.delete(workReportReview);
		addMessage(redirectAttributes, "删除工作报告的评论与回复成功");
		return "redirect:"+Global.getAdminPath()+"/work/review/workReportReview/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<WorkReportReview> list = workReportReviewService.findList(new WorkReportReview());
		for (int i=0; i<list.size(); i++){
			WorkReportReview e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getContent());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}