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

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

import com.jeesite.modules.admin.apply.entity.AdminAccessApply;
import com.jeesite.modules.admin.apply.service.AdminAccessApplyService;
import com.jeesite.modules.sso.entity.SsoApp;
import com.jeesite.modules.sso.service.SsoAppService;
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;
import java.util.UUID;

/**
 * 应用接入申请表Controller
 * @author yinet
 * @version 2022-06-13
 */
@Controller
@RequestMapping(value = "${adminPath}/admin/adminAccessApply")
public class AdminAccessApplyController extends BaseController {

	@Autowired
	private AdminAccessApplyService adminAccessApplyService;
	@Autowired
	private SsoAppService ssoAppService;


	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AdminAccessApply get(Long id, boolean isNewRecord) {
		return adminAccessApplyService.get(id+"", isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("admin:adminAccessApply:view")
	@RequestMapping(value = {"list", ""})
	public String list(AdminAccessApply adminAccessApply, Model model) {
		// model.addAttribute("adminAccessApply", adminAccessApply);
		if (adminAccessApply.getAccessType().equals("0")) {
			return "modules/admin/apply/adminAccessApplyList";
		} else if (adminAccessApply.getAccessType().equals("1")) {
			return "modules/admin/apply/messageList";
		} else {
			return "modules/admin/apply/serviceList";
		}
	}




	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("admin:adminAccessApply:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AdminAccessApply> listData(AdminAccessApply adminAccessApply, HttpServletRequest request, HttpServletResponse response) {
		adminAccessApply.setPage(new Page<>(request, response));
		Page<AdminAccessApply> page = adminAccessApplyService.findPage(adminAccessApply);
		return page;
	}

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


	/**
	 * 查看编辑表单
	 */
//	@RequiresPermissions("admin:adminAccessApply:view")
	@RequestMapping(value = "/accessForm")
	public String accessForm(AdminAccessApply adminAccessApply, Model model) {
		model.addAttribute("adminAccessApply", adminAccessApply);
		return "modules/access/adminAccessApplyForm";
	}


	/**
	 * 查看消息接入页
	 */
	@RequestMapping(value = "/accessForm1")
	public String accessFormXiaoXi(AdminAccessApply adminAccessApply, Model model) {
		model.addAttribute("adminAccessApply", adminAccessApply);
		return "modules/access/accessApplyForm1";
	}

	/**
	 * 查看服务接入页
	 */
	@RequestMapping(value = "/accessForm2")
	public String accessFormFuWu(AdminAccessApply adminAccessApply, Model model) {
		model.addAttribute("adminAccessApply", adminAccessApply);
		return "modules/access/adminAccessApplyForm1";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("admin:adminAccessApply:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AdminAccessApply adminAccessApply) {



		//如果审核通过在应用表中添加数据
		if(adminAccessApply.getType().equals("1")){
			SsoApp ssoApp=new SsoApp();
			ssoApp.setAppKey(UUID.randomUUID().toString());
			ssoApp.setAppSecretkey(UUID.randomUUID().toString());
			ssoApp.setAppStatus("1");
			ssoApp.setCreateDate(new Date());
			ssoApp.setAppName(adminAccessApply.getAppName());
			ssoApp.setAppType(adminAccessApply.getAppType());
			ssoApp.setCallbackUrl(adminAccessApply.getRequestUrl());
			ssoApp.setReturnAttribute(adminAccessApply.getReturnAttribute());
			ssoApp.setAffiliation(adminAccessApply.getAffiliation());
			ssoApp.setAccess(adminAccessApply.getAccess());
			ssoApp.setAppImage(adminAccessApply.getAppImage());
			ssoApp.setRemarks(adminAccessApply.getAppDescribe());
			ssoAppService.save(ssoApp);
		}

		adminAccessApplyService.save(adminAccessApply);
		return renderResult(Global.TRUE, text("保存应用接入申请表成功！"));
	}



	/**
	 * 保存数据
	 */
	@RequiresPermissions("admin:adminAccessApply:edit")
	@PostMapping(value = "edit")
	@ResponseBody
	public String edit(@Validated AdminAccessApply adminAccessApply) {

		//查询有没有通过
		boolean result=adminAccessApplyService.findByName(adminAccessApply.getAppName());
		if(result==false){
			return renderResult(Global.FALSE, text("应用名已被注册"));
		}
		adminAccessApply.setType("3");
		adminAccessApplyService.save(adminAccessApply);
		return renderResult(Global.TRUE, text("保存应用接入申请表成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("admin:adminAccessApply:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AdminAccessApply adminAccessApply) {
		adminAccessApplyService.delete(adminAccessApply);
		return renderResult(Global.TRUE, text("删除应用接入申请表成功！"));
	}

}
