package com.hotent.portal.controller;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.context.BaseContext;
import com.hotent.base.controller.BaseController;
import com.hotent.base.exception.MessagePassingException;
import com.hotent.base.jms.JmsProducer;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.sys.persistence.manager.DataDictManager;
import com.hotent.sys.persistence.manager.SysCategoryManager;
import com.hotent.sys.persistence.manager.SysTypeManager;
import com.hotent.sys.persistence.model.SysCategory;
import com.hotent.sys.persistence.model.SysType;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <pre>
 * 描述：总分类表。用于显示平级或树层次结构的分类，可以允许任何层次结构。管理
 * 构建组：eip平台-bpmx-platform
 * 作者:zyp
 * 邮箱:zyp@jee-soft.cn
 * 日期:2014-1-10-下午3:29:34
 * 版权：广州宏天软件有限公司版权所有
 * </pre>
 */
@RestController
@RequestMapping("/sys/sysType/v1")
@Api(tags="系统分类")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class SysTypeController extends BaseController<SysTypeManager, SysType>{
	@Resource
	SysTypeManager sysTypeManager;
	@Resource
	SysCategoryManager sysCategoryManager;
	@Resource
	IUserService iUserService;
	@Resource
	JmsProducer jmsProducer;
	@Resource
	DataDictManager dataDictManager;
	@Resource
	BaseContext baseContext;




	@RequestMapping(value="list", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "系统分类", httpMethod = "POST", notes = "系统分类")
	public PageList<SysType> listJson(@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysType> queryFilter) throws Exception {
		return sysTypeManager.query(queryFilter);
	}

	@RequestMapping(value="editJson", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "编辑总分类表。用于显示平级或树层次结构的分类，可以允许任何层次结构", httpMethod = "GET", notes = "系统分类")
	public Map<String,Object> editJson(
			@ApiParam(name="id", value="主键", required = false)@RequestParam String id,
			@ApiParam(name="isRoot", value="是否是根节点，1=根节点，0=其他节点", required = false)@RequestParam int isRoot,
			@ApiParam(name="parentId", value="父节点ID", required = false)@RequestParam String parentId,
			@ApiParam(name="isPriNode", value="是否是私有的节点，1=私有节点，0=普通节点", required = false)@RequestParam int isPriNode
		) throws Exception {
		Map<String,Object> res=sysTypeManager.editJson(id,isRoot,parentId,isPriNode);
			return res;
	}

	@RequestMapping(value="getJson", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "系统分类明细页面", httpMethod = "GET", notes = "系统分类")
	public SysType getJson(@ApiParam(name="id", value="主键", required = false)@RequestParam String id) throws Exception {
		SysType sysType = new SysType();
		if (StringUtil.isNotEmpty(id)) {
			sysType = sysTypeManager.get(id);
			if(BeanUtils.isEmpty(sysType)){
				sysType = sysTypeManager.getByKey(id);
			}
		}
		return sysType;
	}

	@RequestMapping(value="getSysTypeByType", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组别名和分类别名获取系统分类", httpMethod = "GET", notes = "系统分类")
	public SysType getSysTypeByType(@ApiParam(name="group", value="分组编码", required = true)@RequestParam String group,@ApiParam(name="typeKey", value="分类编码", required = true)@RequestParam String typeKey) throws Exception {
		return sysTypeManager.getByTypeKeyAndGroupKey(group, typeKey);
	}

	@RequestMapping(value="save", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "保存分类信息", httpMethod = "POST", notes = "保存系统属性信息")
	public CommonResult<String> save(
			@ApiParam(name="sysType", value="分类", required = true)@RequestBody SysType sysType,
			@ApiParam(name="parentId", value="父节点", required = true)@RequestParam String parentId,
			@ApiParam(name="isRoot", value="是否根节点", required = false)@RequestParam int isRoot,
			@ApiParam(name="isPriNode", value="是否是私有的分类", required = false)@RequestParam int isPriNode,
			@ApiParam(name="isDefaultAuth", value="是否添加默认权限", required = false)@RequestParam(required = false) Integer isDefaultAuth
			) throws Exception {
		CommonResult<String> res=sysTypeManager.save(sysType,parentId,isRoot,isPriNode,isDefaultAuth);
		return res;
	}
	@RequestMapping(value="remove", method=RequestMethod.DELETE, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "批量删除系统分类", httpMethod = "DELETE", notes = "批量删除系统分类")
	public CommonResult<String> remove(@ApiParam(name="id", value="主键", required = true)@RequestParam String id) throws Exception {
		try {
			dataDictManager.delByDictTypeId(id);
			sysTypeManager.delByIds(id);
			return new CommonResult<String>(true, "删除成功");
		}  catch (MessagePassingException e) {
			return new CommonResult<String>(false, e.getMessage());
		} catch (Exception e) {
			return new CommonResult<String>(false, "删除失败");
		}
	}

	@RequestMapping(value="sysTypeTree", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "分类树", httpMethod = "POST", notes = "分类树")
	public Map<String,Object> tree() throws Exception {
		List<SysCategory> list = sysCategoryManager.list();
		SysCategory sysCategory = list.get(0);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysCategoryList", list);
		map.put("sysCategory", sysCategory);
		return map;
	}

	@RequestMapping(value="getByParentId", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据父节点获取分类", httpMethod = "GET", notes = "根据父节点获取分类")
	public List<SysType> getByParentId(@ApiParam(name="catId", value="分类id", required = true,defaultValue="0")@RequestParam String catId) throws Exception {
		List<SysType> res=sysTypeManager.getDataByParentId(catId);
		return res;
	}

	@RequestMapping(value="sysTypeSortList", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "排序列表页面", httpMethod = "POST", notes = "排序列表页面")
	public List<SysType> sortList(@ApiParam(name="id", value="主键", required = true,defaultValue="-1")@RequestParam String id) throws Exception {
		String curUserId = ContextUtil.getCurrentUserId();
		List<SysType> sysTypes = sysTypeManager.getPrivByPartId(id, curUserId);
		return sysTypes;
	}

	@RequestMapping(value="sort", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "排序", httpMethod = "POST", notes = "排序")
	public CommonResult<String> sort(@ApiParam(name="typeIds", value="分类id", required = true)@RequestParam String[] typeIds) throws Exception {
		CommonResult<String> res=sysTypeManager.sort(typeIds);
		return res;
	}


	@RequestMapping(value="getTypesByKey", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据可以key获取分类", httpMethod = "GET", notes = "根据可以key获取分类")
	public List<SysType> getTypesByKey(@ApiParam(name="typeKey", value="分类key", required = true)@RequestParam String typeKey) throws Exception {
		List<SysType> res=sysTypeManager.getTypesByKey(typeKey);
		return res;
	}


	@RequestMapping(value="getTreeDateByTypeKey", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据可以key获取分类树", httpMethod = "GET", notes = "根据可以key获取分类树")
	public List<SysType> getTreeDateByTypeKey(@ApiParam(name="typeKey", value="分类key", required = true)@RequestParam String typeKey) throws Exception {
		List<SysType> groupTypes = null;
		if (StringUtil.isNotEmpty(typeKey)) {
			groupTypes = sysTypeManager.getChildByTypeKey(typeKey);
		}
		List<SysType> rtnList= BeanUtils.listToTree(groupTypes);
		return rtnList;
	}

	@RequestMapping(value="getByGroupKey", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据可以组key获取分类", httpMethod = "GET", notes = "根据可以组key获取分类")
	public List<SysType> getByGroupKey(@ApiParam(name="groupKey", value="分类组key", required = true)@RequestParam String groupKey) throws Exception {
		List<SysType> types = null;
		if (StringUtil.isNotEmpty(groupKey)) {
			types = sysTypeManager.getByGroupKey(groupKey);
		}
		List<SysType> rtnList= BeanUtils.listToTree(types);
		return rtnList;
	}

	@RequestMapping(value="updateEntitySysType", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "更新对象的分类信息", httpMethod = "GET", notes = "更新对象的分类信息")
	public CommonResult<String> updateEntitySysType(
			@ApiParam(name="typeID", value="要设置为的分类id", required = true)@RequestParam String typeID,
			@ApiParam(name="entityIds", value="要更新的实体id", required = true)@RequestParam String  entityIds
			) throws Exception {
		CommonResult<String> res=sysTypeManager.updateEntitySysType(typeID,entityIds);
		 return res;
	}

    @RequestMapping(value="getByParentIdOrLikeName", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    @ApiOperation(value = "根据父节点获取分类或者 根据分类名称模糊查询", httpMethod = "GET", notes = "根据父节点获取分类或者 根据分类名称模糊查询")
    public List<SysType> getByParentIdOrLikeName(@ApiParam(name="catId", value="系统分类分组id", required = true,defaultValue="0")@RequestParam String catId,
                                                 @ApiParam(name="parentId", value="分类id", required = false,defaultValue="catId")@RequestParam(required = false) String parentId,
                                                 @ApiParam(name="typeName", value="分类名称", required = false) @RequestParam(required = false) String typeName) throws Exception {
        return sysTypeManager.getByParentIdOrLikeName(catId,parentId,typeName);
    }
}
