using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using System.Data;
using System.Threading;
using MySystem.OpModels;

namespace MySystem
{
    public class OperateStockService
    {
        public readonly static OperateStockService Instance = new OperateStockService();
        private OperateStockService()
        { }

        public void Start()
        {
            Thread th = new Thread(dosomething);
            th.IsBackground = true;
            th.Start();
        }

        public void dosomething()
        {
            while (true)
            {
                try
                {
                    string sql = "";
                    string conn = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
                    DataTable dt = CustomerSqlConn.dtable("select UserId,sum(LaveNum)+(select count(Id) from PreSendStockDetail where FromUserId=s.UserId and `Status`>=0 and `Status`<=1 and ApplyFlag=0) from StoreHouse s where `Status`>-1 group by UserId", conn);
                    foreach(DataRow dr in dt.Rows)
                    {
                        sql += "update Users set StoreStock=" + dr[1].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
                    }
                    CustomerSqlConn.op(sql, conn);
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心返额度异常");
                }
                Thread.Sleep(600000);
            }
        }
    }
}