package com.ebts.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.core.entity.Result;
import com.ebts.system.dao.RealQueryServiceDao;
import com.ebts.system.entity.RealUniCon;
import com.ebts.system.entity.RealUniQuery;
import com.ebts.system.entity.RelColumnInfo;
import com.ebts.system.entity.vo.RealInfo;
import com.ebts.system.service.RealQueryService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author 18209
 * @Date 2021/2/22 18:04
 * @Version 1.0
 */
@Service
public class RealQueryServiceImpl implements RealQueryService {
    private Logger logger = LoggerFactory.getLogger(RealQueryServiceImpl.class);
    @Autowired
    private RealQueryServiceDao realQueryServiceDao;

    @Override
    public List<RelColumnInfo> getInfoList(Long id) {
        return realQueryServiceDao.queryRealColumnInfo(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<RealInfo> RealInfo(Long id) {
            List<RealUniQuery> uniCons = realQueryServiceDao.queryRealInfo(id);
            List<RelColumnInfo> infoList = realQueryServiceDao.queryRealColumnInfo(id);
            return Result.ok(new RealInfo(uniCons,infoList));
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<List<Map<String, Object>>> RealData(RealUniQuery realUniQuery, Integer type) {
            List<RealUniCon> realUniCons = realUniQuery.getUniCons();
            Integer pageNum = null, pageSize = null;
            if (type == 2) {
                pageNum = realUniQuery.getPageNum();
                pageSize = realUniQuery.getPageSize();
            }
            realUniQuery = realQueryServiceDao.selectRealUniQueryById(realUniQuery.getId());
            StringBuilder sql = new StringBuilder(realUniQuery.getUqSql().toLowerCase());
            if (realUniQuery.getIsRelease() == 0) {
                return Result.error(ReturnConstants.STATE_ERROR);
            } else {
                if (realUniCons.size() > 0) {
                    sql.append(" where 1 = 1 ");
                    for (RealUniCon realUniCon : realUniCons) {
                        sql.append(conversionReal(realUniCon));
                    }
                }
                if (type == 2) {
                    PageHelper.startPage(pageNum, pageSize, "");
                }
                List<Map<String, Object>> dataMap = realQueryServiceDao.realUniQuery(sql.toString());
                return Result.ok(realUniQuery.getUqName(),dataMap);
            }
    }

    public String conversionReal(RealUniCon realUniCon) {
        if (realUniCon.getUcReal() == null) {
            return "";
        } else if (realUniCon.getUcReal().equals("")){
            return "";
        }
        else {
            String sql = " and ";
            switch (realUniCon.getUcCon()) {
                case "EQ":
                    sql += realUniCon.getUcKey() + " = '" + realUniCon.getUcReal() + "'";
                    break;
                case "NE":
                    sql += realUniCon.getUcKey() + " != '" + realUniCon.getUcReal() + "'";
                    break;
                case "GT":
                    sql += realUniCon.getUcKey() + " > '" + realUniCon.getUcReal() + "'";
                    break;
                case "GTE":
                    sql += realUniCon.getUcKey() + " >= '" + realUniCon.getUcReal() + "'";
                    break;
                case "LT":
                    sql += realUniCon.getUcKey() + " < '" + realUniCon.getUcReal() + "'";
                    break;
                case "LTE":
                    sql += realUniCon.getUcKey() + " <= '" + realUniCon.getUcReal() + "'";
                    break;
                case "LIKE":
                    sql += realUniCon.getUcKey() + " like '%" + realUniCon.getUcReal() + "%'";
                    break;
                case "BETWEEN":
                    if (realUniCon.getUcType().equals("input")) {
                        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(realUniCon.getUcReal());
                        Object begin = jsonObject.get("begin");
                        Object end = jsonObject.get("end");
                        if (begin != null && end != null) {
                            sql += realUniCon.getUcKey() + " between '" + begin + "' AND '" + end + "'";
                        } else {
                            sql = "";
                        }
                    } else if (realUniCon.getUcType().equals("datetime")) {
                        List<String> list = (ArrayList<String>) realUniCon.getUcReal();
                        if (list.size() == 2) {
                            String startTime = list.get(0);
                            String endTime = list.get(1);
                            sql += realUniCon.getUcKey() + " between '" + startTime + "' AND '" + endTime + "'";
                        } else {
                            sql = "";
                        }
                    }
                    break;
            }
            return sql;
        }
    }

}
