#include "forbid_staff_down_mine.h"
#include "log.h"
#include "tool_time.h"
#include "db_api/CDBSingletonDefine.h"

void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/,int etype)
{
    std::string sql = "select id,staff_id,start_time,end_time,status,oper_time,last_update from rt_person_forbid_down_mine where ";
    if (id != -1)
        sql += " id = " + std::to_string(id) + ";";
    else
        sql += "status = 1 and end_time > now();";
    std::string Error;
    YADB::CDBResultSet DBRes;
    sDBConnPool.Query(sql.c_str(),DBRes,Error);
    int nCount = DBRes.GetRecordCount( Error );
    if (nCount < 1)
    {
        //log_info("增加或修改失败,数据库中没有记录: sql=%s", sql.c_str());
        return ;
    }
    while ( DBRes.GetNextRecod(Error) )
    {
        int key = 0;
        DBRes.GetField("id", key, Error);
        unsigned int staff_id = 0;
        DBRes.GetField("staff_id", staff_id, Error);
        std::string start_time;
        DBRes.GetField("start_time", start_time, Error);
        std::string end_time;
        DBRes.GetField("end_time", end_time, Error);
        int state;
        DBRes.GetField("status", state, Error);
        std::string create_time;
        DBRes.GetField("oper_time", create_time, Error);
        std::string update_time;
        DBRes.GetField("last_update", update_time, Error);
        time_t stime = tool_time::to_time(start_time);
        time_t etime = tool_time::to_time(end_time);
        std::shared_ptr<SForbidStaffList> s = forbid_staff_down_mine::instance()->get(staff_id);
        //如果存在则查找更新。查找不到则插入
        if (s)
        {
            bool f = false; // 是否修改过记录
            for (SForbidStaffInfo &info : s->forbidList)
            {
                //找到记录则更新,停止.找不到,则继续往下走
                if (info.db_id == key)
                {
                    f=true;
                    info.start_time =stime;
                    info.end_time =etime;
                    info.state = state;
                    log_info("Check Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
                                ,key,staff_id,state,start_time.c_str(),end_time.c_str());
                    break;
                }
            }
            if(f)continue;
            //ET_UPDATE
            if(id !=-1 && etype ==2)
            {
                time_t now= time(0);
                if(state!=1 || now>etime)
                    continue;
            }
        }
        else
        {
            s = std::make_shared<SForbidStaffList>();
            forbid_staff_down_mine::instance()->add(staff_id,s);
        }
        s->staff_id = staff_id;
        SForbidStaffInfo info ;
        info.staff_id = staff_id;
        info.db_id = key;
        info.start_time = stime;
        info.end_time =etime;
        info.state = state;

        log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
                    ,key,staff_id,state,start_time.c_str(),end_time.c_str());
        s->forbidList.push_back(info);
    }
}

void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
{
    auto s = forbid_staff_down_mine::instance()->get(staff_id);
    if (s != nullptr)
    {
        log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
        for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
        {
            if ((*it).db_id == id)
            {
                s->forbidList.erase(it);
                break;
            }
        }
        if(s->forbidList.size() == 0)
        {
            forbid_staff_down_mine::instance()->remove(staff_id);
        }
    }
}

//是否禁止状态
bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
{
    auto flist = forbid_staff_down_mine::instance()->m_map;
    auto it = flist.find(staff_id);
    if (it != flist.end())
    {
        for (auto &info : it->second->forbidList)
        {
            if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
            {
                return true;
            }
        }
    }
    return false;
}