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

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.hr.entity.UserInfo;
import com.jeeplus.modules.hr.service.UserInfoService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
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.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

/**
 * 人员评价及客户满意信息Controller
 * @author fgy
 * @version 2017-12-06
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "${adminPath}/hr/evaluation")
public class EvaluationController extends BaseController {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserofficeService userofficeService;
	@Autowired
	private UserService userService;

	@ModelAttribute
	public UserInfo get(@RequestParam(value ="id", required=false) String id,
						@RequestParam(value = "userOfficeId", required = false)String userOfficeId ,
						@RequestParam(value = "userId", required = false)String userId,
						@RequestParam(value = "companyId", required = false)String companyId) {
		UserInfo userInfo = new UserInfo();
		Useroffice useroffice = new Useroffice();
		User user = new User();
		if (StringUtils.isNotBlank(id)){
			userInfo = userInfoService.get(id);
		}else{
			if(StringUtils.isNotBlank(userOfficeId)){
				useroffice = userofficeService.get(userOfficeId);
				userInfo.setUserOffice(useroffice);
			}
			if(StringUtils.isNotBlank(userId)){
				user = userService.get(userId);
				userInfo.setUserId(userId);
				userInfo.setUser(user);
			}
			if(StringUtils.isNotBlank(companyId)){
				userInfo.setCompanyId(companyId);
			}
		}
		if(StringUtils.isBlank(userInfo.getGender())){
			userInfo.setGender(user.getSex());
		}
		if(StringUtils.isBlank(userInfo.getName())){
			userInfo.setName(user.getName());
		}
		if(StringUtils.isBlank(userInfo.getMobilePhone())){
			userInfo.setMobilePhone(user.getMobile());
		}
		return userInfo;
	}

	/**
	 * 人员评价及客户满意信息列表页面
	 */
	@RequiresPermissions("hr:evaluation:list")
	@RequestMapping(value = {"list",""})
	public String evaluationList(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserInfo> page = userInfoService.findPage(new Page<UserInfo>(request, response), userInfo);
		model.addAttribute("page", page);

		User user = UserUtils.getUser();
		if(UserUtils.isManager()){
			return "modules/hr/evaluationList";
		}
		return "modules/hr/evaluationListSimple";

	}
	/**
	 * 查看，增加，编辑表单页面
	 */
	@RequiresPermissions(value={"hr:evaluation:view","hr:evaluation:add","hr:evaluation:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(UserInfo userInfo, Model model, HttpServletRequest request) {
		model.addAttribute("userInfo", userInfo);

		if("evaluationEdit".equals(request.getParameter("dialog"))){
			return "modules/hr/evaluationForm";
		}
		if("evaluationView".equals(request.getParameter("dialog"))){
			return "modules/hr/evaluationView";
		}
		return "modules/hr/evaluationView";
	}

	/**
	 * 保存评价表及客户满意表信息
	 */
	@RequiresPermissions(value={"hr:evaluation:add","hr:evaluation:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(UserInfo userInfo, Model model, RedirectAttributes redirectAttributes,
					   @RequestParam(value = "execute_file",required = false)MultipartFile this_upload_files,
					   @RequestParam(value = "manuscript_file",required = false)MultipartFile next_upload_files) throws Exception{
		if (!beanValidator(model, userInfo)){
			return form(userInfo, model,null);
		}

		UserInfo ui = userInfoService.get(userInfo.getId());//从数据库取出记录的值
		userInfoService.saveEvaluationAndSatisfaction(ui,this_upload_files,next_upload_files);//保存评价表或客户满意表

		addMessage(redirectAttributes, "保存人员评价表和客户满意表信息成功");
		return "redirect:"+Global.getAdminPath()+"/hr/evaluation/?repage";
	}


}