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.SystemInInterfaceService;
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.spring.context.SpringMVCContextHolder;
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;

/**
 * @Author cheng
 * @Description 绯荤粺鎺ュ彛
 * @Date 2021/3/25 19:41
 * @Version 1.0
 */

@RestController
@RequestMapping("api/interface/api")
public class SystemInInterfaceController extends AbstractBaseController {

    @Autowired
    SystemInInterfaceService systemInInterfaceService;

    /**
     * 鍒楄〃
     * @param request
     * @return
     */
    @RequestMapping(value = "/list-api-info/{version}",method = RequestMethod.POST)
    @ApiVersion(1)
    public String getList(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 (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_CLIENTS.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());
            }
            // 鍒ゆ柇鍒嗛〉鍙傛暟鏄惁姝e父
            if (StringUtils.isEmpty(fse.getString(CmnConst.PAGESIZE))
                    || StringUtils.isEmpty(fse.getString(CmnConst.CPAGE))) {
                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());
            }
            return OK_List(systemInInterfaceService.getList(fse));
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            e.printStackTrace();
            return error(e.getCode(), e.getMessageInfo());
        }catch (Exception e){
            SpringMVCContextHolder.getSystemLogger().error(e);
            e.printStackTrace();
            return error(SystemCode.SYSTEM_SELECT_CLIENT_LIST_FIAL.getValue(),SystemCode.SYSTEM_SELECT_CLIENT_LIST_FIAL.getText()+e.getMessage());
        }
    }
}