package com.hotent.portal.controller;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.HttpUtil;
import com.hotent.base.util.time.DateFormatUtil;
import com.hotent.portal.model.SysIndexLayoutManage;
import com.hotent.portal.params.IndexLayoutVo;
import com.hotent.portal.persistence.manager.SysIndexColumnManager;
import com.hotent.portal.persistence.manager.SysIndexLayoutManageManager;
import com.hotent.portal.persistence.manager.SysIndexMyLayoutManager;
import com.hotent.sys.persistence.manager.SysAuthUserManager;
import com.hotent.uc.api.service.IUserService;

import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 布局管理 控制器类
 *
 * @company 广州宏天软件股份有限公司
 * @author maoww
 * @email maoww@jee-soft.cn
 * @date 2018年6月14日
 */
@RestController
@RequestMapping("/portal/sysIndexLayoutManage/sysIndexLayoutManage/v1")
@Api(tags="门户布局管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class SysIndexLayoutManageController extends BaseController<SysIndexLayoutManageManager, SysIndexLayoutManage>
{
	@Resource
	private SysIndexLayoutManageManager sysIndexLayoutManageService;
	@Resource
	private SysIndexColumnManager sysIndexColumnService;
	@Resource
	private SysAuthUserManager sysAuthUserManager;
	@Resource
	IUserService ius;
    @Value("${system.saas.enable:false}")
    Boolean saasEnable;
    @Resource
   	SysIndexMyLayoutManager sysIndexMylayoutService;


    @RequestMapping(value="listJson", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "取得布局管理分页列表", httpMethod = "POST", notes = "取得布局管理分页列表")
	public PageList<SysIndexLayoutManage> listJson(
			@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysIndexLayoutManage> queryFilter)throws Exception {
		PageList<SysIndexLayoutManage> res=sysIndexLayoutManageService.listJson(queryFilter);
    	return res;
	}
	/**
	 * 获取系统默认的布局
	 * @param queryFilter
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getSysDefaultLayout", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取系统默认的布局", httpMethod = "POST", notes = "获取系统默认的布局")
	public PageList<SysIndexLayoutManage> getSysDefaultLayout(
			@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysIndexLayoutManage> queryFilter)throws Exception {
		queryFilter.addFilter("IS_DEF",SysIndexLayoutManage.SET_DEF , QueryOP.EQUAL);
		return sysIndexLayoutManageService.query(queryFilter);
	}

	@RequestMapping(value="getByAlias", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "通过别名获取布局信息", httpMethod = "GET", notes = "通过别名获取布局信息")
	public @ResponseBody List<SysIndexLayoutManage> getByAlias(@ApiParam(name="alias",value="布局别名")@RequestParam List<String> alias)throws Exception {
		Assert.isTrue(BeanUtils.isNotEmpty(alias), "布局别名不能为空");
		// 获取管理员的默认布局，添加adminLayout属性，当用户获取默认别名时获取
		SysIndexLayoutManage defaultFront = sysIndexLayoutManageService.getDataByAlias("default_front",true);
		List<SysIndexLayoutManage> layoutByAlias = sysIndexLayoutManageService.getDataByAlias(alias, false);
		if (BeanUtils.isNotEmpty(defaultFront)&& !layoutByAlias.isEmpty()){
			layoutByAlias.get(0).setAdminLayout(defaultFront.getLayoutConfig());
		}
		return layoutByAlias;
	}

	@RequestMapping(value="getJson", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "通过id取得布局管理分页列表", httpMethod = "GET", notes = "通过id取得布局管理分页列表")
	public @ResponseBody SysIndexLayoutManage getJson(@ApiParam(name="id",value="主键id")@RequestParam String id
	)throws Exception {
		return sysIndexLayoutManageService.get(id);
	}

	@RequestMapping(value="remove", method=RequestMethod.DELETE, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "删除布局管理", httpMethod = "DELETE", notes = "删除布局管理")
	public CommonResult<String> remove(@ApiParam(name="ids", value="主键", required = true)@RequestParam String ids) throws Exception {
		CommonResult<String> res=sysIndexLayoutManageService.delete(ids);
		return res;
	}

	@RequestMapping(value="design", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "设计首页布局", httpMethod = "GET", notes = "设计首页布局")
	public Map<String,Object> design(
			@ApiParam(name="id", value="主键", required = false)@RequestParam String id,
			@ApiParam(name="layoutType", value="部门id", required = false)@RequestParam short layoutType) throws Exception {
		Map<String,Object> res=sysIndexLayoutManageService.design(id,layoutType);
		return res;
	}


//	@RequestMapping(value="changDefault", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "根据ID获取内容", httpMethod = "GET", notes = "根据ID获取内容")
//	public CommonResult<String> changDefault(@ApiParam(name="id", value="主键", required = true)@RequestParam String id,
//							 @ApiParam(name="action" , value="动作", required = true)@RequestParam String action,
//							 @ApiParam(name="orgId" , value="组织id", required = true)@RequestParam String orgId,
//							 @ApiParam(name="layoutType" , value="布局类型", required = true)@RequestParam short layoutType
//	) throws Exception {
//		String message = null;
//		try {
//			SysIndexLayoutManage sysIndexLayoutManage = sysIndexLayoutManageService.get(id);
//			if("set".equals(action)){
//				sysIndexLayoutManageService.cancelOrgIsDef(orgId,layoutType);
//				sysIndexLayoutManage.setIsDef(SysIndexLayoutManage.SET_DEF);
//				message="设置默认成功";
//			}else{
//				sysIndexLayoutManage.setIsDef(SysIndexLayoutManage.CANCEL_DEF);
//				message="取消默认成功";
//			}
//			sysIndexLayoutManageService.update(sysIndexLayoutManage);
//			return new CommonResult<>(true, message, null);
//		} catch (Exception e) {
//			e.printStackTrace();
//			if("set".equals(action)){
//				message="设置默认失败";
//			}else{
//				message="取消默认失败";
//			}
//			return new CommonResult<>(false, message, null);
//		}
//	}

//	@RequestMapping(value="getSysIndexLayouts", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "获取系统首页布局实体", httpMethod = "GET", notes = "获取系统首页布局实体")
//	public List<SysIndexLayout> getSysIndexLayout() throws Exception {
//		//首页布局
//		List<SysIndexLayout> layoutList = sysIndexLayoutService.getAll();
//		// 解码   columnMap 在sysIndexColumnManager.getColumnMap 中解码
//		for (SysIndexLayout sysIndexLayout : layoutList) {
//			sysIndexLayout.setTemplateHtml(Base64.getFromBase64(sysIndexLayout.getTemplateHtml()));
//		}
//		return layoutList;
//	}

//	@RequestMapping(value="getSysIndexColumns", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "获取首页栏目", httpMethod = "GET", notes = "获取首页栏目")
//	public Map<String, List<SysIndexColumn>> getSysIndexColumns(@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter filter,
//								   								@ApiParam(name="paramsJson",value="请求参数")@RequestParam String paramsJson,
//								   								@ApiParam(name="type",value="类型参数")@RequestParam String type
//	) throws Exception {
//		//获取当前用户
//		IUser user = ius.getUserByAccount(current());
//		short layoutType = 0;
//		if(type.equals("pc")){
//			layoutType = SysIndexColumn.TYPE_PC;
//		}else{
//			layoutType = SysIndexColumn.TYPE_MOBILE;
//		}
//		// 不限制取值
//		PageBean page = filter.getPageBean();
//		page.setPageSize(PageBean.WITHOUT_PAGE);
//		filter.setPageBean(page);
//		Map<String,Object>  params  =  JsonUtil.toMap(paramsJson);
//		filter.addFilter("IS_PUBLIC", layoutType, QueryOP.EQUAL);
//		//首页栏目，取出来需要解析
//		List<SysIndexColumn>  columnList = sysIndexColumnService.getHashRightColumnList(filter, params, true, layoutType, user);
//		//获取展示的布局
//		Map<String,List<SysIndexColumn>> columnMap = sysIndexColumnService.getColumnMap(columnList);
//		return columnMap;
//	}

//	@RequestMapping(value="getSysIndexLayoutManage", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "获取当前的布局", httpMethod = "GET", notes = "获取当前的布局")
//	public SysIndexLayoutManage getSysIndexLayoutManage(
//										@ApiParam(name="paramsJson",value="请求参数")@RequestParam String paramsJson,
//										@ApiParam(name="type",value="类型参数")@RequestParam String type,
//										@ApiParam(name="id", value="主键", required = true)@RequestParam String id
//	) throws Exception {
//		//获取当前用户
//		IUser user = ius.getUserByAccount(current());
//		short layoutType = 0;
//		if(type.equals("pc")){
//			layoutType = SysIndexColumn.TYPE_PC;
//		}else{
//			layoutType = SysIndexColumn.TYPE_MOBILE;
//		}
//		QueryFilter filter = QueryFilter.build();
//		// 不限制取值
//		PageBean page = new PageBean();
//		page.setPageSize(PageBean.WITHOUT_PAGE);
//		filter.setPageBean(page);
//		Map<String,Object>  params  =  JsonUtil.toMap(paramsJson);
//		filter.addFilter("IS_PUBLIC", layoutType, QueryOP.EQUAL);
//		//首页栏目，取出来需要解析
//		List<SysIndexColumn>  columnList = sysIndexColumnService.getHashRightColumnList(filter, params, true, layoutType, user);
//		//获取当前的布局
//		SysIndexLayoutManage sysIndexLayoutManage = sysIndexLayoutManageService.getLayoutList(id, columnList, layoutType);
//		return sysIndexLayoutManage;
//	}


//	@RequestMapping(value="getDefaultLayout", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "获取首页布局模板（默认，base64编码）", httpMethod = "GET", notes = "获取首页布局模板（默认，base64编码）")
//	public CommonResult<String> getDefaultLayout() throws Exception {
//		QueryFilter filter = QueryFilter.build().withDefaultPage();
//		filter.addFilter("IS_DEF", 1,QueryOP.EQUAL );
//		PageList<SysIndexLayoutManage> page = sysIndexLayoutManageService.query(filter);
//		try {
//			if(page.getTotal()>0){
//				return new CommonResult<String>(true,"获取布局成功！",page.getRows().get(0).getDesignHtml());
//			}
//		} catch (Exception e) {
//			return new CommonResult<String>(false,"获取布局失败！","");
//		}
//
//		return new CommonResult<String>(false,"未设置首页布局！","");
//	}

	@RequestMapping(value = "saveLayout", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "保存首页布局", httpMethod = "POST", notes = "保存首页布局")
	public CommonResult<String> saveLayout(@ApiParam(name="indexLayout", value="数据源对象", required = true) @RequestBody SysIndexLayoutManage indexLayout)
	throws Exception {
		CommonResult<String> res=sysIndexLayoutManageService.saveLayout(indexLayout);
		return res;
	}

//	@RequestMapping(value="dialog", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "选择首页布局模版", httpMethod = "POST", notes = "选择首页布局模版")
//	public List<SysIndexLayoutManage> dialog() throws Exception {
//		List<SysIndexLayoutManage> list= new ArrayList<SysIndexLayoutManage>();
//		return list;
//	}

//	@RequestMapping(value="getLayoutDef", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
//	@ApiOperation(value = "根据组织id和布局类型获取布局管理", httpMethod = "GET", notes = "根据组织id和布局类型获取布局管理")
//	public CommonResult<String> getLayoutDef(@ApiParam(name="orgId", value="组织id", required = true)@RequestParam String orgId,
//											 @ApiParam(name="layoutType", value="布局类型", required = true)@RequestParam Short layoutType
//	) throws Exception {
//		boolean isMaster = false;
//		if (StringUtil.isNotEmpty(orgId)) {
//			SysIndexLayoutManage sysIndexLayoutManage = sysIndexLayoutManageService.getByOrgIdAndLayoutType(orgId,layoutType);
//			if(BeanUtils.isNotEmpty(sysIndexLayoutManage)){
//				isMaster = true;
//			}
//		}
//		return new CommonResult<>(isMaster, "", null);
//	}

	@RequestMapping(value="setShareToSub", method=RequestMethod.GET)
	@ApiOperation(value = "设置是否共享给子部门", httpMethod = "GET", notes = "设置是否共享给子部门")
	public CommonResult<String> setShareToSub(@ApiParam(name="id", value="主键", required = true)@RequestParam String id,@ApiParam(name="shareToSub", required = true)@RequestParam Short shareToSub) throws Exception {
		try{
			SysIndexLayoutManage layout = sysIndexLayoutManageService.get(id);
			layout.setShareToSub(shareToSub);
			sysIndexLayoutManageService.update(layout);
			return new CommonResult<>(true, "操作成功", null);
		}catch(Exception ex){
			return new CommonResult<>(false, "操作失败" + ex.getMessage(), null);
		}
	}

	@RequestMapping(value="enable", method=RequestMethod.GET)
	@ApiOperation(value = "设置启用/停用", httpMethod = "GET", notes = "设置启用/停用")
	public CommonResult<String> enable(@ApiParam(name="id", value="主键", required = true)@RequestParam String id,@ApiParam(name="enable", required = true)@RequestParam Short enable) throws Exception {
		sysIndexLayoutManageService.setEnable(id, enable);
		return new CommonResult<>(true, "操作成功", null);
	}

	@RequestMapping(value="asTemplate", method=RequestMethod.GET)
	@ApiOperation(value = "", httpMethod = "GET", notes = "设置启用/停用")
	public CommonResult<String> asTemplate(@ApiParam(name="id", value="主键", required = true)@RequestParam String id,@ApiParam(name="asTemplate", required = true)@RequestParam Short asTemplate) throws Exception {
		sysIndexLayoutManageService.setAsTemplate(id, asTemplate);
		return new CommonResult<>(true, "操作成功", null);
	}

	@PostMapping(value = "exportDefaultLayout", produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "根据id集合导出布局", httpMethod = "POST", notes = "根据id集合导出布局")
	public void exportDefaultLayout(@ApiParam(name = "ids", value = "id集合") @RequestBody List<String> ids,
							 HttpServletRequest request , HttpServletResponse response, @RequestParam boolean isDefault) throws Exception{
		String filePrefix;
		if(isDefault){
			filePrefix = "defaultLayout";
		}else{
			filePrefix = "orgLayout";
		}
		String fileName = filePrefix + "_" + DateFormatUtil.format(LocalDateTime.now(),"yyyy_MM_dd_HHmm");
		String json = baseService.exportDefaultLayout(ids,isDefault);
		HttpUtil.downLoadFile(request,response,json,filePrefix+".json",fileName);
	}

	@PostMapping(value = "importDefaultLayout", produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "导入布局", httpMethod = "POST", notes = "导入布局")
	public CommonResult<String> importDefaultLayout(@RequestParam String cacheFileId, @RequestParam String orgId, @RequestParam boolean isDefault) throws Exception{
		return sysIndexLayoutManageService.importDefaultLayout(cacheFileId,orgId,isDefault);
	}

	@PostMapping(value = "importDefaultLayoutCheck", produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "检查导入布局是否重复", httpMethod = "POST", notes = "检查导入布局是否重复")
	public CommonResult<String> importDefaultLayoutCheck(MultipartHttpServletRequest request, @RequestParam String orgId, @RequestParam boolean isDefault) {
		return sysIndexLayoutManageService.importDefaultLayoutCheck(request, orgId, isDefault);
	}

	@RequestMapping(value="checkKey", method=RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "检查别名是否存在", httpMethod = "GET", notes = "检查别名是否存在")
	public boolean checkKey(@ApiParam(name="key",value="表单key") @RequestParam String key) throws Exception{
		List<SysIndexLayoutManage> layouts =baseService.getByAlias(key);
		return  layouts.size()>0;
	}


	@RequestMapping(value="saveCopy", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "复制布局信息", httpMethod = "POST", notes = "复制布局信息")
	public CommonResult saveCopy(@ApiParam(name="layout",value="布局数据对象")@RequestBody SysIndexLayoutManage layout) throws Exception{
		CommonResult res=sysIndexLayoutManageService.saveCopy(layout);
		return res;
	}

	@RequestMapping(value="getCurrentAuthLayout", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前用户有权限门户(包含系统默认门户)", httpMethod = "GET", notes = "获取当前用户有权限门户(包含系统默认门户)")
	public List<IndexLayoutVo> getCurrentAuthLayout(@ApiParam(name="from", value="来源（manage、front、mobile）", required = true) @RequestParam Optional<String> from){
		List<IndexLayoutVo> res=sysIndexLayoutManageService.getCurrentAuthLayout(from);
		return res;
	}

	@PostMapping(value = "importDatav", produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "导入大屏", httpMethod = "POST", notes = "导入大屏")
	public CommonResult importDatav(MultipartHttpServletRequest request, @RequestParam String orgId, @RequestParam boolean isDefault) throws Exception{
		CommonResult res = sysIndexLayoutManageService.importDatav(request,orgId,isDefault);
		return res;
	}

	@RequestMapping(value="confirmImportData", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "导入大屏", httpMethod = "POST", notes = "导入大屏")
	public CommonResult<String> confirmImportData(@ApiParam(name="unZipFilePath",value="unZipFilePath", required = true)@RequestParam String unZipFilePath,
											@ApiParam(name="status",value="status", required = true)@RequestParam String status) throws Exception{
		CommonResult<String> res=sysIndexLayoutManageService.confirmImportData(unZipFilePath,status);
		return res;
	}
}
