| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using Yitter.IdGenerator;
- namespace Admin.NET.Plugin.AiDOP.Supply;
- /// <summary>
- /// 物料需求计划服务
- /// </summary>
- [ApiDescriptionSettings(Order = 305, Description = "物料需求计划")]
- [Route("api/Supply")]
- [AllowAnonymous]
- [NonUnify]
- public class DemandScheduleService : IDynamicApiController, ITransient
- {
- private readonly ISqlSugarClient _db;
- private readonly SqlSugarRepository<DemandSchedule> _rep;
- private readonly UserManager _userManager;
- public DemandScheduleService(
- ISqlSugarClient db,
- SqlSugarRepository<DemandSchedule> rep,
- UserManager userManager)
- {
- _db = db;
- _rep = rep;
- _userManager = userManager;
- }
- [DisplayName("物料需求计划列表")]
- [HttpGet("demand-schedule/list")]
- public async Task<object> GetList([FromQuery] DemandScheduleListInput input)
- {
- var page = input.Page <= 0 ? 1 : input.Page;
- var pageSize = input.PageSize <= 0 ? 10 : input.PageSize;
- var offset = (page - 1) * pageSize;
- var pars = new List<SugarParameter>();
- var where = new List<string>
- {
- "(a.ishistoryversion = 'N' OR a.ishistoryversion IS NULL)",
- "IFNULL(a.IsDeleted,0)=0"
- };
- if (_userManager.TenantId > 0)
- {
- where.Add("a.tenant_id = @TenantId");
- pars.Add(new SugarParameter("@TenantId", _userManager.TenantId));
- }
- if (!string.IsNullOrWhiteSpace(input.ItemNum))
- {
- where.Add("a.itemnum LIKE @ItemNum");
- pars.Add(new SugarParameter("@ItemNum", $"%{input.ItemNum.Trim()}%"));
- }
- if (!string.IsNullOrWhiteSpace(input.Descr))
- {
- where.Add("b.Descr LIKE @Descr");
- pars.Add(new SugarParameter("@Descr", $"%{input.Descr.Trim()}%"));
- }
- if (!string.IsNullOrWhiteSpace(input.RequestDateFrom) && DateTime.TryParse(input.RequestDateFrom, out var reqFrom))
- {
- where.Add("a.requestdate >= @RequestDateFrom");
- pars.Add(new SugarParameter("@RequestDateFrom", reqFrom.Date));
- }
- var fromSql = $"""
- FROM ic_demandschedule a
- INNER JOIN ItemMaster b ON a.itemnum = b.ItemNum AND CAST(a.factory_id AS CHAR(64)) = CAST(b.Domain AS CHAR(64))
- WHERE {string.Join(" AND ", where)}
- """;
- var total = await _db.Ado.GetIntAsync($"SELECT COUNT(1) {fromSql}", pars);
- var list = await _db.Ado.SqlQueryAsync<DemandScheduleListRow>(
- $"""
- SELECT
- a.Id AS Id,
- a.itemnum AS ItemNum,
- b.Descr AS Descr,
- b.Descr1 AS Descr1,
- a.fversion AS FVersion,
- a.drawing AS Drawing,
- a.requestdate AS RequestDate,
- a.arrivaldate AS ArrivalDate,
- a.shortqty AS ShortQty,
- a.mesqty AS MesQty,
- a.locqty AS LocQty,
- a.sechedqty AS SechedQty,
- a.tosechedqty AS ToSechedQty,
- CASE IFNULL(a.status,'') WHEN 'P' THEN '已发布' ELSE '' END AS StatusText,
- CASE IFNULL(a.status,'') WHEN 'P' THEN DATE_FORMAT(a.update_time,'%Y-%m-%d') ELSE '' END AS PublishDate,
- IFNULL(a.status,'') AS Status,
- a.remarks AS Remarks,
- a.tenant_id AS TenantId,
- a.factory_id AS FactoryId,
- a.company_id AS CompanyId
- {fromSql}
- ORDER BY a.requestdate DESC, a.Id DESC
- LIMIT {pageSize} OFFSET {offset}
- """,
- pars);
- return new { total, page, pageSize, list };
- }
- [DisplayName("获取物料需求计划详情")]
- [HttpGet("demand-schedule/{id:long}")]
- public async Task<object> GetDetail(long id)
- {
- var row = await _rep.GetFirstAsync(x => x.Id == id && (x.IsDeleted == null || x.IsDeleted == 0));
- if (row == null) throw Oops.Oh("记录不存在");
- return row;
- }
- [DisplayName("保存物料需求计划")]
- [ApiDescriptionSettings(Name = "SaveDemandSchedule"), HttpPost("demand-schedule/save")]
- public async Task<object> Save([FromBody] DemandScheduleSaveInput input)
- {
- if (string.IsNullOrWhiteSpace(input.ItemNum)) throw Oops.Oh("物料编号不能为空");
- if (input.Id is null or 0)
- {
- var entity = input.Adapt<DemandSchedule>();
- entity.Id = YitIdHelper.NextId();
- entity.ItemNum = input.ItemNum?.Trim();
- entity.Status ??= string.Empty;
- entity.IsHistoryVersion ??= "N";
- entity.IsDeleted ??= 0;
- entity.TenantId ??= _userManager.TenantId;
- entity.CompanyId ??= 1000;
- entity.CreateBy = _userManager.UserId;
- entity.CreateByName = _userManager.Account;
- entity.CreateTime = DateTime.Now;
- entity.UpdateBy = _userManager.UserId;
- entity.UpdateByName = _userManager.Account;
- entity.UpdateTime = DateTime.Now;
- entity.RequestDate = ParseDate(input.RequestDate);
- entity.ArrivalDate = ParseDate(input.ArrivalDate);
- await _rep.InsertAsync(entity);
- return new { id = entity.Id, message = "新增成功" };
- }
- else
- {
- var entity = await _rep.GetFirstAsync(x => x.Id == input.Id.Value && (x.IsDeleted == null || x.IsDeleted == 0))
- ?? throw Oops.Oh("记录不存在");
- entity.ItemNum = input.ItemNum?.Trim();
- entity.FVersion = input.FVersion;
- entity.Drawing = input.Drawing;
- entity.RequestDate = ParseDate(input.RequestDate);
- entity.ArrivalDate = ParseDate(input.ArrivalDate);
- entity.ShortQty = input.ShortQty;
- entity.MesQty = input.MesQty;
- entity.LocQty = input.LocQty;
- entity.SechedQty = input.SechedQty;
- entity.ToSechedQty = input.ToSechedQty;
- entity.Remarks = input.Remarks;
- entity.CompanyId = input.CompanyId ?? entity.CompanyId ?? 1000;
- entity.FactoryId = input.FactoryId ?? entity.FactoryId;
- entity.UpdateBy = _userManager.UserId;
- entity.UpdateByName = _userManager.Account;
- entity.UpdateTime = DateTime.Now;
- await _rep.UpdateAsync(entity);
- return new { id = entity.Id, message = "编辑成功" };
- }
- }
- [DisplayName("删除物料需求计划")]
- [HttpPost("demand-schedule/delete/{id:long}")]
- public async Task<object> Delete(long id)
- {
- var entity = await _rep.GetFirstAsync(x => x.Id == id && (x.IsDeleted == null || x.IsDeleted == 0))
- ?? throw Oops.Oh("记录不存在");
- entity.IsDeleted = 1;
- entity.UpdateBy = _userManager.UserId;
- entity.UpdateByName = _userManager.Account;
- entity.UpdateTime = DateTime.Now;
- await _rep.UpdateAsync(entity);
- return new { message = "删除成功" };
- }
- [DisplayName("勾选发布")]
- [HttpPost("demand-schedule/publish-selected")]
- public async Task<object> PublishSelected([FromBody] DemandScheduleBatchIdsInput input)
- {
- var ids = ParseIds(input.Ids);
- if (!ids.Any()) throw Oops.Oh("请选择要发布的数据");
- var affected = await _db.Updateable<DemandSchedule>()
- .SetColumns(x => new DemandSchedule
- {
- Status = "P",
- UpdateBy = _userManager.UserId,
- UpdateByName = _userManager.Account,
- UpdateTime = DateTime.Now
- })
- .Where(x => ids.Contains(x.Id) && (x.IsDeleted == null || x.IsDeleted == 0) && (x.Status == null || x.Status == ""))
- .ExecuteCommandAsync();
- return new { affected, message = affected > 0 ? "发布成功" : "无可发布数据" };
- }
- [DisplayName("全部发布")]
- [HttpPost("demand-schedule/publish-all")]
- public async Task<object> PublishAll()
- {
- var query = _db.Updateable<DemandSchedule>()
- .SetColumns(x => new DemandSchedule
- {
- Status = "P",
- UpdateBy = _userManager.UserId,
- UpdateByName = _userManager.Account,
- UpdateTime = DateTime.Now
- })
- .Where(x => (x.IsDeleted == null || x.IsDeleted == 0) && (x.Status == null || x.Status == ""));
- if (_userManager.TenantId > 0)
- query = query.Where(x => x.TenantId == _userManager.TenantId);
- var affected = await query.ExecuteCommandAsync();
- return new { affected, message = affected > 0 ? "全部发布成功" : "无可发布数据" };
- }
- [DisplayName("取消发布")]
- [HttpPost("demand-schedule/unpublish-selected")]
- public async Task<object> UnPublishSelected([FromBody] DemandScheduleBatchIdsInput input)
- {
- var ids = ParseIds(input.Ids);
- if (!ids.Any()) throw Oops.Oh("请选择要取消发布的数据");
- var affected = await _db.Updateable<DemandSchedule>()
- .SetColumns(x => new DemandSchedule
- {
- Status = "",
- UpdateBy = _userManager.UserId,
- UpdateByName = _userManager.Account,
- UpdateTime = DateTime.Now
- })
- .Where(x => ids.Contains(x.Id) && (x.IsDeleted == null || x.IsDeleted == 0) && x.Status == "P")
- .ExecuteCommandAsync();
- return new { affected, message = affected > 0 ? "取消发布成功" : "无可取消发布数据" };
- }
- private static DateTime? ParseDate(string? value)
- {
- return DateTime.TryParse(value, out var dt) ? dt.Date : null;
- }
- private static List<long> ParseIds(string ids)
- {
- return ids.Split(',', StringSplitOptions.RemoveEmptyEntries)
- .Select(x => long.TryParse(x.Trim(), out var id) ? id : 0)
- .Where(x => x > 0)
- .Distinct()
- .ToList();
- }
- private sealed class DemandScheduleListRow
- {
- public long Id { get; set; }
- public string? ItemNum { get; set; }
- public string? Descr { get; set; }
- public string? Descr1 { get; set; }
- public string? FVersion { get; set; }
- public string? Drawing { get; set; }
- public DateTime? RequestDate { get; set; }
- public DateTime? ArrivalDate { get; set; }
- public decimal? ShortQty { get; set; }
- public decimal? MesQty { get; set; }
- public decimal? LocQty { get; set; }
- public decimal? SechedQty { get; set; }
- public decimal? ToSechedQty { get; set; }
- public string? Status { get; set; }
- public string? StatusText { get; set; }
- public string? PublishDate { get; set; }
- public string? Remarks { get; set; }
- public long? TenantId { get; set; }
- public long? FactoryId { get; set; }
- public long? CompanyId { get; set; }
- }
- }
|