package com.engine.htsc.receipt.service;

import com.engine.core.impl.Service;
import dm.jdbc.util.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

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

public class ReceiptWaitService extends Service {

    /**
     * 更新收文等待状态
     * @param requestid
     * @param userid
     * @param type
     * @return
     */
    public Map<String, Object> updateWait(String requestid, String userid, String type){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> apidatas = new HashMap<String, Object>();

        String showName = "收文等待";
        String colorval = "#C576BA";
        String operatedate = TimeUtil.getCurrentDateString();
        String operatetime = TimeUtil.getOnlyCurrentTimeString();
        String id = "";
        String sql = "select id from workflow_attentionTypeSet where userid = "+userid+" and SHOWNAME ='"+showName+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            id = Util.null2String(rs.getString("id"));
        }else{
            String insertSql = "insert into WORKFLOW_ATTENTIONTYPESET(userid,usertype,showname,colorval,showorder,operatedate,operatetime) values(?,?,?,?,?,?,?)";
            rs.executeUpdate(insertSql,new Object[]{userid,0,showName,colorval,-1,operatedate,operatetime});
            sql = "select id from workflow_attentionTypeSet where userid = "+userid+" and SHOWNAME ='"+showName+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                id = Util.null2String(rs.getString("id"));
            }
        }
        if("add".equals(type)){
            sql = "select * from workflow_attention where userid= "+userid+" and usertype= 0 and requestid = "+requestid+" and atypeid = "+id;
            bs.writeLog(sql);
            rs.execute(sql);
            if (rs.next()){
            }else {
                String addSql = "insert into workflow_attention(userid,usertype,requestid,atypeid,operatedate,operatetime) values(?,?,?,?,?,?)";
                rs.executeUpdate(addSql,new Object[]{userid,0,requestid,id,operatedate,operatetime});
            }
        }else if("delete".equals(type)){
            String delsql = "delete from workflow_attention where userid= "+userid+" and usertype= 0 and requestid = "+requestid+" and atypeid = "+id;
            bs.writeLog(delsql);
            rs.executeUpdate(delsql);
        }
        return apidatas;
    }

    /**
     * 查询收文等待状态
     * @param requestid
     * @param userid
     * @return
     */
    public Map<String, Object> getWaitStatus(String requestid, String userid){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> apidatas = new HashMap<String, Object>();

        String showName = "收文等待";
        String id = "";
        String sql = "select id from workflow_attentionTypeSet where userid = "+userid+" and SHOWNAME ='"+showName+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            id = Util.null2String(rs.getString("id"));
        }

        if(StringUtil.isNotEmpty(id)){
            String attentionid = "";
            sql = "select id from workflow_attention where userid= "+userid+" and usertype= 0 and requestid = "+requestid+" and atypeid = "+id;
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                attentionid = Util.null2String(rs.getString("id"));
            }
            if(StringUtil.isNotEmpty(attentionid)){
                apidatas.put("waitStatus",true);
            }else{
                apidatas.put("waitStatus",false);
            }
        }else {
            apidatas.put("waitStatus",false);
        }
        return apidatas;
    }
}
