package com.product.admin.controller;


import com.alibaba.druid.util.StringUtils;
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.service.CodeLogicService;
import com.product.core.config.CoreConst;
import com.product.core.controller.support.AbstractBaseController;
import com.product.core.entity.DataTableEntity;
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 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  product-BASE
 * @Title: CodeLogicController
 * @Project: product-BASE-CONTROLLER
 * @Date:  2020-09-09 11:30
 * @Author: ZhouJie
 * @Description: 浠g爜閫昏緫閰嶇疆
 */
@RestController
@RequestMapping("/api/codelogic")
public class CodeLogicController extends AbstractBaseController {
    @Autowired
    public CodeLogicService codeLogicService;
    /**
     * 鏁版嵁鏉冮檺楠岃瘉
     */
    @Autowired
    PermissionService permissionService;

    /**
     * 浠g爜閫昏緫閰嶇疆鍒楄〃鏌ヨ鎺ュ彛
     */
    @RequestMapping(value = "/list-codelogic/{version}", method = RequestMethod.POST)
    public String listCodeLogic(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_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            //鍒ゆ柇琛ㄥ悕鏄惁姝e父
            if (!CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS.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());
            }
            DataTableEntity dt = codeLogicService.getCodeLogicList(fse);
            return OK_List(dt);
        }catch(BaseException e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        }catch (Exception e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_CODELOGIC_LIST_FAIL.getValue(), SystemCode.SYSTEM_CODELOGIC_LIST_FAIL.getText());
        }
    }

    /**
     * 浠g爜閫昏緫閰嶇疆璇︽儏鏌ヨ
     * @return
     */
    @RequestMapping(value="/find-codelogic/{version}",method=RequestMethod.POST)
    @ApiVersion(1)
    public String getCodeLogicInfo(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());
            }
            //鍒ゆ柇琛ㄥ悕鏄惁姝e父
            if (!CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS.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());
            }
            //鍒ゆ柇uuid鏄惁涓虹┖
            //鍛ㄦ澃 2020骞�12鏈�17鏃� 涓婂崍10:26
            if (StringUtils.isEmpty(fse.getString("uuid"))) { 
            	SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
            }
            //鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
            if(!permissionService.validDataPermission(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS,fse.getString("uuid"),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 fs = codeLogicService.getCodeLogicInfo(fse.getString("uuid"));
            return OK_List(fs);
        }catch(BaseException e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        }catch (Exception e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_CODELOGIC_DETAIL_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_DETAIL_FAIL.getText());
        }
    }

    /**
     * 浠g爜閫昏緫閰嶇疆鏂板
     * @param request
     * @return
     */
    @RequestMapping(value="/add-codelogic/{version}",method=RequestMethod.POST)
    @ApiVersion(1)
    public String addCodeLogic(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());
            }
            //鍒ゆ柇琛ㄥ悕鏄惁姝e父
            if (!CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS.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());
            }
            String uuid = codeLogicService.addCodeLogic(fse);
            return OK_Add(uuid);
        } catch (BaseException e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_CODELOGIC_ADD_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_ADD_FAIL.getText());
        } catch (Exception e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_CODELOGIC_ADD_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_ADD_FAIL.getText());
        }
    }

    /**
     * 浠g爜閫昏緫閰嶇疆鏇存柊
     * @param request
     * @return
     */
    @RequestMapping(value="/update-codelogic/{version}",method=RequestMethod.POST)
    @ApiVersion(1)
    public String updateCodeLogic(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());
            }
            //鍒ゆ柇琛ㄥ悕鏄惁姝e父
            if (!CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS.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());
            }
            //鍒ゆ柇uuid鏄惁涓虹┖
            //鍛ㄦ澃 2020骞�12鏈�17鏃� 涓婂崍10:26
            if (StringUtils.isEmpty(fse.getString("uuid"))) {
            	SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
                return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
            }
            //鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
            if(!permissionService.validDataPermission(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS,fse.getString("uuid"),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());
            }
            boolean succ=codeLogicService.updateCodeLogic(fse);
            if(succ) {
            	return OK();
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getText());
            return error(SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getText());
        }catch(BaseException e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        }catch (Exception e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_UPDATE_FAIL.getText());
        }
    }



    /**
     * 浠g爜閫昏緫閰嶇疆鍒犻櫎
     * @return
     */
    @RequestMapping(value="/delete-codelogic/{version}",method=RequestMethod.POST)
    @ApiVersion(1)
    public String deleteCodeLogic(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());
            }
            //鍒ゆ柇琛ㄥ悕鏄惁姝e父
            if (!CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS.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());
            }
            //鍒ゆ柇uuid鏄惁涓虹┖
            //鍛ㄦ澃 2020骞�12鏈�17鏃� 涓婂崍10:26
            if (StringUtils.isEmpty(fse.getString("uuid"))) {
            	SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
                return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
            }
            //鏁版嵁鎿嶄綔鐨勬潈闄愰獙璇�
            if(!permissionService.validDataPermission(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE_PROCESS,fse.getString("uuid"),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());
            }
            boolean succ=codeLogicService.deleteCodeLogic(fse);
            if(succ) {
            	return OK();
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getText());
            return error(SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getText());
        }catch(BaseException e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        }catch (Exception e) {
        	SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getValue(),SystemCode.SYSTEM_CODELOGIC_DELETE_FAIL.getText());
        }
    }





}