package com.brilliance.isc.common.component;

import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.mda.dao.PublicMapper;
import com.google.common.collect.ImmutableBiMap;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class DyncDaoService {

    @Autowired
    private PublicMapper publicMapper;

    @Value("${env.mapperPackage:com.brilliance.isc.mda.dao}")
    private String mapperPackage;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 通过inr获取表对象
     *
     * @param clazz
     * @param inr
     * @return
     */
    public Object dyncReadByInr(Class<?> clazz, String inr) {
        if (clazz == null) {
            return null;
        }
        try {
            Object obj = clazz.newInstance();
            dyncReadByInr(obj, inr);
            return obj;
        } catch (IllegalAccessException e) {
            throw new CommonServiceException(e.getMessage());
        } catch (InstantiationException e) {
            throw new CommonServiceException(e.getMessage());
        }
    }

    public Map<String, Object> dyncReadByInr(String tableName, String inr) {
        Map<String, Object> maps = new HashMap<>();
        if (StringUtils.isNotEmpty(tableName) && StringUtils.isNotEmpty(inr)) {
            String sql = "select * from %s where INR = %s";
            sql = String.format(sql, tableName, "'" + inr + "'");
            Map<String, Object> params = ImmutableBiMap.of("sql", sql);
            List<Map<String, Object>> result = publicMapper.dyncQueryMapList(params);
            if (result != null && result.size() > 0) {
                result.get(0).forEach((key, value) -> maps.put(key.toLowerCase(), value));
            }
        }
        return maps;
    }

    public boolean dyncReadByInr(Object bo, String inr) {
        String sql = "select * from %s where INR = %s";
        sql = String.format(sql, Utils.getBoType(bo), "'" + inr + "'");
        Map<String, Object> params = ImmutableBiMap.of("sql", sql);
        List<Map<String, Object>> result = publicMapper.dyncQueryMapList(params);
        if (result != null && result.size() > 0) {
            result.get(0).entrySet().stream().forEach(e -> {
                Utils.setFieldValue(bo, e.getKey().toLowerCase(), e.getValue());
            });
            return true;
        }
        return false;
    }

    public <T> T dyncReadFieldByInr(String tableName, String inr, String field) {
        String sql = "select %s from %s where INR = %s";
        sql = String.format(sql, field, tableName, "'" + inr + "'");
        Map<String, Object> params = ImmutableBiMap.of("sql", sql);
        Map<String, Object> result = publicMapper.dyncQueryMap(params);
        if (result != null && result.size() > 0) {
            if (result.get(field)!=null ){
                return (T) result.get(field);
            }else{
                return (T) result.get(field.toUpperCase());
            }
        }
        return null;
    }

    public boolean dyncReadNoDataByInr(Object bo) {
        String inr = Utils.getFieldValue(bo, "inr", "");
        if (inr == null || inr.isEmpty()) {
            return false;
        }
        String sql = "select inr from %s where INR = %s";
        sql = String.format(sql, Utils.getBoType(bo), "'" + inr + "'");
        Map<String, Object> params = ImmutableBiMap.of("sql", sql);
        List<Map<String, Object>> result = publicMapper.dyncQueryMapList(params);
        return result != null && result.size() > 0;
    }

    public void dyncInsert(Object bo) {
        String sqlId = String.format("%s.%s.%s", mapperPackage, Utils.getMapperName(bo), "insert");
        sqlSessionTemplate.insert(sqlId, bo);
    }

    public void dyncDeleteByPrimaryKey(Object bo) {
        String sqlId = String.format("%s.%s.%s", mapperPackage, Utils.getMapperName(bo), "deleteByPrimaryKey");
        sqlSessionTemplate.delete(sqlId, bo);
    }

    public void dyncUpdateByPrimaryKeySelective(Object bo) {
        String sqlId = String.format("%s.%s.%s", mapperPackage, Utils.getMapperName(bo), "updateByPrimaryKeySelective");
        sqlSessionTemplate.update(sqlId, bo);
    }

    public void dyncUpdateByPrimaryKey(Object bo) {
        String sqlId = String.format("%s.%s.%s", mapperPackage, Utils.getMapperName(bo), "updateByPrimaryKey");
        sqlSessionTemplate.update(sqlId, bo);
    }


}
