DAL - 主从表业务单据数据访问层C#代码
DAL - 主从表业务单据数据访问层C#代码
由CodeGeneratorV6.0代码生成器自动生成。
代码摘自纸箱行业ERP系统《生产计划管理》。
C# 全选
using CartonERP.Common;
using CartonERP.Interfaces;
using CartonERP.Models;
using CartonERP.Models.ResModels;
using CartonERP.Models.UpdateModel;
using CSFrameworkV6.Core;
using CSFrameworkV6.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
/*===================================================================
* 程序说明: PFPlan 数据访问层
* 作者资料: CSFramework
* 创建日期: 2023/08/08 07:09:38
* 最后修改: 2023/08/08 07:09:38
*
* 注:本文件由CSFramework.CodeGenerator代码生成器自动生成。
* 请将源码文件复制到 YourProject 对应的项目。
*
* 版权所有 Copyright 2006~2023, C/S框架网 (www.csframework.com)
*===================================================================*/
namespace CartonERP.DataAccess
{
/// <summary>
/// DAL数据层:dal_PFPlan
/// </summary>
[DefaultORM_UpdateMode(typeof(tb_PFPlan), true)]
public class dal_PFPlan : dalBaseBusiness, IBridge_PFPlan
{
/// <summary>
/// 构造器
/// </summary>
/// <param name="loginer">当前登录用户</param>
public dal_PFPlan(Loginer loginer) : base(loginer)
{
}
//查询数据
public List<res_tb_PFPlan> Query(req_PFPlan input)
{
var q = _Database.GetQueryable<tb_PFPlan>();
if (!input.PLNO.IsEmpty())
q = q.Where(w => w.PLNO == input.PLNO);
if (input.DocDate > Globals.MinSqlDate)//1900-01-01
{
q = q.Where(w => true
&& w.DocDate.Value.Year >= input.DocDate.Year
&& w.DocDate.Value.Month >= input.DocDate.Month
&& w.DocDate.Value.Day >= input.DocDate.Day);
}
var resList = q.Select(s => new res_tb_PFPlan
{
isid = s.isid,
PLNO = s.PLNO,
DocDate = s.DocDate,
Remark = s.Remark,
CreationDate = s.CreationDate,
CreatedBy = s.CreatedBy,
LastUpdateDate = s.LastUpdateDate,
LastUpdatedBy = s.LastUpdatedBy,
FlagApp = s.FlagApp,
AppUser = s.AppUser,
AppDate = s.AppDate,
});
//返回查询结果
return resList.ToList();
}
/// <summary>
/// 获取业务单据数据,包括主表及明细表
/// </summary>
/// <param name="docNo">单据号码</param>
/// <returns></returns>
public BusinessData<res_tb_PFPlan, res_tb_PFPlanDetails> GetDataByKey(string docNo)
{
var q_PFPlan = _Database.GetQueryable<tb_PFPlan>();//主表
q_PFPlan = q_PFPlan.Where(w => w.PLNO == docNo);
var q_tb_PFPlanDetails = _Database.GetQueryable<tb_PFPlanDetails>();//明细表
q_tb_PFPlanDetails = q_tb_PFPlanDetails.Where(w => w.PLNO == docNo);
//主表res模型
var master = q_PFPlan.Select(s => new res_tb_PFPlan
{
isid = s.isid,
PLNO = s.PLNO,
DocDate = s.DocDate,
Remark = s.Remark,
CreationDate = s.CreationDate,
CreatedBy = s.CreatedBy,
LastUpdateDate = s.LastUpdateDate,
LastUpdatedBy = s.LastUpdatedBy,
FlagApp = s.FlagApp,
AppUser = s.AppUser,
AppDate = s.AppDate,
}).FirstOrDefault();
//明细表res模型
var listDetail = q_tb_PFPlanDetails.Select(s => new res_tb_PFPlanDetails
{
isid = s.isid,
PLNO = s.PLNO,
SortId = s.SortId,
PFNO = s.PFNO,
PFDate = s.PFDate,
Days = s.Days,
PaperInQty = s.PaperInQty,
PaperOutQty = s.PaperOutQty,
CustomerCode = s.CustomerCode,
CustomerName = s.CustomerName,
SONO = s.SONO,
DeliveryDate = s.DeliveryDate,
CustomerOrderNo = s.CustomerOrderNo,
ProductCode = s.ProductCode,
ProductName = s.ProductName,
GoodsNo = s.GoodsNo,
CartonCode = s.CartonCode,
CartonName = s.CartonName,
CartonSpec = s.CartonSpec,
PaperName = s.PaperName,
PaperNameSupplier = s.PaperNameSupplier,
PaperSpec = s.PaperSpec,
PaperQty = s.PaperQty,
OrderQty = s.OrderQty,
CartonInQty = s.CartonInQty,
CartonBalanceQty = s.CartonBalanceQty,
DOQty = s.DOQty,
DOBalance = s.DOBalance,
Price = s.Price,
Remark = s.Remark,
CreationDate = s.CreationDate,
CreatedBy = s.CreatedBy,
LastUpdateDate = s.LastUpdateDate,
LastUpdatedBy = s.LastUpdatedBy,
SchedulQty = s.SchedulQty,
}).ToList();
//返回结果
var result = new BusinessData<res_tb_PFPlan, res_tb_PFPlanDetails>
{
Master = master,
Details = listDetail,
};
return result;
}
/// <summary>
/// 删除业务单据
/// </summary>
public bool Delete(string docNo)
{
try
{
_Database.BeginTransaction();
var master = _Database.GetQueryable<tb_PFPlan>().Where(w => w.PLNO == docNo).FirstOrDefault();
var details = _Database.GetQueryable<tb_PFPlanDetails>().Where(w => w.PLNO == docNo).ToList();
int count1 = _Database.Remove<tb_PFPlan>(master);
int count2 = _Database.Remove<tb_PFPlanDetails>(details);
_Database.CommitTransaction();
//数据日志
if (count1 > 0 || count2 > 0)
_Log.Log(DataLogType.delete, "PFPlan - 删除成功", nameof(tb_PFPlan), docNo, _Loginer.Account, master, details);
return count1 + count2 > 0;
}
catch (Exception e)
{
_Database.RollbackTransaction();
throw;
}
}
/// <summary>
/// 保存数据
/// </summary>
public SaveResult Update(BusinessData<tb_PFPlan, tb_PFPlanDetails> data)
{
try
{
_Database.BeginTransaction();
SetCommonValue<tb_PFPlan>(new List<tb_PFPlan> { data.Master });
SetCommonValue<tb_PFPlanDetails>(data.Details);
//判断单据号码是否已存在,即可识别当前操作是新增或修改状态
var m = _Database.GetQueryable<tb_PFPlan>();
var isEdit = m.Any(a => a.PLNO == data.Master.PLNO);//修改状态
var isAdd = isEdit == false;//新增状态
if (isAdd)
{
#region 新增状态
//获取单据号码
data.Master.PLNO = DocNoHelper.GetDocNo(_Database, "PL", _Loginer.Account);
//设置明细表外键
foreach (var d in data.Details)
{
d.PLNO = data.Master.PLNO;
}
_Database.Add<tb_PFPlan>(data.Master);
_Database.Add<tb_PFPlanDetails>(data.Details);
#endregion
}
else
{
#region 修改状态
//保存主表
_Database.Update<tb_PFPlan>(data.Master);
//逻辑:前端界面收集主表及明细表数据,不需要关注对象是新增还是修改,这里从数据库匹配主键是否存在来自动识别对象状态。
var q = _Database.GetQueryable<tb_PFPlanDetails>();
var oldData = q.Where(w => w.PLNO == data.Master.PLNO).ToList();
var listAdded = data.Details.Where(w => !oldData.Any(a => a.isid == w.isid)).ToList();//新增的记录
var listEdit = data.Details.Where(w => oldData.Any(a => a.isid == w.isid)).ToList();//修改的记录
var listDelete = oldData.Where(w => !data.Details.Any(a => a.isid == w.isid)).ToList();//删除的记录
//新增的对象,需要设置主键和外键
foreach (var v in listAdded)
{
v.isid = IdHelper.GetId();
v.PLNO = data.Master.PLNO;
}
//保存明细表:提交3组数据
if (listAdded.Count > 0) _Database.Add(listAdded);
if (listEdit.Count > 0) _Database.Update(listEdit);
if (listDelete.Count > 0) _Database.Remove(listDelete);
#endregion
}
_Database.CommitTransaction();
//数据操作日志
_Log.Log(DataLogType.save, "PFPlan保存资料", nameof(tb_PFPlan), "", _Loginer.Account, data);
var result = new SaveResult
{
DocNo = data.Master.PLNO,//单据号码
Result = (int)ResultID.SUCCESS,//成功状态
Description = "保存成功",
};
return result;
}
catch (Exception ex)
{
_Database.RollbackTransaction();
//数据操作日志
_Log.Log(DataLogType.save, "PFPlan保存失败!" + ex.GetMessage(), nameof(tb_PFPlan), "", _Loginer.Account, data);
throw;
}
}
/// <summary>
/// 审核单据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public bool ApprovalBusiness(req_Approval input)
{
var q = _Database.GetQueryable<tb_PFPlan>();
var doc = q.Where(w => w.PLNO == input.DocNo).FirstOrDefault();
if (doc != null)
{
if (input.Type == ApprovalType.Approval)
{
doc.AppDate = DateTime.Now;
doc.AppUser = _Loginer.Account;
doc.FlagApp = "Y";
}
else if (input.Type == ApprovalType.UnApproval)
{
doc.AppDate = null;
doc.AppUser = string.Empty;
doc.FlagApp = "N";
}
var ok = _Database.Update(doc) == 1;
if (ok) _Log.Log(DataLogType.approval, "审核/反审", nameof(tb_PFPlan), input.DocNo, _Loginer.Account, input);
return ok;
}
else
{
return false;
}
}
}//public class
}//namespace
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
CSFrameworkV6 C/S框架网