package cc.smtweb.biz.mztb.web.widgetManage;

import cc.smtweb.biz.mztb.web.base.DbSourceConfig;
import cc.smtweb.biz.mztb.web.tool.DbUtil;
import cc.smtweb.biz.mztb.web.tool.UtilPub;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.mvc.service.SwListData;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageListHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageLoadHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TaskMonthHandler {
    static class TaskMonthLoadHandler extends DynPageLoadHandler {

        @Override
        public DynPageListHandler getListWorker(SwMap filter, PageDataset pageDataSet) {
            TaskMonthListHandler taskMonthListHandler = new TaskMonthListHandler(form.getId(), filter, pageDataSet);
            taskMonthListHandler.init(filter, us);
            return taskMonthListHandler;
        }
    }


    static class TaskMonthListHandler extends DynPageListHandler {

        @Override
        public void init(SwMap params, UserSession us) {
            this.params = params;
            this.us = us;
        }

        @Override
        public SwListData buildListData() {
            List<SwMap> listData = new ArrayList<>();
            List<DbSourceConfig> dbSourceConfigs = DbEngine.getInstance().queryWhere(DbSourceConfig.class, " dsc_use_state = 0");
            if(dbSourceConfigs == null || dbSourceConfigs.size() < 1) {
                throw new BizException("请联系管理员配置前置库连接");
            }
            DbSourceConfig dbSourceConfig = dbSourceConfigs.get(0);
            getTaskMonth(dbSourceConfig,listData);
            return SwListData.create(listData);
        }

        public TaskMonthListHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
        }

        private void getTaskMonth(DbSourceConfig dbSourceConfig, List<SwMap> listData) {
            // 找到已填报的数据 过滤
//            DbEngine preDbEngine = DbUtil.getInstance(dbSourceConfig.getWholeUrl(),dbSourceConfig.getUsername(),dbSourceConfig.getPassword())
//                    .getDbEngine(dbSourceConfig.getDbType());
            Connection connection = null;
            try {
                connection = DriverManager
                        .getConnection(dbSourceConfig.getWholeUrl(),dbSourceConfig.getUsername(),dbSourceConfig.getPassword());
                DbUtil dbUtil =new DbUtil(connection);
                ResultSet query = dbUtil.dbHandSql("select * from qz_fill_task", "query");
                while (query.next()) {
                    SwMap swMap = new SwMap();
                    swMap.put("id",query.getLong("ft_id"));
                    swMap.put("task",query.getString("fi_task_month"));
                    swMap.put("text",query.getString("fi_task_month").contains("99") ?
                            query.getString("fi_task_month").replace("99","年报")
                            : UtilPub.getDateStr(query.getString("fi_task_month")));
                    listData.add(swMap);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
}
