using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.IDAL;
using System.Data.SqlClient;
using System.Data;
using CY.Infrastructure.Query;
using CY.Model;
using System.Transactions;
namespace CY.SQLDAL
{
public class OA_FirmAccountRecordDAL : IOA_FirmAccountRecordDAL
{
private Database _dataBase = null;
public OA_FirmAccountRecordDAL()
{
_dataBase = new Database();
}
public OA_FirmAccountRecordDAL(Database dataBase)
{
_dataBase = dataBase;
}
///
/// 新增
///
///
///
public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.OA_FirmAccountRecord trueModel = model as Model.OA_FirmAccountRecord;
if (trueModel == null)
{
return false;
}
IList sqlParms = new List()
{
new SqlParameter("@SubjectId",trueModel.SubjectId),
new SqlParameter("@AccountId",trueModel.AccountId),
new SqlParameter("@RecordTypeId",trueModel.RecordTypeId),
new SqlParameter("@Money",trueModel.Money),
new SqlParameter("@PaymentUnit",trueModel.PaymentUnit),
new SqlParameter("@OperationalMatters",trueModel.OperationalMatters),
new SqlParameter("@LastUpdateTime",trueModel.LastUpdateTime),
new SqlParameter("@Operator",trueModel.Operator),
new SqlParameter("@Remark",trueModel.Remark) ,
new SqlParameter("@ResidualAmount",trueModel.ResidualAmount) ,
new SqlParameter("@Department",trueModel.Department??"")
};
try
{
_dataBase.Query("sp_OA_FirmAccountRecord_Insert", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
///
/// 修改
///
///
///
public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.OA_FirmAccountRecord trueModel = model as Model.OA_FirmAccountRecord;
if (trueModel == null)
{
return false;
}
IList sqlParms = new List()
{new SqlParameter("@Keyid",trueModel.Keyid),
new SqlParameter("@SubjectId",trueModel.SubjectId),
new SqlParameter("@AccountId",trueModel.AccountId),
new SqlParameter("@RecordTypeId",trueModel.RecordTypeId),
new SqlParameter("@Money",trueModel.Money),
new SqlParameter("@PaymentUnit",trueModel.PaymentUnit),
new SqlParameter("@OperationalMatters",trueModel.OperationalMatters),
new SqlParameter("@LastUpdateTime",trueModel.LastUpdateTime),
new SqlParameter("@Operator",trueModel.Operator),
new SqlParameter("@Remark",trueModel.Remark) ,
new SqlParameter("@ResidualAmount",trueModel.ResidualAmount)
};
try
{
_dataBase.Query("sp_OA_FirmAccountRecord_Update", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
///
/// 删除
///
///
///
public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.OA_FirmAccountRecord trueModel = model as Model.OA_FirmAccountRecord;
if (trueModel == null)
{
return false;
}
IList sqlParms = new List()
{
new SqlParameter("@Keyid",trueModel.Keyid)
};
try
{
_dataBase.Query("sp_OA_FirmAccountRecord_DeleteRow", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
///
/// 分页查询
///
///
///
///
public IEnumerable SelectAllModel(Infrastructure.Query.Query query, Infrastructure.Query.Pagination pagination)
{
throw new NotImplementedException();
}
///
/// 分页查询
///
///
///
///
///
///
///
///
///
///
///
///
///
public IEnumerable SelectModelPage(Pagination pa, Guid FirmId, DateTime? RegTimeStart, DateTime? RegTimeEnd, string AccountName, string AcoountType, string Operator, string SubjectId, string RecordTypeId, string PaymentUnit, string OperationalMatters)
{
try
{
string target = " a.*,b.AccountType AS FirmAccountType,b.UserName AS UserName,b.AccountName AS FirmAccountName,c.SubjectName AS SusjectName";
string fromSource = " dbo.OA_FirmAccountRecord a LEFT JOIN dbo.OA_FirmAccount b ON b.Keyid = a.AccountId LEFT JOIN dbo.OA_SubjectSet c ON a.SubjectId = c.Keyid WHERE b.FirmId='" + FirmId + "' ";
if (!string.IsNullOrEmpty(RecordTypeId))
{
fromSource += string.Format(" and RecordTypeId ='{0}'", RecordTypeId);
}
if (RegTimeStart.HasValue)
{
fromSource += string.Format(" and CAST(a.LastUpdateTime AS DATE) >='{0}'", RegTimeStart);
}
if (RegTimeEnd.HasValue)
{
fromSource += string.Format(" and CAST(a.LastUpdateTime AS DATE) <='{0}'", RegTimeEnd);
}
if (!string.IsNullOrEmpty(AccountName))
{
fromSource += string.Format(" and a.AccountId ='{0}'", AccountName);
}
if (!string.IsNullOrEmpty(AcoountType))
{
fromSource += string.Format(" and b.AccountType='{0}'", AcoountType);
}
if (!string.IsNullOrEmpty(Operator))
{
fromSource += string.Format(" and a.Operator like '%{0}%'", Operator);
}
if (!string.IsNullOrEmpty(OperationalMatters))
{
fromSource += string.Format(" and a.OperationalMatters like '%{0}%'", OperationalMatters);
}
if (!string.IsNullOrEmpty(SubjectId))
{
fromSource += string.Format(" and a.SubjectId='{0}'", SubjectId);
}
if (!string.IsNullOrEmpty(PaymentUnit))
{
fromSource += string.Format(" and a.PaymentUnit like '%{0}%'", PaymentUnit);
}
return _dataBase.SelectModelPage(pa, target, fromSource, "a.LastUpdateTime DESC");
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 金额统计
///
///
///
///
///
///
///
///
///
///
///
///
public Model.OA_FirmAccountRecord SumRecordMoney(Pagination pa, Guid FirmId, DateTime? RegTimeStart, DateTime? RegTimeEnd, string AccountName, string AcoountType, string Operator, string SubjectId, string RecordTypeId, string PaymentUnit)
{
try
{
string target = " case when RecordTypeId = 1 then sum(Money) else 0 end as AllIncomeMoney , case when RecordTypeId = 2 then sum(Money) else 0 end as AllExpensesMoney ";
string fromSource = " dbo.OA_FirmAccountRecord a LEFT JOIN dbo.OA_FirmAccount b ON b.Keyid = a.AccountId LEFT JOIN dbo.OA_SubjectSet c ON a.SubjectId = c.Keyid WHERE b.FirmId='" + FirmId + "' ";
if (!string.IsNullOrEmpty(RecordTypeId))
{
fromSource += string.Format(" and RecordTypeId ='{0}'", RecordTypeId);
}
if (RegTimeStart.HasValue)
{
fromSource += string.Format(" and CAST(a.LastUpdateTime AS DATE) >='{0}'", RegTimeStart);
}
if (RegTimeEnd.HasValue)
{
fromSource += string.Format(" and CAST(a.LastUpdateTime AS DATE) <='{0}'", RegTimeEnd);
}
if (!string.IsNullOrEmpty(AccountName))
{
fromSource += string.Format(" and a.AccountId ='{0}'", AccountName);
}
if (!string.IsNullOrEmpty(AcoountType))
{
fromSource += string.Format(" and b.AccountType='{0}'", AcoountType);
}
if (!string.IsNullOrEmpty(Operator))
{
fromSource += string.Format(" and a.Operator like '%{0}%'", Operator);
}
if (!string.IsNullOrEmpty(SubjectId))
{
fromSource += string.Format(" and a.SubjectId='{0}'", SubjectId);
}
if (!string.IsNullOrEmpty(PaymentUnit))
{
fromSource += string.Format(" and a.PaymentUnit like '%{0}%'", PaymentUnit);
}
IList m_OA_FirmAccountRecordList = _dataBase.SelectModel(target, fromSource + " group by RecordTypeId");
if (m_OA_FirmAccountRecordList != null && m_OA_FirmAccountRecordList.Count == 2)
{
m_OA_FirmAccountRecordList[0].AllIncomeMoney = m_OA_FirmAccountRecordList[0].AllIncomeMoney > 0 ? m_OA_FirmAccountRecordList[0].AllIncomeMoney : m_OA_FirmAccountRecordList[1].AllIncomeMoney;
m_OA_FirmAccountRecordList[0].AllExpensesMoney = m_OA_FirmAccountRecordList[0].AllExpensesMoney > 0 ? m_OA_FirmAccountRecordList[0].AllExpensesMoney : m_OA_FirmAccountRecordList[1].AllExpensesMoney;
return m_OA_FirmAccountRecordList[0];
}
else if (m_OA_FirmAccountRecordList != null && m_OA_FirmAccountRecordList.Count > 0)
{
return m_OA_FirmAccountRecordList[0];
}
else
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 单个查询
///
///
///
public Model.OA_FirmAccountRecord SelectSingleModel(string Keyid, Guid FirmId)
{
try
{
string target = " a.*,b.AccountType AS FirmAccountType,b.AccountName AS FirmAccountName,c.SubjectName AS SusjectName";
string fromSource = " dbo.OA_FirmAccountRecord a LEFT JOIN dbo.OA_FirmAccount b ON b.Keyid = a.AccountId LEFT JOIN dbo.OA_SubjectSet c ON a.SubjectId = c.Keyid WHERE b.FirmId='" + FirmId + "' and a.Keyid='" + Keyid + "'";
return _dataBase.SelectModel(target, fromSource)[0];
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 分页查询
///
///
///
///
public IEnumerable SelectModelPage(Infrastructure.Query.Query query, Infrastructure.Query.Pagination pagination)
{
throw new NotImplementedException();
}
///
/// 单个查询
///
///
///
public IEnumerable SelectAllModel(Infrastructure.Query.Query query)
{
throw new NotImplementedException();
}
///
/// 添加收入/支出记录
///
///
///
public bool AddModel(OA_FirmAccountRecord model, OA_FirmAccount m_OA_FirmAccount)
{
try
{
OA_FirmAccountRecordDAL dal_OA_FirmAccountRecordDAL = new OA_FirmAccountRecordDAL(_dataBase);
OA_FirmAccountDAL dal_OA_FirmAccountDAL = new OA_FirmAccountDAL(_dataBase);
bool IsSuccess = true;
using (TransactionScope t_TransactionScope = new TransactionScope())
{
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountRecordDAL.InserModel(model);
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountDAL.UpdateModel(m_OA_FirmAccount);
if (IsSuccess)
{
t_TransactionScope.Complete();
}
}
}
}
return IsSuccess;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 添加收入/支出记录,并更改会员状态
///
///
///
///
///
public bool AddModel(OA_FirmAccountRecord model, OA_FirmAccount m_OA_FirmAccount, EC_MemberBasic memberBasic, EC_PaymentRecord paymentRecord)
{
try
{
OA_FirmAccountRecordDAL dal_OA_FirmAccountRecordDAL = new OA_FirmAccountRecordDAL(_dataBase);
OA_FirmAccountDAL dal_OA_FirmAccountDAL = new OA_FirmAccountDAL(_dataBase);
EC_MemberBasicDAL eC_MemberBasicDAL = new EC_MemberBasicDAL(_dataBase);
EC_PaymentRecordDAL eC_PaymentRecordDAL = new EC_PaymentRecordDAL(_dataBase);
bool IsSuccess = true;
using (TransactionScope t_TransactionScope = new TransactionScope())
{
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountRecordDAL.InserModel(model);
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountDAL.UpdateModel(m_OA_FirmAccount);
if (IsSuccess)
{
if (paymentRecord.PayType == "个人网店押金" || paymentRecord.PayType == "印刷厂商注册费" || paymentRecord.PayType == "印刷厂商续费")
{
IsSuccess = eC_MemberBasicDAL.UpdateModel(memberBasic);
}
if (IsSuccess)
{
IsSuccess = eC_PaymentRecordDAL.UpdateModel(paymentRecord);
if (IsSuccess)
{
t_TransactionScope.Complete();
}
}
}
}
}
}
return IsSuccess;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 账户转账
///
///
///
///
///
///
public bool AccountOToO(OA_FirmAccount m_OA_FirmAccountIn, OA_FirmAccount m_OA_FirmAccountOut, OA_FirmAccountRecord m_OA_FirmAccountRecordIn, OA_FirmAccountRecord m_OA_FirmAccountRecordOut)
{
try
{
OA_FirmAccountRecordDAL dal_OA_FirmAccountRecordDAL = new OA_FirmAccountRecordDAL(_dataBase);
OA_FirmAccountDAL dal_OA_FirmAccountDAL = new OA_FirmAccountDAL(_dataBase);
bool IsSuccess = true;
using (TransactionScope t_TransactionScope = new TransactionScope())
{
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountDAL.UpdateModel(m_OA_FirmAccountIn);
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountDAL.UpdateModel(m_OA_FirmAccountOut);
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountRecordDAL.InserModel(m_OA_FirmAccountRecordIn);
if (IsSuccess)
{
IsSuccess = dal_OA_FirmAccountRecordDAL.InserModel(m_OA_FirmAccountRecordOut);
if (IsSuccess)
{
t_TransactionScope.Complete();
}
}
}
}
}
}
return IsSuccess;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 订单收款
///
/// 账户编号
/// 操作金额
/// 科目编号
/// 支付时间
/// 支付方
/// 操作人
///
public bool OrderReceivables(int? accountId, decimal? money, int? subjectId, DateTime? payTime, string PaymentUnit, string Operator)
{
try
{
bool result = false;
IList sqlParms = new List()
{
new SqlParameter("@Keyid",accountId),
new SqlParameter("@PayMoney",money),
new SqlParameter("@Operator",Operator),
new SqlParameter(){ParameterName="@result",Direction=ParameterDirection.ReturnValue,SqlDbType=SqlDbType.Int}
};
_dataBase.Query("sp_OA_FirmAccount_Update_Money", CommandType.StoredProcedure, sqlParms.ToArray());
result = 1.Equals(sqlParms[3].Value);
if (result)
{
OA_FirmAccountDAL dal_OA_FirmAccountDAL = new OA_FirmAccountDAL(_dataBase);
OA_FirmAccount m_OA_FirmAccount = dal_OA_FirmAccountDAL.getSingleSubject(accountId);
OA_FirmAccountRecord m_OA_FirmAccountRecord = new OA_FirmAccountRecord();
m_OA_FirmAccountRecord.AccountId = accountId;
m_OA_FirmAccountRecord.LastUpdateTime = payTime;
m_OA_FirmAccountRecord.Money = money;
m_OA_FirmAccountRecord.OperationalMatters = "订单收款";
m_OA_FirmAccountRecord.Operator = Operator;
m_OA_FirmAccountRecord.PaymentUnit = PaymentUnit;
m_OA_FirmAccountRecord.RecordTypeId = 1;
m_OA_FirmAccountRecord.Remark = "";
m_OA_FirmAccountRecord.SubjectId = subjectId;
m_OA_FirmAccountRecord.ResidualAmount = m_OA_FirmAccount.Balance;
m_OA_FirmAccountRecord.Department = "";
result = InserModel(m_OA_FirmAccountRecord);
}
return result;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 更具ID数据获取列表
///
///
///
///
public IEnumerable SelectModelListByKeyids(Guid FirmId, string keyids)
{
try
{
string target = " a.*,b.AccountType AS FirmAccountType,b.UserName AS UserName,b.AccountName AS FirmAccountName,c.SubjectName AS SusjectName ";
string fromSource = " dbo.OA_FirmAccountRecord a LEFT JOIN dbo.OA_FirmAccount b ON b.Keyid = a.AccountId LEFT JOIN dbo.OA_SubjectSet c ON a.SubjectId = c.Keyid ";
return _dataBase.SelectModel(target, fromSource, " b.FirmId='" + FirmId + "' and a.keyid in (" + keyids + ") order by a.LastUpdateTime ASC ");
}
catch (Exception ex)
{
throw ex;
}
}
}
}