using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace RMYY_CallCenter_Api.Bll
{
   public class AddLogBll
    {
        T_Dis_WorkOrderItem itembll = new T_Dis_WorkOrderItem();
        /// <summary>
        /// 添加工单记录
        /// </summary>
        public long AddLog(int woid, int wostate, int type, string content, int itemtype, int opttype, string nextuser, int nextdept, string usercode,  int F_IsUsed = 0)
        {

            Model.T_Dis_WorkOrderItem itemModel = new Model.T_Dis_WorkOrderItem();
            #region 
            long res = 0;
            //处理当前工单的记录之前的记录过期
            var itemlast = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + " and  (F_CreateUser=" + usercode + " or F_NextUser like '%" + usercode + "%')");
            using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 10, 0)))
            {
                if (F_IsUsed == 0)
                {

                    if (itemlast.Count > 0)
                    {
                        foreach (var it in itemlast)
                        {
                            it.F_IsUsed = 1;
                            itembll.Update(it);
                        }
                    }
                }
                #endregion
                itemModel.F_WoID = woid;
                itemModel.F_WoState = wostate;
                itemModel.F_WoType = type;
                itemModel.F_ItemType = itemtype;
                itemModel.F_OptType = opttype;
                itemModel.F_OptContent = content;
                itemModel.F_NextUser = nextuser;
                itemModel.F_NextDept = nextdept;
                itemModel.F_IsUsed = F_IsUsed;
                itemModel.F_CreateTime = DateTime.Now;
                itemModel.F_CreateUser = usercode;
                res = itembll.Add(itemModel);
                trans.Complete();
            }
            return res;
        }
        public long AddLogAboutKey(int woid, int wostate, int type, string content, int itemtype, int opttype, string nextuser, int nextdept, string usercode,int key,string value,string reason ,int F_IsUsed = 0)
        {

            Model.T_Dis_WorkOrderItem itemModel = new Model.T_Dis_WorkOrderItem();
            #region 
            long res = 0;
            //处理当前工单的记录之前的记录过期
            var itemlast = itembll.GetModelList(" isnull(F_IsUsed,'0')='0' and F_ItemType=" + itemtype + " and  F_WoID=" + woid + " and  (F_CreateUser=" + usercode + " or F_NextUser like '%" + usercode + "%')");
            using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 10, 0)))
            {
                if (F_IsUsed == 0)
                {

                    if (itemlast.Count > 0)
                    {
                        foreach (var it in itemlast)
                        {
                            it.F_IsUsed = 1;
                            itembll.Update(it);
                        }
                    }
                }
                #endregion
                itemModel.F_WoID = woid;
                itemModel.F_WoState = wostate;
                itemModel.F_WoType = type;
                itemModel.F_ItemType = itemtype;
                itemModel.F_OptType = opttype;
                itemModel.F_OptContent = content;
                itemModel.F_NextUser = nextuser;
                itemModel.F_NextDept = nextdept;
                itemModel.F_IsUsed = F_IsUsed;
                itemModel.F_CreateTime = DateTime.Now;
                itemModel.F_CreateUser = usercode;

                itemModel.F_AboutKey = key;
                itemModel.F_AboutReason = value;
                itemModel.F_EnterReason = reason;

                res = itembll.Add(itemModel);
                trans.Complete();
            }
            return res;
        }

    }
}
