package com.jeesite.modules.admin.apply.web;

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

import com.jeesite.modules.admin.apply.entity.AdminAppEvaluate;
import com.jeesite.modules.admin.apply.service.AdminAppEvaluateService;
import com.jeesite.modules.sso.dao.SsoAppDao;
import com.jeesite.modules.sso.entity.SsoApp;
import com.jeesite.modules.sso.service.SsoAppService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;

import java.util.Date;


/**
 * 应用评价表Controller
 * @author yinet
 * @version 2022-06-16
 */
@Controller
@RequestMapping(value = "${adminPath}/admin/adminAppEvaluate")
public class AdminAppEvaluateController extends BaseController {

	@Autowired
	private AdminAppEvaluateService adminAppEvaluateService;
	@Autowired
	private SsoAppService ssoAppService;
	@Autowired
	private SsoAppDao ssoAppDao;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AdminAppEvaluate get(Long id, boolean isNewRecord) {
		return adminAppEvaluateService.get(id+"", isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("admin:adminAppEvaluate:view")
	@RequestMapping(value = {"list", ""})
	public String list(AdminAppEvaluate adminAppEvaluate, Model model) {
		model.addAttribute("adminAppEvaluate", adminAppEvaluate);
		return "modules/admin/evaluate/adminAppEvaluateList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("admin:adminAppEvaluate:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AdminAppEvaluate> listData(AdminAppEvaluate adminAppEvaluate, HttpServletRequest request, HttpServletResponse response) {
		adminAppEvaluate.setPage(new Page<>(request, response));
		Page<AdminAppEvaluate> page = adminAppEvaluateService.findPage(adminAppEvaluate);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("admin:adminAppEvaluate:view")
	@RequestMapping(value = "form")
	public String form(AdminAppEvaluate adminAppEvaluate, Model model) {
		model.addAttribute("adminAppEvaluate", adminAppEvaluate);
		return "modules/admin/evaluate/adminAppEvaluateForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("admin:adminAppEvaluate:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AdminAppEvaluate adminAppEvaluate) {
		adminAppEvaluateService.save(adminAppEvaluate);
		return renderResult(Global.TRUE, text("保存应用评价表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("admin:adminAppEvaluate:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AdminAppEvaluate adminAppEvaluate) {
		adminAppEvaluateService.delete(adminAppEvaluate);
		return renderResult(Global.TRUE, text("删除应用评价表成功！"));
	}


	/**
	 * 前台保存数据
	 */
	@PostMapping(value = "save2")
	@ResponseBody
	public String save2(@Validated AdminAppEvaluate adminAppEvaluate) {
		//获取评价人信息
		/*User user = UserUtils.getUser();
		adminAppEvaluate.setEvaluateUsername(user.getUserName());
		adminAppEvaluate.setEvaluateUser(user.getId());
		//根据appid查询应用信息
		SsoApp ssoApp = ssoAppService.get(adminAppEvaluate.getEvaluateApp());
		adminAppEvaluate.setAppName(ssoApp.getAppName());*/
		adminAppEvaluate.setCreateDate(new Date());
		adminAppEvaluateService.save(adminAppEvaluate);
		return renderResult(Global.TRUE, text("保存应用评价表成功！"));
	}

	@RequestMapping("/evaluateForm")
	public String evaluateForm(String appId, Model model){
		User user = UserUtils.getUser();
		//根据appid查询app的名字
		SsoApp ssoApp = ssoAppDao.getSsoAppById(appId);

		model.addAttribute("appId", appId);
		model.addAttribute("appName", ssoApp.getAppName());
		model.addAttribute("evaluateUsername", user.getUserName());
		model.addAttribute("evaluateMsg", ssoApp.getRemarks());

		return "modules/admin/evaluate/AppEvaluateForm";
	}
	
}