Beispiel #1
0
        /*
         * public static string CommonHttpRequest(string data, string uri, string type)
         * {
         *
         *  //Web访问对象,构造请求的url地址
         *  string serviceUrl = uri;
         *
         *  //构造http请求的对象
         *  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
         *  myRequest.Timeout = 10000;
         *  //转成网络流
         *  byte[] buf = ENCODING.GetBytes(data);
         *  //设置
         *  myRequest.Method = type;
         *  myRequest.ContentLength = buf.LongLength;
         *  myRequest.ContentType = "application/json";
         *
         *  //将客户端IP加到头文件中
         *
         *   //string sRealIp = GetHostAddress();
         *  //if (!string.IsNullOrEmpty(sRealIp))
         *  //{
         *   //   myRequest.Headers.Add("ClientIp", sRealIp);
         *  //}
         *
         *
         *
         *  using (Stream reqstream = myRequest.GetRequestStream())
         *  {
         *      reqstream.Write(buf, 0, (int)buf.Length);
         *  }
         *  HttpWebResponse resp = myRequest.GetResponse() as HttpWebResponse;
         *  System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream(), ENCODING);
         *  string result = reader.ReadToEnd();
         *  reader.Close();
         *  resp.Close();
         *  return result;
         * }
         */

        public static string HTTPJsonGet(QuestionaireEntity entity)
        {
            string url    = getURL(entity);
            string result = string.Empty;

            try
            {
                //测试时候使用的接口
                //url = "http://192.168.0.102:8000/hello/sleep";
                System.Diagnostics.Debug.WriteLine("开始调用接口: " + url);
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.ContentType      = "application/json";
                request.Method           = "GET";
                request.ReadWriteTimeout = 1000;
                request.Timeout          = 2000;
                HttpWebResponse        resp   = request.GetResponse() as HttpWebResponse;
                System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream(), ENCODING);
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                logForm log = logForm.InstanceLogForm();
                log.addLog(CreateController.getLogStr(entity, "  刷题入库失败! 刷题结果::" + ex.Message));
                System.Diagnostics.Debug.WriteLine("HTTPJsonGet调用超时或者异常:" + ex.Message);
            }


            //string result = "{\"code\":200,\"msg\":\"问卷添加成功,感谢您的参与!\",\"data\":[]}";
            return(result);
        }
        public static void slotTimeChange(object sender, EventArgs e)
        {
            //刷题间隔时间改变事件
            NumericUpDown slotNum = (NumericUpDown)sender;

            try
            {
                int value = int.Parse(slotNum.Value.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数字!");
            }
            //加上锁 进行对QuestionaireData.result; 的修改
            lock (syncLocker)
            {
                logForm log = logForm.InstanceLogForm();
                List <QuestionaireEntity> questionList = QuestionaireData.result;
                QuestionaireEntity        entity       = new QuestionaireEntity();
                foreach (var q in questionList)
                {
                    if ("slotNum_" + q.SeriaNum == slotNum.Name)
                    {
                        QuestionaireData.result.Remove(q);
                        entity = q;
                        break;
                    }
                }
                entity.SlotTime = int.Parse(slotNum.Value.ToString());
                QuestionaireData.result.Add(entity);
                log.addLog("序号: " + entity.Code + "修改了刷题间隔为: " + entity.SlotTime);
            }
            System.Diagnostics.Debug.WriteLine("刷题间隔时间修改为: " + slotNum.Value);
        }
        /**
         * 此方法只能被调用一次
         */
        public static List <QuestionaireEntity> getData()
        {
            try
            {
                if (result.Count > 0)
                {
                    //result 有值,则返回这个值,避免多次调用出现bug
                    return(result);
                }
                string str = System.AppDomain.CurrentDomain.BaseDirectory;
                System.Diagnostics.Debug.WriteLine("文件根路径:" + str);
                string excelPath = str + "刷题配置.xlsx";

                Application excel = new Application();
                excel.Visible       = false;
                excel.DisplayAlerts = false;

                object missing = System.Reflection.Missing.Value;
                // 以只读的形式打开EXCEL文件
                Workbook wb = excel.Application.Workbooks.Open(excelPath, missing, true, missing, missing, missing,
                                                               missing, missing, missing, true, missing, missing, missing, missing, missing);
                //取得第一个工作薄
                Worksheet ws       = (Worksheet)wb.Worksheets.get_Item(1);
                int       rowCount = 0;
                try
                {
                    rowCount = ws.UsedRange.Rows.Count;//赋值有效行

                    string ordernum = string.Empty;
                    string count    = string.Empty;

                    //循环行
                    for (int i = 2; i <= rowCount; i++)//
                    {
                        if (ws.Rows[i] != null)
                        {
                            QuestionaireEntity entity = new QuestionaireEntity();
                            entity.SeriaNum = i.ToString();
                            entity.KindNum  = ((Range)ws.Cells[i, 1]).Value2.ToString();
                            entity.Name     = ((Range)ws.Cells[i, 2]).Value2.ToString();
                            entity.Code     = ((Range)ws.Cells[i, 3]).Value2.ToString();
                            entity.Address  = ((Range)ws.Cells[i, 4]).Value2.ToString();

                            try
                            {
                                entity.QuestionNum = int.Parse(((Range)ws.Cells[i, 5]).Value2.ToString());
                                entity.UpNum       = int.Parse(((Range)ws.Cells[i, 7]).Value2.ToString());
                                entity.SlotTime    = int.Parse(((Range)ws.Cells[i, 8]).Value2.ToString());
                            }
                            catch (Exception)
                            {
                                //答题数量和范围数量对不上
                                logForm log = logForm.InstanceLogForm();
                                log.addLog("问卷编号:" + entity.Code + " 题目数量/刷题上限/刷题时间间隔,为非法数字,请确认...");
                                continue;
                            }
                            Dictionary <int, int> ansDict = new Dictionary <int, int>();

                            //获取答题答案范围
                            string   ansScope      = ((Range)ws.Cells[i, 6]).Value2.ToString();
                            String[] ansScopeArray = ansScope.Split(',');
                            if (entity.QuestionNum != ansScopeArray.Length)
                            {
                                //答题数量和范围数量对不上
                                logForm log = logForm.InstanceLogForm();
                                log.addLog("问卷编号:" + entity.Code + " 答题数量和答题范围 的数量匹配不上,请确认...");
                                continue;
                            }

                            bool continueStatus = false;
                            for (int ansIndex = 1; ansIndex <= ansScopeArray.Length; ansIndex++)
                            {
                                string scopeStr = ansScopeArray[ansIndex - 1];
                                try
                                {
                                    int scopInt = int.Parse(scopeStr);
                                    ansDict.Add(ansIndex, scopInt);
                                }
                                catch (Exception)
                                {
                                    logForm log = logForm.InstanceLogForm();
                                    log.addLog("问卷编号:" + entity.Code + " 答案范围内有非法数字,请确认...");
                                    continueStatus = true;
                                    continue;
                                }
                            }
                            if (continueStatus)
                            {
                                continue;
                            }

                            entity.AnswerScope = ansDict;
                            result.Add(entity);
                        }
                    }
                    //循环列
                    //for (int i = 1; i <= ws.UsedRange.Columns.Count; i++)
                    // {
                    //ws.Columns[i]
                    //}
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("读取excel出错:" + ex.Message);
                }
                finally
                {
                    ClosePro(excelPath, excel, wb);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("读取excel出错:" + ex.Message);
            }

            /**
             * QuestionaireEntity entity20_1 = new QuestionaireEntity();
             *
             * QuestionaireEntity entity21_1 = new QuestionaireEntity();
             *
             * //
             * //医疗机构医疗器械使用情况调研问卷(一)
             * //
             *
             * entity20_1.SeriaNum = "1";
             * entity20_1.KindNum = "5";
             * entity20_1.Name = "医疗机构医疗器械使用情况调研问卷(一)";
             * entity20_1.Code = "20-1";
             * entity20_1.QuestionNum = 11;
             * entity20_1.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=25&suid=25&puid=46";
             * Dictionary<int, int> dict1 = new Dictionary<int, int>();
             * dict1.Add(1, 2);
             * dict1.Add(2, 2);
             * dict1.Add(3, 2);
             * dict1.Add(4, 4);
             * dict1.Add(5, 3);
             * dict1.Add(6, 3);
             * dict1.Add(7, 3);
             * dict1.Add(8, 2);
             * dict1.Add(9, 3);
             * dict1.Add(10, 3);
             * dict1.Add(11, 4);
             * entity20_1.AnswerScope = dict1;
             * result.Add(entity20_1);
             *
             * QuestionaireEntity entity20_2 = entity20_1.Copy();
             * entity20_2.SeriaNum = "2";
             * entity20_2.Code = "20-2";
             * entity20_2.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=25&suid=25&puid=48";
             * result.Add(entity20_2);
             *
             * QuestionaireEntity entity20_3 = entity20_1.Copy();
             * entity20_3.SeriaNum = "3";
             * entity20_3.Code = "20-3";
             * entity20_3.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=25&suid=25&puid=50";
             * result.Add(entity20_3);
             *
             * QuestionaireEntity entity20_4 = entity20_1.Copy();
             * entity20_4.SeriaNum = "4";
             * entity20_4.Code = "20-4";
             * entity20_4.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=27&suid=13&puid=46";
             * result.Add(entity20_4);
             *
             * QuestionaireEntity entity20_5 = entity20_1.Copy();
             * entity20_5.SeriaNum = "5";
             * entity20_5.Code = "20-5";
             * entity20_5.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=27&suid=13&puid=49";
             * result.Add(entity20_5);
             *
             * QuestionaireEntity entity20_6 = entity20_1.Copy();
             * entity20_6.SeriaNum = "6";
             * entity20_6.Code = "20-6";
             * entity20_6.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=23&suid=7&puid=14";
             * result.Add(entity20_6);
             *
             * QuestionaireEntity entity20_7 = entity20_1.Copy();
             * entity20_7.SeriaNum = "7";
             * entity20_7.Code = "20-7";
             * entity20_7.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=23&suid=7&puid=47";
             * result.Add(entity20_7);
             *
             * QuestionaireEntity entity20_8 = entity20_1.Copy();
             * entity20_8.SeriaNum = "8";
             * entity20_8.Code = "20-8";
             * entity20_8.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=20&eid=23&suid=7&puid=51";
             * result.Add(entity20_8);
             *
             * QuestionaireEntity entity20_9 = entity20_1.Copy();
             * entity20_9.SeriaNum = "9";
             * entity20_9.Code = "20-9";
             * entity20_9.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=26&suid=25&puid=15";
             * result.Add(entity20_9);
             *
             * QuestionaireEntity entity20_10 = entity20_1.Copy();
             * entity20_10.SeriaNum = "10";
             * entity20_10.Code = "20-10";
             * entity20_10.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=26&suid=25&puid=47";
             * result.Add(entity20_10);
             *
             * QuestionaireEntity entity20_11 = entity20_1.Copy();
             * entity20_11.SeriaNum = "11";
             * entity20_11.Code = "20-11";
             * entity20_11.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=26&suid=25&puid=49";
             * result.Add(entity20_11);
             *
             *
             * //
             * //医疗机构医疗器械使用情况调研问卷(二)
             * //
             *
             * entity21_1.SeriaNum = "12";
             * entity21_1.KindNum = "6";
             * entity21_1.Name = "医疗机构医疗器械使用情况调研问卷(二)";
             * entity21_1.Code = "21-1";
             * entity21_1.QuestionNum = 36;
             * entity21_1.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=28&suid=13&puid=48";
             * Dictionary<int, int> dict2 = new Dictionary<int, int>();
             * dict2.Add(1, 4);
             * dict2.Add(2, 3);
             * dict2.Add(3, 3);
             * dict2.Add(4, 3);
             * dict2.Add(5, 3);
             * dict2.Add(6, 3);
             * dict2.Add(7, 5);
             * dict2.Add(8, 5);
             * dict2.Add(9, 4);
             * dict2.Add(10, 3);
             * dict2.Add(11, 3);
             * dict2.Add(12, 4);
             * dict2.Add(13, 4);
             * dict2.Add(14, 4);
             * dict2.Add(15, 5);
             * dict2.Add(16, 4);
             * dict2.Add(17, 4);
             * dict2.Add(18, 4);
             * dict2.Add(19, 4);
             * dict2.Add(20, 5);
             * dict2.Add(21, 5);
             * dict2.Add(22, 5);
             * dict2.Add(23, 5);
             * dict2.Add(24, 3);
             * dict2.Add(25, 3);
             * dict2.Add(26, 3);
             * dict2.Add(27, 5);
             * dict2.Add(28, 3);
             * dict2.Add(29, 3);
             * dict2.Add(30, 4);
             * dict2.Add(31, 4);
             * dict2.Add(32, 7);
             * dict2.Add(33, 5);
             * dict2.Add(34, 5);
             * dict2.Add(35, 5);
             * dict2.Add(36, 4);
             * entity21_1.AnswerScope = dict2;
             * result.Add(entity21_1);
             *
             * QuestionaireEntity entity21_2 = entity21_1.Copy();
             * entity21_2.SeriaNum = "13";
             * entity21_2.Code = "21-2";
             * entity21_2.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=28&suid=13&puid=51";
             * result.Add(entity21_2);
             *
             * QuestionaireEntity entity21_3 = entity21_1.Copy();
             * entity21_3.SeriaNum = "14";
             * entity21_3.Code = "21-3";
             * entity21_3.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=24&suid=7&puid=15";
             * result.Add(entity21_3);
             *
             *
             * QuestionaireEntity entity21_4 = entity21_1.Copy();
             * entity21_4.SeriaNum = "15";
             * entity21_4.Code = "21-4";
             * entity21_4.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=24&suid=7&puid=14";
             * result.Add(entity21_4);
             *
             * QuestionaireEntity entity21_5 = entity21_1.Copy();
             * entity21_5.SeriaNum = "16";
             * entity21_5.Code = "21-5";
             * entity21_5.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=21&eid=24&suid=7&puid=46";
             * result.Add(entity21_5);
             *
             **/

            /**
             * entity1.SeriaNum = "1";
             * entity1.Name = "测试";
             * entity1.QuestionNum = 11;
             * entity1.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=5&eid=9&suid=25&puid=26";
             * Dictionary<int, int> dict1 = new Dictionary<int, int>();
             * dict1.Add(1, 2);
             * dict1.Add(2, 3);
             * dict1.Add(3, 3);
             * dict1.Add(4, 3);
             * dict1.Add(5, 3);
             * dict1.Add(6, 4);
             * dict1.Add(7, 4);
             * dict1.Add(8, 4);
             * dict1.Add(9, 4);
             * dict1.Add(10, 5);
             * dict1.Add(11, 5);
             * entity1.AnswerScope = dict1;
             * result.Add(entity1);
             *
             * entity2.SeriaNum = "2";
             * entity2.Name = "骨科观念调查问卷";
             * entity2.QuestionNum = 13;
             * entity2.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=15&eid=16&suid=25&puid=15";
             * Dictionary<int, int> dict2 = new Dictionary<int, int>();
             * dict2.Add(1, 4);
             * dict2.Add(2, 5);
             * dict2.Add(3, 4);
             * dict2.Add(4, 9);
             * dict2.Add(5, 4);
             * dict2.Add(6, 5);
             * dict2.Add(7, 4);
             * dict2.Add(8, 4);
             * dict2.Add(9, 4);
             * dict2.Add(10, 5);
             * dict2.Add(11, 4);
             * dict2.Add(12, 7);
             * dict2.Add(13, 6);
             *
             * entity2.AnswerScope = dict2;
             * result.Add(entity2);
             *
             *
             * entity3.SeriaNum = "3";
             * entity3.Name = "内分泌科观念调查问卷";
             * entity3.QuestionNum = 13;
             * entity3.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=16&eid=17&suid=25&puid=15";
             * Dictionary<int, int> dict3 = new Dictionary<int, int>();
             * dict3.Add(1, 4);
             * dict3.Add(2, 5);
             * dict3.Add(3, 4);
             * dict3.Add(4, 9);
             * dict3.Add(5, 4);
             * dict3.Add(6, 5);
             * dict3.Add(7, 4);
             * dict3.Add(8, 6);
             * dict3.Add(9, 4);
             * dict3.Add(10, 5);
             * dict3.Add(11, 4);
             * dict3.Add(12, 7);
             * dict3.Add(13, 6);
             * entity3.AnswerScope = dict3;
             * result.Add(entity3);
             *
             *
             * entity4.SeriaNum = "4";
             * entity4.Name = "高血压知识问卷";
             * entity4.QuestionNum = 27;
             * entity4.Address = "http://www.masghkj.com/index.php?s=questionnaire&c=options&m=index&cid=18&eid=18&suid=25&puid=15";
             * Dictionary<int, int> dict4 = new Dictionary<int, int>();
             * dict4.Add(1, 8);
             * dict4.Add(2, 4);
             * dict4.Add(3, 4);
             * dict4.Add(4, 4);
             * dict4.Add(5, 5);
             * dict4.Add(6, 5);
             * dict4.Add(7, 5);
             * dict4.Add(8, 4);
             * dict4.Add(9, 4);
             * dict4.Add(10, 3);
             * dict4.Add(11, 2);
             * dict4.Add(12, 2);
             * dict4.Add(13, 4);
             * dict4.Add(14, 4);
             * dict4.Add(15, 4);
             * dict4.Add(16, 3);
             * dict4.Add(17, 4);
             * dict4.Add(18, 4);
             * dict4.Add(19, 4);
             * dict4.Add(20, 4);
             * dict4.Add(21, 4);
             * dict4.Add(22, 4);
             * dict4.Add(23, 5);
             * dict4.Add(24, 7);
             * dict4.Add(25, 6);
             * dict4.Add(26, 7);
             * dict4.Add(27, 7);
             * entity4.AnswerScope = dict4;
             * result.Add(entity4);
             *
             **/

            return(result);
        }
        public static void exercise(Form1 mainForm, QuestionaireEntity questEntity)
        {
            QuestionaireEntity entity = questEntity;
            logForm            log    = logForm.InstanceLogForm();

            log.addLog(getLogStr(entity, " 开始刷题"));

            int tempNum = 0;

            successNum.TryGetValue(entity.SeriaNum, out tempNum);

            while (tempNum < entity.UpNum && !entity.CloseThread)
            {
                System.Diagnostics.Debug.WriteLine("开始调用接口,调用时间间隔:" + (entity.SlotTime * 1000).ToString());

                // 进行接口调用,判断是否调用成功,更新主界面数据,调用记录保存
                String resultStr = HttpUtil.HTTPJsonGet(entity);
                System.Diagnostics.Debug.WriteLine("接口返回: " + resultStr);

                //解析返回的结果
                JavaScriptObject re = JavaScriptConvert.DeserializeObject <JavaScriptObject>(resultStr);
                if (re == null)
                {
                    //log.addLog(getLogStr(entity, " 刷题入库失败! 刷题结果: " + resultStr));
                    Thread.Sleep(entity.SlotTime * 1000);
                    continue;
                }
                string msg = re["msg"].ToString();
                System.Diagnostics.Debug.WriteLine("code: " + re["code"].ToString());
                System.Diagnostics.Debug.WriteLine("msg: " + msg);
                if ("200".Equals(re["code"].ToString()))
                {
                    if (msg.Contains("问卷添加成功"))
                    {
                        //添加成功了
                        int num = 0;
                        if (successNum.ContainsKey(entity.SeriaNum))
                        {
                            //有这个线程的完成刷题的数量值
                            successNum.TryGetValue(entity.SeriaNum, out num);
                        }
                        else
                        {
                            //没有这个线程的完成刷题的数量值
                            successNum.Add(entity.SeriaNum, 0);
                        }
                        num++;
                        //新值加入到集合中
                        successNum.Remove(entity.SeriaNum);
                        successNum.Add(entity.SeriaNum, num);
                        //修改对应label的值
                        foreach (var control in mainForm.Controls)
                        {
                            if (control.GetType().Name.Equals("Label"))
                            {
                                Label  lb        = (Label)control;
                                string labelName = "label_" + entity.SeriaNum;
                                if (labelName.Equals(lb.Name))
                                {
                                    //找到了对应的label,开始修改值
                                    lb.Text = "完成数量: " + num;
                                    tempNum = num;
                                }
                            }
                        }
                        //添加日志
                        log.addLog(getLogStr(entity, " 刷题入库成功! 刷题结果: " + msg));
                    }
                    else
                    {
                        //已经填写过了问卷或者其他情况
                        log.addLog(getLogStr(entity, " 刷题入库失败! 刷题结果: " + msg));
                    }
                }
                List <QuestionaireEntity> questionList = QuestionaireData.result;
                foreach (var q in questionList)
                {
                    if (q.SeriaNum == entity.SeriaNum)
                    {
                        entity = q;
                        break;
                    }
                }
                //需要暂停20秒左右的时间,等待ip更新
                Thread.Sleep(entity.SlotTime * 1000);
            }


            log.addLog(getLogStr(entity, " 刷题结束 "));
            //走到这里,刷单线程,即将结束
            //修改按钮的显示
            foreach (var control in mainForm.Controls)
            {
                if (control.GetType().Name.Equals("Button"))
                {
                    Button tmpBtn = (Button)control;
                    if (entity.SeriaNum.Equals(tmpBtn.Name))
                    {
                        //找到了触发此线程的控件,在线程完成任务后,修改按钮状态
                        tmpBtn.Text    = "单条执行";
                        tmpBtn.Enabled = true;
                        //先维护好线程集合
                        threadDict.Remove(entity.SeriaNum);
                    }
                }
            }
        }