package com.home.xining.controller;

import com.home.xining.config.CmnConst;
import com.home.xining.config.SystemCode;
import com.home.xining.service.ReceiptDocumentService;
import com.home.xining.service.idel.IReceiptDocumentService;
import com.product.admin.service.PublicService;
import com.product.core.config.CoreConst;
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.module.sys.version.ApiVersion;
import com.product.util.support.AbstractBaseController;
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;

/**
 * @ClassName: ReceiptDocumentController
 * @Author: ZhouJie
 * @Date: 2022/2/21 13:47
 * @Description: 鏀舵枃绠$悊
 */
@RequestMapping("/api/receiptdocument")
@RestController
public class ReceiptDocumentController extends AbstractBaseController {

    @Autowired
    PublicService publicService;
    @Autowired
    public ReceiptDocumentService receiptDocumentService;

    /**
     * 鏀舵枃绠$悊鍒楄〃
     * @param request
     * @return
     */
    @RequestMapping(value ="/list-redoc/{version}",  method = RequestMethod.POST)
    @ApiVersion(1)
    public String listReceiptDoc(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.SFGL.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 = receiptDocumentService.getReciptDocList(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.GETLIST_RECIPT_DOC_FILE.getValue(), SystemCode.GETLIST_RECIPT_DOC_FILE.getText());
        }
    }

    /**
     * 鏀舵枃绠$悊鍒楄〃
     * @param request
     * @return
     */
    @RequestMapping(value ="/isPrint/{version}",  method = RequestMethod.POST)
    @ApiVersion(1)
    public String isPrint(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.SFGL.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());
            }
            IReceiptDocumentService service = (IReceiptDocumentService)getProxyInstance(receiptDocumentService);
            if(service.isPrint(fse)){
                return OK();
            }
            return this.error(SystemCode.SAVE_DATA_FILE.getValue(), SystemCode.SAVE_DATA_FILE.getText());
        }catch(BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e);
        }catch (Exception e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SAVE_DATA_FILE.getValue(), SystemCode.SAVE_DATA_FILE.getText());
        }
    }
}