package com.product.data.sync.controller;

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.spring.context.SpringMVCContextHolder;
import com.product.data.sync.config.CmnConst;
import com.product.data.sync.config.SystemCode;
import com.product.data.sync.service.SyncConfigService;
import com.product.data.sync.service.ide.ISyncConfigService;
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
 *
 * @Title: SyncConfigController
 * @Project: product-server
 * @date: 2021-08-11 16:35
 * @author: ZhouJie
 * @Description: 鏁版嵁搴撳悓姝ラ厤缃�
 */
@RequestMapping("/api/sync")
@RestController
public class SyncConfigController extends AbstractBaseController {

    @Autowired
    public SyncConfigService syncConfigService;

    /**
     * 淇濆瓨鏁版嵁搴撳悓姝ラ厤缃�
     * @param request
     * @return
     */
    @RequestMapping(value = "/save-syncconfig/{version}", method = RequestMethod.POST)
    public String saveSyncConfig(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_DATABASE_SYNC_CONFIG.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());
            }
            ISyncConfigService service = (ISyncConfigService)getProxyInstance(syncConfigService);
            String uuid = service.saveSyncConfig(fse);
            if (!BaseUtil.strIsNull(uuid)) {
                return OK_Add(uuid);
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SAVE_SYNC_CONFIG_FAIL.getValue(), SystemCode.SYSTEM_SAVE_SYNC_CONFIG_FAIL.getText());
            return error(SystemCode.SYSTEM_SAVE_SYNC_CONFIG_FAIL.getValue(), SystemCode.SYSTEM_SAVE_SYNC_CONFIG_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_SAVE_SYNC_CONFIG_FAIL.getValue(), SystemCode.SYSTEM_SAVE_SYNC_CONFIG_FAIL.getText());
        }
    }

    /**
     * 鍒犻櫎鏁版嵁搴撳悓姝ラ厤缃�
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete-syncconfig/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String delSyncConfig(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_DATABASE_SYNC_CONFIG.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鏄惁涓虹┖
            if (com.alibaba.druid.util.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());
            }
            ISyncConfigService service = (ISyncConfigService) getProxyInstance(syncConfigService);
            boolean success=service.delSyncConfig(fse);
            if(success) {
                return OK();
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_DELETE_SYNC_CONFIG_FAIL.getValue(),SystemCode.SYSTEM_DELETE_SYNC_CONFIG_FAIL.getText());
            return error(SystemCode.SYSTEM_DELETE_SYNC_CONFIG_FAIL.getValue(),SystemCode.SYSTEM_DELETE_SYNC_CONFIG_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_DELETE_SYNC_CONFIG_FAIL.getValue(),SystemCode.SYSTEM_DELETE_SYNC_CONFIG_FAIL.getText());
        }
    }

    /**
     * 鑾峰彇鏁版嵁搴撳悓姝ラ厤缃垪琛�
     * @param request
     * @return
     */
    @RequestMapping(value = "/list-syncconfig/{version}", method = RequestMethod.POST)
    public String listSyncConfig(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_DATABASE_SYNC_CONFIG.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());
            }
            // 鍒ゆ柇鍒嗛〉鍙傛暟
            if (fse.getString(CmnConst.PAGESIZE) == null || fse.getString(CmnConst.CPAGE) == null) {
                return this.error(SystemCode.SYSTEM_CPAGES_NOT_NULL.getValue(),
                        SystemCode.SYSTEM_CPAGES_NOT_NULL.getText());
            }
            DataTableEntity dt = syncConfigService.listSyncConfig(fse);
            return OK_List(dt);
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_LIST_SYNC_CONFIG_FAIL.getValue(),
                    SystemCode.SYSTEM_LIST_SYNC_CONFIG_FAIL.getText());
        }
    }

    /**
     * 鏌ヨ鏁版嵁搴撳悓姝ラ厤缃鎯�
     * @param request
     * @return
     */
    @RequestMapping(value = "/get-syncconfig/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String getSyncConfigSyncConfig(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_DATABASE_SYNC_CONFIG.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鏄惁涓虹┖
            if (BaseUtil.strIsNull(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());
            }
            FieldSetEntity fs = syncConfigService.getSyncConfig(fse);
            return OK_List(fs);
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_GET_SYNC_CONFIG_FAIL.getValue(),
                    SystemCode.SYSTEM_GET_SYNC_CONFIG_FAIL.getText());
        }
    }

    /**
     * 鑾峰彇鏁版嵁鍚屾鏃ュ織
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSyncLog/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String getSyncLog(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_DATABASE_SYNC_CONFIG.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鏄惁涓虹┖
            if (BaseUtil.strIsNull(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());
            }
            DataTableEntity dataTableEntity = syncConfigService.getSyncLog(fse);
            return OK_List(dataTableEntity);
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.GET_SYNC_LOG_FAIL.getValue(),
                    SystemCode.GET_SYNC_LOG_FAIL.getText());
        }
    }

    /**
     * 鑾峰彇鏁版嵁鍚屾鏃ュ織閿欒瀛愯〃
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSyncLogSub/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String getSyncLogSub(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_DATABASE_SYNC_CONFIG_LOG.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鏄惁涓虹┖
            if (BaseUtil.strIsNull(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());
            }
            DataTableEntity dataTableEntity = syncConfigService.getSyncLogSub(fse);
            return OK_List(dataTableEntity);
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.GET_SYNC_LOG_FAIL.getValue(),
                    SystemCode.GET_SYNC_LOG_FAIL.getText());
        }
    }
}