package com.product.file.controller;

import com.product.core.config.CoreConst;
import com.product.core.controller.support.AbstractBaseController;
import com.product.core.entity.FieldSetEntity;
import com.product.core.entity.RequestParameterEntity;
import com.product.core.exception.BaseException;
import com.product.core.permission.PermissionService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.config.CmnConst;
import com.product.file.config.DocumentCode;
import com.product.file.service.DocumentDirectoryService;
import com.product.file.service.ide.IDocumentDirectoryService;
import com.product.file.util.ResultInfo;
import com.product.module.sys.config.SystemErrorCode;
import com.product.module.sys.entity.SystemUser;
import com.product.module.sys.version.ApiVersion;
import com.product.util.BaseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Copyright LX-BASE
 *
 * @Title: ClaimExpenseController
 * @Project: LX-BASE-SERVER
 * @Date: 2020-11-18 11:30:48
 * @Author: luoxin
 * @Description: 鏂囦欢澶圭鐞咰onstoller
 */
@RequestMapping("/api/directory")
@RestController
public class DocumentDirectoryController extends AbstractBaseController {

	@Autowired
	DocumentDirectoryService documentDirectoryService;

	/**
	 * 鏁版嵁鏉冮檺楠岃瘉
	 */
	@Autowired
	PermissionService permissionService;

	/**
	 * 鏂板鏂囦欢澶规暟鎹�
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/all-directory-tree/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String allDirectoryTree(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.PRODUCT_OA_DIRECTORY);
			return com.product.util.ResultInfo.success(BaseUtil.dataTableEntityToJson(documentDirectoryService.allDirectoryTree()));
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getText());
		}
	}

	/**
	 * 鏂囨。鐩綍淇濆瓨
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/save-directory/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String saveDocumentDireactory(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			IDocumentDirectoryService service = (IDocumentDirectoryService) getProxyInstance(documentDirectoryService);
			if(service.saveDocumentDireactory(fse)) {
				return OK();
			}
			return error(DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getText());
		} catch (BaseException e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return error(DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getText());
		}
	}

	/**
	 * 涓汉鏂囦欢澶归噸鍛藉悕
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/upDocumentDirectoryName/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String upDocumentDirectoryName(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = null;
			Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
			if (bean != null) {
				RequestParameterEntity reqp = (RequestParameterEntity) bean;
				fse = reqp.getFormData();
			}
			// 鍒ゆ柇鍙傛暟鏄惁涓虹┖
			if (fse == null) {
				SpringMVCContextHolder.getSystemLogger().error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
				return this.error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
			}
			String uuid = fse.getString(CmnConst.UUID);// 琛╱uid
			// 瓒呯骇绠$悊鍛樹负1 鏅€氬崟浣嶇鐞嗗憳2
			SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
			int type;
			if (currentUser.getUserType() == 2) {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
			} else {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
			}
			// 鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if (!permissionService.validDataPermission(CmnConst.PRODUCT_OA_DIRECTORY, uuid, type)) {
				SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
				return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
			}
			IDocumentDirectoryService service = (IDocumentDirectoryService) getProxyInstance(documentDirectoryService);
			if (service.upDocumentDirectoryName(fse)) {
				return OK();
			}
			return this.error(DocumentCode.DOCUMENT_DIRECTORY_NAME_FAIL.getValue(),
					DocumentCode.DOCUMENT_DIRECTORY_NAME_FAIL.getText());
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_DIRECTORY_NAME_FAIL.getValue(),
					DocumentCode.DOCUMENT_DIRECTORY_NAME_FAIL.getText());
		}
	}

	/**
	 * 鍒犻櫎鏂囦欢澶�
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delDocumentDirectory/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String delDocumentDirectory(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = null;
			Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
			if (bean != null) {
				RequestParameterEntity reqp = (RequestParameterEntity) bean;
				fse = reqp.getFormData();
			}
			// 鍒ゆ柇鍙傛暟鏄惁涓虹┖
			if (fse == null) {
				SpringMVCContextHolder.getSystemLogger().error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
				return this.error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
			}
			String uuid = fse.getString(CmnConst.UUID);// 琛╱uid
			// 瓒呯骇绠$悊鍛樹负1 鏅€氬崟浣嶇鐞嗗憳2
			SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
			int type;
			if (currentUser.getUserType() == 2) {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
			} else {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
			}
			// 鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if (!permissionService.validDataPermission(CmnConst.PRODUCT_OA_DIRECTORY, uuid, type)) {
				SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
				return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
			}
			IDocumentDirectoryService service = (IDocumentDirectoryService) getProxyInstance(documentDirectoryService);
			if (service.delDocumentDirectory(fse)) {
				return OK();
			}
			return this.error(DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getText());
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getText());
		}
	}

	/**
	 * 鏂囦欢鐩綍鍒楄〃灞曠ず
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/moveToList/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String moveToList(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = null;
			Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
			if (bean != null) {
				RequestParameterEntity reqp = (RequestParameterEntity) bean;
				fse = reqp.getFormData();
			}
			// 鍒ゆ柇鍙傛暟鏄惁涓虹┖
			if (fse == null) {
				SpringMVCContextHolder.getSystemLogger().error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
				return this.error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
			}
			return OK_List(documentDirectoryService.moveToList(fse));
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_CATALOG_GET_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_GET_FAIL.getText());
		}
	}

	/**
	 * 鑾峰彇鏂囨。鐩綍鏍戝舰鍒楄〃
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/treeListAll/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String treeListAll(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = null;
			Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
			if (bean != null) {
				RequestParameterEntity reqp = (RequestParameterEntity) bean;
				fse = reqp.getFormData();
			}
			// 鍒ゆ柇鍙傛暟鏄惁涓虹┖
			if (fse == null) {
				SpringMVCContextHolder.getSystemLogger().error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
				return this.error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
			}
			return ResultInfo.success(documentDirectoryService.treeListAll(fse));
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_CATALOG_GET_TREE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_GET_TREE_FAIL.getText());
		}
	}

	/**
	 * 鑾峰彇鏂囨。鐩綍璇︽儏
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/documentDirectoryInfo/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String documentDirectoryInfo(HttpServletRequest request) {
		try {
			// 鑾峰彇鍙傛暟
			FieldSetEntity fse = null;
			Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
			if (bean != null) {
				RequestParameterEntity reqp = (RequestParameterEntity) bean;
				fse = reqp.getFormData();
			}
			// 鍒ゆ柇鍙傛暟鏄惁涓虹┖
			if (fse == null) {
				SpringMVCContextHolder.getSystemLogger().error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
				return this.error(DocumentCode.DOCUMENT_FORM_NODATA.getValue(),
						DocumentCode.DOCUMENT_FORM_NODATA.getText());
			}
			String uuid = fse.getString(CmnConst.UUID);// 琛╱uid
			// 瓒呯骇绠$悊鍛樹负1 鏅€氬崟浣嶇鐞嗗憳2
			SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
			int type;
			if (currentUser.getUserType() == 2) {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
			} else {
				type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
			}
			// 鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if (!permissionService.validDataPermission(CmnConst.PRODUCT_OA_DIRECTORY, uuid, type)) {
				SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
				return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(),
						SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
			}
			return OK_List(documentDirectoryService.documentDirectoryInfo(fse));
		} catch (BaseException e) {
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(DocumentCode.DOCUMENT_CATALOG_GET_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_GET_FAIL.getText());
		}
	}

	/**
	 * 淇濆瓨鏂囨。褰掓。閰嶇疆
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/save-file-config/{version}", method = RequestMethod.POST)
	@ApiVersion(1)
	public String saveFileConfig(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			IDocumentDirectoryService service = (IDocumentDirectoryService) getProxyInstance(documentDirectoryService);
			if(service.saveFileConfig(fse)) {
				return OK();
			}
			return error(DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getText());
		} catch (BaseException e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return this.error(e);
		} catch (Exception e) {
			e.printStackTrace();
			SpringMVCContextHolder.getSystemLogger().error(e);
			return error(DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getValue(),
					DocumentCode.DOCUMENT_CATALOG_SAVE_FAIL.getText());
		}
	}
}