package com.app.admin.framework.system.module.controller;

import com.app.admin.framework.system.module.convert.SysModuleTreeConvert;
import com.app.admin.framework.system.module.model.SysModule;
import com.app.admin.framework.system.module.service.SysModuleService;
import com.app.admin.framework.system.module.view.SysModuleTreeNode;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.controller.CrudBaseControllerSupport;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.grid.Paging;
import com.app.framework.core.utils.request.SessionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单资源管理
 */
@Controller
@RequestMapping("/sys/module")
public class SysModuleController extends CrudBaseControllerSupport<SysModule> {

	@Autowired
	SysModuleService sysModuleService;

	/**
	 * 查询列表的具体实现
	 * @param request	HttpServletRequest
	 * @param view		查询条件
	 * @param paging	分页条件
	 * @return 查询结果
	 */
	@Override
	public List<SysModule> list(HttpServletRequest request, SysModule view, Paging paging){
		LoginUser loginUser = SessionUtil.getCurrentUser(request);
		view.setOrganizationId(loginUser.getOrganizationId());
		return sysModuleService.getPageList(view, paging);
	}
	
	/**
	 * 保存记录的具体实现
	 * @param request	HttpServletRequest
	 * @param view	实体
	 * @return	View对象
	 */
	@Override
	public AjaxResult saveEntity(HttpServletRequest request, SysModule view){
		String[] mtypeBtn = request.getParameterValues("mtypeBtn");
		sysModuleService.saveModule(view, mtypeBtn);
		return AjaxResult.wrap(true);
	}
	
	/**
	 * 删除记录,根据Id 具体实现
	 * @param request	HttpServletRequest
	 * @param oids	实体
	 */
	@Override
	public AjaxResult batchDelete(HttpServletRequest request, Long oids[]){
		boolean b = sysModuleService.removeByIds(Arrays.stream(oids).collect(Collectors.toList()));
		return AjaxResult.wrap(b);
	}
	
	@RequestMapping(value = "/main")
	public ModelAndView main(HttpServletRequest request) {
		return new ModelAndView("admin/framework/system/module/main.html");
	}
	
	/**
     * 跳转到编辑页面
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request, SysModule entity) {
    	HashMap<String, Object> map = new HashMap<String, Object>();
    	if(entity.getOid() != null){
    		entity = sysModuleService.getById(entity.getOid());
    		if(StringUtils.isNotBlank(entity.getParams())){
    			String params = entity.getParams().replace(" ", "&nbsp;").replace("\"", "&quot;");
    			entity.setParams(params);
    		}
    		if(StringUtils.isNotBlank(entity.getVisiturl())){
    			String visiturl = entity.getVisiturl().replace(" ", "&nbsp;").replace("\"", "&quot;");
    			entity.setVisiturl(visiturl);
    		}
    	}
    	if(entity.getParentId() != null){
    		SysModule parent = sysModuleService.getById(entity.getParentId());
    		map.put("parent", parent);
    	}
    	map.put("entity", entity);
        return new ModelAndView("admin/framework/system/module/edit.html", map);
    }
	
	@RequestMapping(value = "/updateStatus")
    public @ResponseBody AjaxResult updateStatus(Long oid, Integer state) {
		SysModule entity = new SysModule();
		entity.setOid(oid);
		entity.setState(state);;
		sysModuleService.updateById(entity);
        return AjaxResult.wrap(true);
    }
	
	/**
     * 模块树
     */
    @RequestMapping(value = "/treeList")
    public @ResponseBody List<SysModuleTreeNode> tree(HttpServletRequest request, SysModule view) {
		LoginUser loginUser = SessionUtil.getCurrentUser(request);
		view.setState(StateEnum.NORMAL.getValue());
		view.setMtype(1);//菜单
		view.setOrganizationId(loginUser.getOrganizationId());
    	List<SysModule> list = sysModuleService.getPageList(view, null);
        return new SysModuleTreeConvert().transStaticTree(list);
    }
}
