批量修改

时间:2022-10-09 10:57:20

之前做过的批量修改是从第一个窗体获取到N个Id,在第二个窗体循环修改它的值

现在记录的这个批量修改是 两张表通过服务商单号关联,修改它的服务商,一张表是订单表,另外一张表是单号种类设置表。

首先从第一个窗体获取到N个Id,在第二个窗体,根据id 查询出转运订单列表,循环转运订单列表,在里面实例化单号种类设置

表,实例化IBB,DAL,批量查询要修改的服务商单号,最后进行修改。


​第一个窗体代码​

/// <summary>
/// 批量出货
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BatchOrderChu_Click(object sender, EventArgs e){
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("请选择需要批量出货的数据", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int[] ids = new int[dataGridView1.SelectedRows.Count];

List<int> Id = new List<int>();
for (int i = 0; i < ids.Length; i++)
{
Id.Add(ids[i]);
}

NumberTypeIBLL ntdb = new NumberTypeDAL();
var nts = ntdb.GetModel(ServiceNumber[0]);
ScanZyOrder_Model s = new ScanZyOrder_Model(Id, nts.ServiceProviderId);
s.MaximizeBox = false;
s.StartPosition = FormStartPosition.CenterParent;//窗体居中代码
s.ShowDialog();
}


​第二个窗体代码​

public partial class ScanZyOrder_Model : Form
{
private List<Models.ZyOrder> model = new List<Models.ZyOrder>();
private List<int> _idList;
public ScanZyOrder_Model()
{
InitializeComponent();
}
public ScanZyOrder_Model(List<int> idList, int serviceProviderId) : this()
{
_idList = idList;
_serviceProviderId = serviceProviderId;
}

/// <summary>
///批量修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Save_BtnClick(object sender, EventArgs e)
{
int[] id = new int[_idList.Count];
for (int i = 0; i < _idList.Count; i++)
{
id[i] = _idList[i];
}
ZyOrderIBll db = new ZyOrderDAL();
model = db.GetModel(id); // 根据id 查询出转运订单列表
for (int i = 0; i < model.Count; i++)
{
NumberTypeSet model1 = new NumberTypeSet();
NumberTypeIBLL db2 = new NumberTypeDAL();
model1 = db2.GetModel(model[i].ServiceNumber); // 批量查询要修改的服务商单号
model1.ServiceProviderId = MsParse.ToInt32(this.ServiceProviderId.SelectedValue);
var s = db2.UpDate(model1);
}


var list = _idList.Select(i => new Models.ZyOrder()
{
Id = i,
ShipmentNum = this.ShipmentNum.Text,
ShipmentCom = MsParse.ToInt32(this.ServiceProviderId.SelectedValue),
ShipmentState = 2,
ShipmentTime = MsAdmin.DateTime_Now,
State = 4
}).ToList();

if (!db.UpDate(list, o => new
{
o.ShipmentNum,
o.ShipmentCom,
o.ShipmentState,
o.ShipmentTime,
o.State
}))
{
MessageBox.Show("出货失败,请稍后再试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}


var list2 = _idList.Select(i => new Models.ZyPackage()
{
ZyOrderId = i,
OffTime = DateTime.Now,
State = 4,
ShelfState = 2,
ShipmentDateTime = DateTime.Now,
ShipmentPeople = MsAdmin.Id,
ShipmentPeopleName = MsAdmin.Name,
}).ToList();

if (!zpdb.UpDate(list2, p => new
{
p.ZyOrderId,
p.OffTime,
p.State,
p.ShelfState,
p.ShipmentDateTime,
p.ShipmentPeople,
p.ShipmentPeopleName,
}, p => new
{
p.ZyOrderId
}))
{
MessageBox.Show("出货失败,请稍后再试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// 添加轨迹
for (int i = 0; i < model.Count; i++)
{
TrackIBLL tdb = new TrackDAL();
Track t = new Track
{
Content = "订单已出货",
CreationTime = MsAdmin.DateTime_Now,
OrderId = model[i].Id,
OrderNo = model[i].OrderNum,
Source = "系统后台出仓",
TrackTime = MsAdmin.DateTime_Now,
Type = 3,
AdminId = MsAdmin.Id
};
tdb.Add(t);
}

MessageBox.Show("出货成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
ZyOrderList.ug.GetList(); // 刷新列表
}
}


ZyOrderDAL

        public List<ZyOrder> GetModel(int[] Id)
{

var list = Db.SqlQueryable<ZyOrder>(sql)
.In(x => x.Id, Id)
.Select(x => new ZyOrder
{
Id = x.Id,
ServiceNumber=x.ServiceNumber, // 服务商单号
...
...
...
}).ToList();

return list;

}

public bool UpDate(List<ZyOrder> updateObjs, Expression<Func<ZyOrder, object>> setValueExpression)
{
if (Db.Updateable(updateObjs).UpdateColumns(setValueExpression).ExecuteCommand() > 0)
{
return true;
}
else
{
return false;
}

}

​ZyOrderIBll​

List<ZyOrder> GetModel(int[] Id);
bool UpDate(List<ZyOrder> updateObjs, Expression<Func<ZyOrder, object>> setValueExpression);

​NumberTypeDAL​

        /// <summary>
/// 查询单号是否存在
/// </summary>
/// <param name="Number"></param>
/// <returns></returns>
public NumberTypeSet GetModel(string Number)
{
List<NumberTypeSet> list = Db.Queryable<NumberTypeSet>().Where(x => x.Number == Number).ToList();
if (list.Count > 0)
{
return list[0];
}
else
{
return new NumberTypeSet();
}
}

/// <summary>
/// 修改
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool UpDate(NumberTypeSet obj)
{
if (Db.Updateable(obj).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand() > 0)
{
return true;
}
else
{
return false;
}
}

​NumberTypeIBLL​

NumberTypeSet GetModel(string Number);
bool UpDate(NumberTypeSet obj);
var list2 = _idList.Select(i => new Models.ZyPackage()
{
ZyOrderId = i,
OffTime = DateTime.Now,
State = 4,
ShelfState = 2,
ShipmentDateTime = DateTime.Now,
ShipmentPeople = MsAdmin.Id,
ShipmentPeopleName = MsAdmin.Name,
}).ToList();

zpdb.UpDate(list2, p => new{p.ZyOrderId,p.OffTime}, p => new{p.ZyOrderId})

/// <summary>
///
/// </summary>
/// <param name="updateObjs"></param>
/// <param name="updateColumns"></param>
/// <param name="whereColumns"></param>
/// <returns></returns>
public bool UpDate(List<ZyPackage> updateObjs, Expression<Func<ZyPackage, object>> updateColumns, Expression<Func<ZyPackage, object>> whereColumns)
{
if (Db.Updateable(updateObjs).UpdateColumns(updateColumns).WhereColumns(whereColumns).ExecuteCommand() > 0)
{
return true;
}
else
{
return false;
}
}