C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
C#TMS系統代碼-業務頁面ShippingNotice學習
學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達
感覺感覺前面的BaseCity差不太多,這邊的分頁查詢複雜一點,其他的當個加強記憶了
Service頁面
//跟BaseCity頁面的差不多
[ApiDescriptionSettings(Tag = "Business", Name = "ShippingNotice", Order = 200)]
[Route("api/TMS/Business/[controller]")]
public class TmsBusiShippingNoticeService : ITmsBusiShippingNoticeService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<TmsBusiShippingNoticeEntity> _repository;
private readonly IDataInterfaceService _dataInterfaceService;
private readonly IUserManager _userManager;
public TmsBusiShippingNoticeService(
ISqlSugarRepository<TmsBusiShippingNoticeEntity> tmsBusiShippingNoticeRepository,
IDataInterfaceService dataInterfaceService,
ISqlSugarClient context,
IUserManager userManager)
{
_repository = tmsBusiShippingNoticeRepository;
_dataInterfaceService = dataInterfaceService;
_userManager = userManager;
}
//各種方法
......
}
獲取單個ShippingNotice
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
//BaseCity那邊更簡單一點_repository.FirstOrDefaultAsync(x => x.Id == id)).Adapt<TmsBaseCityInfoOutput>();
return await _repository.Context.Queryable<TmsBusiShippingNoticeEntity>()
.Where(it => it.Id == id)
.Select(it => new TmsBusiShippingNoticeInfoOutput
{
//需要處理數據
id = it.Id,
......
}).FirstAsync();
}
查詢ShippingNotice分頁List
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] TmsBusiShippingNoticeListQueryInput input)
{
//獲取時間參數
List<DateTime> PlanDateList = input.planDate?.Split(',').ToObject<List<DateTime>>();
DateTime? SPlanDate = PlanDateList?.First();
DateTime? EPlanDate = PlanDateList?.Last();
//數據許可權
//522839 這個id是直接資料庫查了之後放在這裡的?
//id為表的主鍵
var authorizeWhere = new List<IConditionalModel>();
authorizeWhere = await _userManager.GetConditionAsync<TmsBusiShippingNoticeListOutput>("522839", "it.F_Id");
//獲取登錄人CompanyCode 類型List<string>,管理員可以看全部
var loginUserCompany = new List<string>();
if (!_userManager.IsAdministrator)
{
loginUserCompany = await _repository.Context.Queryable<TmsBaseCompanyEmployeeEntity>().Where(x => x.DeleteMark == null && x.EnabledMark == 1 && x.UserId == _userManager.UserId).GroupBy(x => x.CompanyCode).Select(x => x.CompanyCode).ToListAsync();
}
//查詢主表然後左連接明細,我猜應該有(1+n)條數據
var querywherr = _repository.Context.Queryable<TmsBusiShippingNoticeEntity, TmsBusiShippingNoticeDetailEntity>((it, b) => new object[]{ JoinType.Left,it.CRMNo==b.CRMNo })
.Where(it => it.DeleteMark == null)
//添加數據許可權
.Where(authorizeWhere)
//只查詢當前登錄人綁定的公司編碼數據
.WhereIF(!_userManager.IsAdministrator, it => loginUserCompany.Contains(it.CompanyNo))
//添加時間查詢
.WhereIF(!string.IsNullOrEmpty(input.planDate), it => SqlFunc.Between(it.PlanDate, SPlanDate.ParseToDateTime("yyyy-MM-dd 00:00:00"), EPlanDate.ParseToDateTime("yyyy-MM-dd 23:59:59")))
//一大堆查詢條件
......
.WhereIF(!string.IsNullOrEmpty(input.keyword), it => it.CRMNo.Contains(input.keyword) || it.SysType.Contains(input.keyword) || it.CustomerCode.Contains(input.keyword) || it.CustomerName.Contains(input.keyword) || it.Status.ToString().Contains(input.keyword));
//返回由SqlSugar生成的SQL語句字元串
var sd = querywherr.ToSql().Key;
//groupby成主表數據
var query = querywherr.GroupBy((it, b) => new
{
it.Id,
......
})
//轉成輸出類
.Select((it, b) => new TmsBusiShippingNoticeListOutput
{
id = it.Id,
//返回明細的F_ShippingNo用'/'做分隔符, xxx1/xxx2/xxx3
shippingNo = SqlFunc.MappingColumn(default(string), $@"STUFF((select '/'+F_ShippingNo from tms_busi_shipping_notice_detail where F_CRMNo =it.F_CRMNo AND F_DeleteMark is null group by F_ShippingNo FOR xml path('')),1,1,'')"),
//運貨方式,取數據字典
transportMethod = SqlFunc.Subqueryable<DictionaryDataEntity>().Where(w => it.TransportMethod == w.EnCode && w.DictionaryTypeId == "501598831910").Select(w => w.FullName),
//轉中文前端顯示 '是' '否'
isShd = SqlFunc.IF(it.IsSHD == 1).Return("Y").End("N"),
.......
//彙總明細數量、金額
qty = SqlFunc.AggregateSum(b.Qty),
amount = SqlFunc.AggregateSum(b.Amount)
})
.MergeTable();
//聚合數據的查詢
query.WhereIF(!string.IsNullOrEmpty(input.shippingNo), it => it.shippingNo.Contains(input.shippingNo))
.WhereIF(!string.IsNullOrEmpty(input.qtyMin) && !string.IsNullOrEmpty(input.qtyMax), it => SqlFunc.Between(it.qty, input.qtyMin, input.qtyMax))
.WhereIF(!string.IsNullOrEmpty(input.amountMin) && !string.IsNullOrEmpty(input.amountMax), it => SqlFunc.Between(it.amount, input.amountMin, input.amountMax));
//添加排序
if (!string.IsNullOrEmpty(input.sidx))
{
query.OrderBy(input.sidx + " " + input.sort);
}
else
{
query.OrderBy(it => it.creatorTime, OrderByType.Desc);
}
//轉成PageResult
var data = await query.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<TmsBusiShippingNoticeListOutput>.SqlSugarPageResult(data);
}
ShippingNotice新增
沒有新增,這邊的發貨通知單都是從SAP上傳過來,後面去瞭解一下傳遞介面
同理也沒有刪除功能
ShippingNotice修改
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] TmsBusiShippingNoticeUpInput input)
{
//轉一下類型
var entity = input.Adapt<TmsBusiShippingNoticeEntity>();
//Updateable(修改對象) -> UpdateColumns(it => new {it.數據列}) -> ExecuteCommandAsync()
var isOk = await _repository.Context.Updateable(entity).UpdateColumns(it => new
{
it.CRMNo,
......
it.CostCenterName,
}).ExecuteCommandAsync();
if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1001);
}
批量修改ShippingNotice(鎖定)
//指示方法不應被視為可通過URL訪問的操作方法
[NonAction]
public async Task LockOrReleaseShippingNotice(List<string> shippingNoArray, int lockFlag)
{
//獲取要修改的List
var entity = await _repository.Context.Queryable<TmsBusiShippingNoticeEntity>().Where(x => shippingNoArray.Contains(x.CRMNo)).ToListAsync();
if (entity.Count > 0)
{
//批量修改
entity.ForEach(x =>
{
x.LockFlag = lockFlag;
x.LastModifyTime = DateTime.Now;
x.LastModifyUserId = _userManager.UserId;
});
var isOk = await _repository.Context.Updateable(entity).UpdateColumns(x => new { x.LockFlag, x.LastModifyTime, x.LastModifyUserId }).ExecuteCommandAsync();
}
}
導出
//和BaseCity幾乎一摸一樣的代碼,除了轉pararmList的字元串不一樣,為什麼不直接寫個通用方法算了???
[HttpGet("Actions/Export")]
public async Task<dynamic> Export([FromQuery] TmsBusiShippingNoticeListQueryInput input)
{
var exportData = new List<TmsBusiShippingNoticeListOutput>();
if (input.dataType == 0)
exportData = Clay.Object(await GetList(input)).Solidify<PageResult<TmsBusiShippingNoticeListOutput>>().list;
else
exportData = await GetNoPagingList(input);
List<ParamsModel> paramList = "[{\"value\":\"SAP單號\",\"field\":\"shippingNo\"},{\"value\":\"發貨通知單單號\",\"field\":\"cRMNo\"},{\"value\":\"業務類型\",\"field\":\"sysType\"}, ....... {\"value\":\"發貨數量\",\"field\":\"qty\"},{\"value\":\"發貨總金額\",\"field\":\"amount\"}]".ToList<ParamsModel>();
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = "發貨通知單.xls";
excelconfig.HeadFont = "微軟雅黑";
excelconfig.HeadPoint = 10;
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnModel = new List<ExcelColumnModel>();
foreach (var item in input.selectKey.Split(',').ToList())
{
var isExist = paramList.Find(p => p.field == item);
if (isExist != null)
excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
}
var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
ExcelExportHelper<TmsBusiShippingNoticeListOutput>.Export(exportData, excelconfig, addPath);
var fileName = _userManager.UserId + "|" + addPath + "|xls";
return new
{
name = excelconfig.FileName,
url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "SHZY")
};
}