package com.storlead.system.controller;

import com.storlead.framework.common.result.Result;
import com.storlead.user.model.DepartIdModel;
import com.storlead.user.model.DeptTreeModel;
import com.storlead.user.service.IDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
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.RestController;

import java.util.List;

@RestController
@RequestMapping("/lingcun/dept")
@Slf4j
@Api(tags="平台中心 - 系统: 公共接口")
public class DeptController {

	@Resource
	private IDepartService departService;

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-部门-tree数据", notes = "公共接口-部门-tree数据")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = DeptTreeModel.class)
	})
	public Result<List<DeptTreeModel>> queryTreeList() {
		Result<List<DeptTreeModel>> result = new Result<>();
		try {
			// 从内存中读取
//			List<SysDepartTreeModel> list =FindsDepartsChildrenUtil.getSysDepartTreeList();
//			if (CollectionUtils.isEmpty(list)) {
//				list = sysDepartService.queryTreeList();
//			}
			List<DeptTreeModel> list = departService.queryTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * 查询数据 查出分公司下所有部门
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryDeptBySub", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-公司部门-根据分公司ID查询部门tree数据", notes = "根据分公司ID查询部门tree数据")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = DeptTreeModel.class)
	})
	public Result<List<DeptTreeModel>> queryDeptBySub(Integer subCompanyId) {
		Result<List<DeptTreeModel>> result = new Result<>();
		try {
			List<DeptTreeModel> list = departService.queryDeptBySubList(subCompanyId);
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeListWithSubCompanyId", method = RequestMethod.GET)
	@ApiOperation(value = "公共接口-公司部门-tree数据", notes = "根据分公司ID查询部门tree数据")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = DeptTreeModel.class)
	})
	public Result<List<DeptTreeModel>> queryTreeListWithSubCompanyId(Integer subCompanyId) {
		Result<List<DeptTreeModel>> result = new Result<>();
		try {
			// 从内存中读取
//			List<SysDepartTreeModel> list =FindsDepartsChildrenUtil.getSysDepartTreeList();
//			if (CollectionUtils.isEmpty(list)) {
//				list = sysDepartService.queryTreeList();
//			}
			List<DeptTreeModel> list = departService.queryTreeListWithSubCompanyId(subCompanyId);
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有部门的名称,方便用户的操作
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
	public Result<List<DepartIdModel>> queryIdTree() {
//		Result<List<DepartIdModel>> result = new Result<List<DepartIdModel>>();
//		List<DepartIdModel> idList;
//		try {
//			idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
//			if (idList != null && idList.size() > 0) {
//				result.setResult(idList);
//				result.setSuccess(true);
//			} else {
//				sysDepartService.queryTreeList();
//				idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
//				result.setResult(idList);
//				result.setSuccess(true);
//			}
//			return result;
//		} catch (Exception e) {
//			log.error(e.getMessage(),e);
//			result.setSuccess(false);
//			return result;
//		}
		Result<List<DepartIdModel>> result = new Result<>();
		try {
			List<DepartIdModel> list = departService.queryDepartIdTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * <p>
	 * 部门搜索功能方法,根据关键字模糊搜索相关部门
	 * </p>
	 *
	 * @param keyWord
	 * @return
	 */
	@RequestMapping(value = "/searchBy", method = RequestMethod.GET)
	public Result<List<DeptTreeModel>> searchBy(@RequestParam(name = "keyWord") String keyWord) {
		Result<List<DeptTreeModel>> result = new Result<List<DeptTreeModel>>();
		try {
			List<DeptTreeModel> treeList = this.departService.searhBy(keyWord);
			if (treeList.size() == 0 || treeList == null) {
				throw new Exception();
			}
			result.setSuccess(true);
			result.setResult(treeList);
			return result;
		} catch (Exception e) {
			e.fillInStackTrace();
			result.setSuccess(false);
			result.setMessage("查询失败或没有您想要的任何数据!");
			return result;
		}
	}

}
