using Dapper;
using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Learun.Application.TwoDevelopment.LogisticsManagement;
namespace Learun.Application.TwoDevelopment.EducationalAdministration
{
///
/// 版 本 Learun-ADMS V7.0.6 力软敏捷开发框架
/// Copyright (c) 2013-2020 力软信息技术(苏州)有限公司
/// 创 建:超级管理员
/// 日 期:2022-03-03 10:15
/// 描 述:教材入库
///
public class TextBookInService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 查询参数
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(@" * ");
strSql.Append(" FROM TextBookIn t ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["TextBookName"].IsEmpty())
{
dp.Add("TextBookName", queryParam["TextBookName"].ToString(), DbType.String);
strSql.Append(" AND t.TextBookName = @TextBookName ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取TextBookIn表实体数据
///
/// 主键
///
public TextBookInEntity GetTextBookInEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取TextBookIn表RK明细
///
/// 主键
///
public IEnumerable GetInEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindList(x => x.InOutCode == keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除实体数据
///
/// 主键
public void DeleteEntity(string keyValue)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
decimal? Num = 0;
var keyValueArr = keyValue.Split(',');
foreach (var item in keyValueArr)
{
var entity = BaseRepository("CollegeMIS").FindEntity(x => x.ID == item);
if (entity != null)
{
Num += entity.variate;
var InOutEntity = db.FindEntity(x => x.BookCode == entity.InOutCode);
if (InOutEntity != null)
{
InOutEntity.FinallyNum = InOutEntity.FinallyNum - Num;
if (InOutEntity.FinallyNum == 0 || InOutEntity.FinallyNum > 0)
{
db.Delete(entity);
db.Update(InOutEntity);
}
else
{
db.Rollback();
}
}
else
{
db.Rollback();
}
}
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
///
/// 主键
/// 实体
public void SaveEntity(string keyValue, TextBookInEntity entity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
this.BaseRepository("CollegeMIS").Update(entity);
}
else
{
entity.Create();
this.BaseRepository("CollegeMIS").Insert(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存
///
///
/// 库存
///
public void SaveEntity(string keyValue, TextbookInOutEntity entity, List textbookInList)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
decimal? variate = 0;
if (textbookInList.Count > 0)
{
for (int i = 0; i < textbookInList.Count; i++)
{
textbookInList[i].InOutCode = entity.BookCode;
textbookInList[i].LessonNo = entity.LessonNo;
textbookInList[i].PublishNo = entity.PublishNo;
textbookInList[i].TextBookNo = entity.TextBookNo;
textbookInList[i].TextBookName = entity.TextBookName;
textbookInList[i].FirstAuthor = entity.FirstAuthor;
textbookInList[i].OtherAuthor = entity.OtherAuthor;
textbookInList[i].Pubdate = entity.Pubdate;
textbookInList[i].Publisher = entity.Publisher;
textbookInList[i].Edition = entity.Edition;
textbookInList[i].Impression = entity.Impression;
textbookInList[i].Impression = entity.Impression;
variate += textbookInList[i].variate;
}
db.Insert(textbookInList);
if (variate != 0 && variate > 0)
{
var newtextInOutEntity = db.FindEntity(x => x.BookCode == entity.BookCode);
if (newtextInOutEntity != null)
{
newtextInOutEntity.FinallyNum += variate;
newtextInOutEntity.RKNum += variate;
db.Update(newtextInOutEntity);
}
}
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}