package com.bonus.canteen.core.common.base;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bonus.canteen.core.common.converter.GsonLocalDateAdapter;
import com.bonus.canteen.core.common.converter.GsonLocalDateTimeAdapter;
import com.bonus.canteen.core.common.utils.HeaderFetchUtil;
import com.bonus.common.core.web.page.TableDataInfo;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BaseController {
   protected Logger logger = LoggerFactory.getLogger(this.getClass());
   protected Gson gson = (new GsonBuilder()).registerTypeAdapter(LocalDate.class, new GsonLocalDateAdapter()).registerTypeAdapter(LocalDateTime.class, new GsonLocalDateTimeAdapter()).create();
   protected static final String MGRVII_TYPE = "mgrvii-type";

   protected Long getMerchantIdByHeaders(Map<String, String> headers) {
      String value = HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "MERCHANT-ID");
      return CharSequenceUtil.isNotBlank(value) ? Long.valueOf(value) : null;
   }

   protected String getOpenidByHeaders(Map<String, String> headers) {
      return HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "openid");
   }

   protected int getfaceVerHeaders(Map<String, String> headers) {
      if (CollectionUtils.isEmpty(headers)) {
         return -1;
      } else {
         Iterator var2 = headers.keySet().iterator();

         String key;
         do {
            if (!var2.hasNext()) {
               return -1;
            }

            key = (String)var2.next();
         } while(!"face-ver".equals(key));

         return Integer.parseInt((String)headers.get(key));
      }
   }

   protected int getMgrviiTypeHeaders(Map<String, String> headers) {
      if (CollectionUtils.isEmpty(headers)) {
         return 1;
      } else {
         Iterator var2 = headers.keySet().iterator();

         String key;
         do {
            if (!var2.hasNext()) {
               return 1;
            }

            key = (String)var2.next();
         } while(!"mgrvii-type".equals(key));

         return Integer.parseInt((String)headers.get(key));
      }
   }

   protected String getMachineSnHeaders(Map<String, String> headers) {
      return HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "machine-sn");
   }

   protected String getMachineNumHeaders(Map<String, String> headers) {
      return HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "machine-num");
   }

   protected Integer getSourceTypeByHeaders(Map<String, String> headers) {
      String value = HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "source-type");
      return CharSequenceUtil.isNotBlank(value) ? Integer.valueOf(value) : null;
   }

   protected Integer getAppletTypeByHeaders(Map<String, String> headers) {
      String value = HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "applet-type");
      return CharSequenceUtil.isNotBlank(value) ? Integer.valueOf(value) : null;
   }

   protected TableDataInfo getDataTable(List<?> list) {
      TableDataInfo rspData = new TableDataInfo();
      rspData.setCode(200);
      rspData.setRows(list);
      rspData.setMsg("查询成功");
      rspData.setTotal((new PageInfo(list)).getTotal());
      return rspData;
   }

   protected TableDataInfo getDataTableError(List<?> list) {
      TableDataInfo rspData = new TableDataInfo();
      rspData.setCode(500);
      rspData.setRows(list);
      rspData.setMsg("系统错误,请联系管理员");
      rspData.setTotal((new PageInfo(list)).getTotal());
      return rspData;
   }
}
