package com.product.admin.controller;

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 com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.service.SystemButtonsService;
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.module.sys.config.SystemErrorCode;
import com.product.module.sys.version.ApiVersion;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright  LX-BASE
 *
 * @Title: SystemButtonsController
 * @Project: LX-BASE-SERVER
 * @Date: 2020-06-11 18:12
 * @Author: ZhouJie
 * @Description: 鎸夐挳绠$悊(姝ょ被鏈敤)
 */
@RestController
@RequestMapping("/api/button")
public class SystemButtonsController extends AbstractBaseController {


    @Autowired
    private SystemButtonsService systemButtonsService;
	@Autowired
    PermissionService permissionService;
    /**
     * 鍔犺浇椤甸潰鎸夐挳
     * @param request
     * @return
     */
    @RequestMapping(value = "/load-page-button/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String findButtonByPage(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 (bean == null || fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS.equals(fse.getTableName())) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
                return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
            }
            Map<String,Object> map=new HashMap<>();
            map.put("currentButton",BaseUtil.fieldSetEntityToJson(systemButtonsService.findFromButtonByPage(fse)));
            return BaseUtil.success(systemButtonsService.findButtonByPage(fse),map);
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_PAGE_BUTTON_LOAD_FAIL.getValue(), SystemCode.SYSTEM_PAGE_BUTTON_LOAD_FAIL.getText());
        }
    }

/**
     * @Date: 2020-06-11 17:05   
     * @Author: ZhouJie
     * @Description: 鎸夐挳璇︽儏鏌ョ湅
     */
	@RequestMapping(value = "/find-function/{version}",method = RequestMethod.POST)
	@ApiVersion(1)
	public String findFunctionInfo(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 (bean == null || fse == null) {
				return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
			}
			if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_FUNCTIONS.equals(fse.getTableName())) {
				return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
			}
			//鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if(!permissionService.validDataPermission(fse,CoreConst.DATA_PERMISSION_VALID_TYPE_USER)) {
				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());
			}
			FieldSetEntity fss = systemButtonsService.findButton(fse);
			return OK_List(fss);
        }catch(BaseException e) {
            logger.error("FunctionsController.findFunctionInfo error",e);
            return this.error(e);
        }	
	}
    
    /**
     * @Date: 2020-06-11 17:45   
     * @Author: ZhouJie
     * @Description: 鎸夐挳鐗堟湰鍙�
     */
	@RequestMapping(value = "/find-buttonversion/{version}",method = RequestMethod.POST)
	@ApiVersion(1)
	public String findVersion(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 (bean == null || fse == null) {
				return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
			}
			if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS.equals(fse.getTableName())) {
				return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
			}
			//鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if(!permissionService.validDataPermission(fse,CoreConst.DATA_PERMISSION_VALID_TYPE_USER)) {
				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());
			}
			FieldSetEntity fss = systemButtonsService.findBottonVersion(fse);
			return OK_List(fss);
        }catch(BaseException e) {
            logger.error("FunctionsController.findFunctionInfo error",e);
            return this.error(e);
        }	
	}
	
	/**
	 * @Date: 2020-06-13 10:13   
	 * @Author: ZhouJie
	 * @Description: 鍗囩骇鎸夐挳鐗堟湰鍙�
	 */
	@RequestMapping(value = "/up-version/{version}",method = RequestMethod.POST)
	@ApiVersion(1)
	public String upVersion(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 (bean == null || fse == null) {
				return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
			}
			if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS.equals(fse.getTableName())) {
				return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
			}
			//鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
			if(!permissionService.validDataPermission(fse,CoreConst.DATA_PERMISSION_VALID_TYPE_USER)) {
				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());
			}
			FieldSetEntity fss = systemButtonsService.upVersion(fse);
			return OK_List(fss);
        }catch(BaseException e) {
            logger.error("FunctionsController.findFunctionInfo error",e);
            return this.error(e);
        }	
	}


}