using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.MES.dev;
using ZR.Model.MES.dev.Dto;
using ZR.Repository;
using ZR.Service.MES.dev.IService;
using System.Linq;
namespace ZR.Service.DeviceManagement
{
///
/// 巡检任务结果表Service业务层处理
///
[AppService(ServiceType = typeof(IDeviceTaskExecuteResultService), ServiceLifetime = LifeTime.Transient)]
public class DeviceTaskExecuteResultService : BaseService, IDeviceTaskExecuteResultService
{
///
/// 查询巡检任务结果表列表
///
///
///
public PagedInfo GetList(DeviceTaskExecuteResultQueryDto parm)
{
var predicate = Expressionable.Create()
.AndIF(!string.IsNullOrEmpty(parm.RouteInspectionPlanName), it => it.RouteInspectionPlanName.Contains(parm.RouteInspectionPlanName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), it => it.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.InspectName), it => it.InspectName.Contains(parm.InspectName));
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public DeviceTaskExecuteResult GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加巡检任务结果表
///
///
///
public DeviceTaskExecuteResult AddDeviceTaskExecuteResult(DeviceTaskExecuteResult model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
///
/// 修改巡检任务结果表
///
///
///
public int UpdateDeviceTaskExecuteResult(DeviceTaskExecuteResult model)
{
//var response = Update(w => w.Id == model.Id, it => new DeviceTaskExecuteResult()
//{
// Person = model.Person,
// Type = model.Type,
// ResultText = model.ResultText,
// ResultArray = model.ResultArray,
// ResultFile = model.ResultFile,
// Remark = model.Remark,
// CreatedBy = model.CreatedBy,
// CreatedTime = model.CreatedTime,
// UpdatedBy = model.UpdatedBy,
// UpdatedTime = model.UpdatedTime,
//});
//return response;
return Update(model, true);
}
///
/// 清空巡检任务结果表
///
///
public bool TruncateDeviceTaskExecuteResult()
{
var newTableName = $"device_route_inspection_result_{DateTime.Now:yyyyMMdd}";
if (Queryable().Any() && !Context.DbMaintenance.IsAnyTable(newTableName))
{
Context.DbMaintenance.BackupTable("device_route_inspection_result", newTableName);
}
return Truncate();
}
///
/// 导入巡检任务结果表
///
///
public (string, object, object) ImportDeviceTaskExecuteResult(List list)
{
var x = Context.Storageable(list)
.SplitInsert(it => !it.Any())
.SplitError(x => x.Item.Id.IsEmpty(), "id 雪花不能为空")
.SplitError(x => x.Item.FkRouteInspectionPlanId.IsEmpty(), "巡检计划id不能为空")
//.WhereColumns(it => it.UserName)//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
.ToStorage();
var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;
string msg = $"插入{x.InsertList.Count} 更新{x.UpdateList.Count} 错误数据{x.ErrorList.Count} 不计算数据{x.IgnoreList.Count} 删除数据{x.DeleteList.Count} 总共{x.TotalList.Count}";
Console.WriteLine(msg);
//输出错误信息
foreach (var item in x.ErrorList)
{
Console.WriteLine("错误" + item.StorageMessage);
}
foreach (var item in x.IgnoreList)
{
Console.WriteLine("忽略" + item.StorageMessage);
}
return (msg, x.ErrorList, x.IgnoreList);
}
}
}