package com.boman.system.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public interface SyncMapper {
    Logger LOGGER = LoggerFactory.getLogger(SyncMapper.class);

    @SelectProvider(type = SqlProvider.class, method = "selectBySql")
    List<JSONObject> selectBySql(@Param("sql") String sql);

    @SelectProvider(type = SqlProvider.class, method = "selectCount")
    int selectCount(@Param("sql") String sql);


    @InsertProvider(type = SqlProvider.class, method = "updateData")
    int updateData(@Param("jsonObject") JSONObject jsonObject, @Param("key") String key);

    class SqlProvider {

        public String selectBySql(Map<String, Object> para) {
            return (String) para.get("sql");
        }

        public String selectCount(Map<String, Object> para) {
            return (String) para.get("sql");
        }


        public String updateData(Map<String, Object> para) {
            JSONObject jsonObject = (JSONObject) para.get("jsonObject");
            String key = (String) para.get("key");
            String tableName = "vaccine_info";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("update ").append(tableName).append(" set ");
            for(String column : jsonObject.keySet()) {
                if(StringUtils.isNotEmpty((String) jsonObject.get(column))) {
                    stringBuilder.append(column).append(" = ").append("'").append(jsonObject.get(column)).append("'").append(",");
                }
            }
            String sql = stringBuilder.toString();
            sql = sql.substring(0, sql.lastIndexOf(","));
            stringBuilder.append(" where id_card = ").append("''" + key + "");
            sql += " where id_card = '" + key + "'";

            return sql;
        }
    }
}
