package com.roma.romaapi.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roma.romaapi.dao.DbApiDao;

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

@Service
public class DbApiService {

    @Autowired
    DbApiDao dbApiDao;
    public Map<String, Object> transferSql(String sqlCode, Map<String, String[]> maps){
        System.out.println("参数--------长度-----"+maps.size());
        Map hasSql = dbApiDao.dbQueryForMap(sqlCode);
        boolean isEmpty = hasSql.containsKey("sysErrorCode");
        if(isEmpty) {
            return  hasSql;
        }
        // 获取全部的请求参数
        Map bindData = new HashMap<>();
        String page = "1"; // 页数
        String perPage = "15"; // 显示多少条
        // 组装要绑定的参数数据
        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
            String paramsKey = entry.getKey();
            String paramsValue = entry.getValue()[0];
            String fourBeforestring = "";
            if(paramsKey.length() >= 4) {
                fourBeforestring = paramsKey.substring(0,4);
                if (fourBeforestring.equals("dbp_")) {
                    System.out.println("参数-------------------"+paramsValue);
                    bindData.put(paramsKey, paramsValue);
                }
            }
            if(paramsKey.equals("page")) {
                page = paramsValue;
            }
            if(paramsKey.equals("perPage")) {
                perPage = paramsValue;
            }
        }

        Object sqlBehavior = hasSql.get("sql_type");
        Map ret = new HashMap<>();
        // query查询行为 update数据有变动行为
        switch (sqlBehavior.toString()) {
            case "query":
                // 查询语句,可带分页,或者不带分页
                ret = this.transferSelect(hasSql, bindData, page, perPage);
                break;
            case "update":
                Integer updateRow = this.transferUpdate(hasSql, bindData);
                if(updateRow <= 0) {
                    ret.put("sysErrorCode", "500");
                }
                break;
            default:
                break;
        }

        return ret;
    }


    // 转换查询
    public Map transferSelect(Map sqlData, Map bindData, String page, String perPage) {
        Map res = new HashMap<>();
        Object sql = sqlData.get("sql_string");// 获取sql语句
        Object isList = sqlData.get("is_list");// 是否查单条数据
        if(isList.equals(2)) {
            // 如果查询单条数据
            String selectSql = sql.toString();
            res = dbApiDao.dbCustomQueryForMap(selectSql, bindData);

        } else if(isList.equals(1)) {
            // 如果是查询列表
            Integer nowPage = Integer.parseInt(page);  // 当前第几页
            Integer nowPerPage = Integer.parseInt(perPage); // 每页多少条
            String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + sql + " ) AS ROMA";
            Integer count = dbApiDao.dbCountQueryForObject(countSql, bindData);
            Integer maxPage = count / nowPerPage + (count % nowPerPage != 0 ? 1 : 0);// 总数/每页多少条=最多多少页
            if (nowPage <= 1) {
                nowPage = 1;
            } else if (maxPage > 0 && nowPage >= maxPage) {
                nowPage = maxPage;
            }
            Integer startNum = (nowPage - 1) * nowPerPage;
            String selectSql = sql + " LIMIT " + startNum + "," + nowPerPage;
            List info = dbApiDao.dbCustomQueryForList(selectSql, bindData);
            res.put("count", count);
            res.put("rows", info);
        }

        return  res;
    }

    // 执行增删改
    public Integer transferUpdate(Map sqlData, Map bindData){
        Object sql = sqlData.get("sql_string");// 获取sql语句
        Integer ret = dbApiDao.dbUpdate(sql.toString(), bindData);

        return  ret;
    }
}
