using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Dapper;
using DigitalSchoolApi.Models;
using Learun.Application.Organization;
using Learun.Util;
using Swagger.Net;
using Swagger.Net.Annotations;

namespace DigitalSchoolApi.Controllers
{
    /// <summary>
    /// 长阳报名
    /// </summary>
    public class CYStuVolunteerController
    {
        private readonly static string _logConnection = ConfigurationManager.ConnectionStrings["CoreDBString"].ConnectionString;
        private readonly static string _sqlConnection = ConfigurationManager.ConnectionStrings["ConnectionPfcMisDBString"].ConnectionString;
        
        /// <summary>
        /// 报名信息保存草稿24小时后，自动提交
        /// </summary>
        /// <returns></returns>
        public static void StuVolunteer()
        {
            var sql = @"update StuVolunteer set EnrollStatus=1 where ID in (
select ID from StuVolunteer where EnrollStatus='0' and DATEDIFF(MINUTE,EnrollTime,getdate())>=24*60
)";
            try
            {
                using (IDbConnection conn = new SqlConnection(_logConnection))
                {
                    conn.Execute(
                        "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'长阳报名信息自动提交','开始同步',getdate())");
                }

                using (IDbConnection conn = new SqlConnection(_sqlConnection))
                {
                    conn.Execute(sql);
                }
            }
            catch (Exception ex)
            {
                using (IDbConnection conn = new SqlConnection(_logConnection))
                {
                    conn.Execute(
                        $"insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'长阳报名信息自动提交发生异常','{ex.Message}',getdate())");
                }

            }

        }


    }
}
