package com.keyware.shandan.dynacmicform.core.basic.services;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.dynacmicform.core.basic.entity.FormInfoPo;
import com.keyware.shandan.dynacmicform.core.basic.entity.QueryParam;
import com.keyware.shandan.dynacmicform.core.db.SqlHelper;
import com.keyware.shandan.dynacmicform.core.db.executor.SqlExecutor;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.*;

@Service
public class DynamicDataService {

    private final SqlExecutor sqlExecutor;

    public DynamicDataService(SqlExecutor sqlExecutor) {
        this.sqlExecutor = sqlExecutor;
    }


    /**
     * 根据ID删除数据
     *
     * @param form 表单
     * @param id   数据主键
     * @return 是否删除成功
     * @throws Exception -
     */
    public boolean deleteById(FormInfoPo form, Integer id) throws Exception {
        QueryParam param = QueryParam.builder().column("ID").eq(id).build();
        return sqlExecutor.executeUpdate(SqlHelper.deleteSql(form, param), id);

    }


    /**
     * 保存或者更新数据
     *
     * @param form   表单
     * @param entity 待保存或者更新的数据
     * @return 是否执行成功
     * @throws Exception -
     */
    public Object saveOrUpdate(FormInfoPo form, Map<String, Object> entity) throws Exception {
        String id = (String) entity.get("ID");
        if (StringUtils.isBlank(id)) {
            return insert(form, entity);
        }
        return updateById(form, entity);
    }

    /**
     * 插入数据
     *
     * @param form   表单
     * @param params 待插入的数据
     * @return 是否插入成功
     * @throws Exception -
     */
    public boolean insert(FormInfoPo form, Map<String, Object> params) throws Exception {
        // 插入Sql，去除主键列
        params.remove("ID");
        LinkedHashMap<String, Object> data = new LinkedHashMap<>(params);
        return sqlExecutor.executeUpdate(SqlHelper.insertSql(form, data), data.values().toArray());
    }

    /**
     * 根据ID更新数据
     *
     * @param form   表单
     * @param params 更新参数
     * @return 是否更新成功
     * @throws Exception -
     */
    public boolean updateById(FormInfoPo form, Map<String, Object> params) throws Exception {
        Object id = params.remove("ID");
        LinkedHashMap<String, Object> dataMap = new LinkedHashMap<>(params);
        QueryParam queryParam = QueryParam.builder().column("ID").eq(id).build();
        List<Object> paramValues = new ArrayList<>(dataMap.values());
        paramValues.addAll(queryParam.getParamValues());
        return sqlExecutor.executeUpdate(SqlHelper.updateSql(form, dataMap, queryParam), paramValues.toArray());
    }

    /**
     * 根据ID查询数据信息
     *
     * @param form 表单信息
     * @param id   数据ID
     * @return 数据
     * @throws Exception -
     */
    public Map<String, Object> getById(FormInfoPo form, @NotNull Integer id) throws Exception {
        QueryParam param = QueryParam.builder().column("ID").eq(id).build();
        List<Map<String, Object>> list = sqlExecutor.executeQuery(SqlHelper.selectSql(form, param), id);
        if (list == null) {
            return null;
        }
        return list.get(0);
    }


    /**
     * 分页查询数据
     *
     * @param form   表单
     * @param params 参数
     * @return 分页数据
     * @throws Exception -
     */
    public Page<Map<String, Object>> pageList(FormInfoPo form, QueryParam params) throws Exception {
        List<Map<String, Object>> countList = sqlExecutor.executeQuery(SqlHelper.countSql(form, params), params.getParamValues().toArray());
        long count = 0;
        if (countList != null && countList.size() > 0) {
            count = (long) countList.get(0).get("COUNT");
        }
        List<Map<String, Object>> dataList = sqlExecutor.executeQuery(SqlHelper.pageSelectSql(form, params), params.getParamValues().toArray());
        Page<Map<String, Object>> page = new Page<>(params.getPage(), params.getPageSize(), count);
        page.setRecords(dataList);
        return page;
    }

    /**
     * 查询所有数据
     *
     * @param form
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> list(FormInfoPo form) throws Exception {
        return sqlExecutor.executeQuery(SqlHelper.selectSql(form, null));
    }
}
