WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
using JiepeiWMS.Common.Enums;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.Common.HttpContextUser;
using JiepeiWMS.Extends;
using JiepeiWMS.Filter;
using JiepeiWMS.IServices;
using JiepeiWMS.Model;
using JiepeiWMS.Model.Models;
using JiepeiWMS.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class WMQuoteInfoController : ControllerBase
    {
        /// <summary>
        /// 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下
        /// </summary>
        private readonly IWMQuoteInfoServices _BllWMQuoteInfo;
        private readonly IWMQuoteInfoLogServices _BllWMQuoteInfoLog;
        private readonly IWMPurchaseQuoteDetailServices _BllWMPurchaseQuoteDetail;
        private readonly IWMSupplierServices _BllWMSupplier;
        private readonly IWMPurchaseInfoServices _BllWMPurchase;
        private readonly ISysUserInfoServices _BllSysUserInfo;
        private readonly ISysOrgServices _BllSysOrg;
        private readonly IWMProductUnitServices _BllWMProductUnit;
        private readonly IUser _User;
        ILogger<WMQuoteInfoController> _Logger;
 
        public WMQuoteInfoController(
            IWMQuoteInfoServices BllWMQuoteInfo,
            IWMPurchaseQuoteDetailServices BllWMPurchaseQuoteDetail,
            IWMSupplierServices BllWMSupplier,
            ISysUserInfoServices BllSysUserInfo,
            ISysOrgServices BllSysOrg,
            IWMProductUnitServices BllWMProductUnit,
            IUser User,
           ILogger<WMQuoteInfoController> Logger,
           IWMQuoteInfoLogServices BllWMQuoteInfoLog,
           IWMPurchaseInfoServices BllWMPurchase
        )
        {
            _BllWMQuoteInfo = BllWMQuoteInfo;
            _BllWMPurchaseQuoteDetail = BllWMPurchaseQuoteDetail;
            _BllWMSupplier = BllWMSupplier;
            _BllSysUserInfo = BllSysUserInfo;
            _BllSysOrg = BllSysOrg;
            _BllWMProductUnit = BllWMProductUnit;
            _User = User;
            _Logger = Logger;
            _BllWMQuoteInfoLog = BllWMQuoteInfoLog;
            _BllWMPurchase = BllWMPurchase;
        }
 
        [HttpGet]
        public async Task<MessageModel<PageModel<WMQuoteInfo>>> GetList(
            int? Id = null, DateTime? StartTime = null, DateTime? EndTime = null,
            string QuoteCode = null,
            int PageIndex = 1, int PageSize = 20)
        {
            Expression<Func<WMQuoteInfo, bool>> whereExpression =
                (pg) => pg.SysOrgId == _User.SysOrgId;
            if (Id != null) { whereExpression = whereExpression.And((pg) => pg.Id == Id); }
            if (!string.IsNullOrEmpty(QuoteCode))
            {
                QuoteCode = QuoteCode.Trim();
                whereExpression = whereExpression.And((pg) => pg.QuoteCode == QuoteCode);
            }
            if (StartTime.HasValue)
            {
                whereExpression = whereExpression.And((pg) => pg.CreateTime >= StartTime.Value);
            }
            if (EndTime.HasValue)
            {
                EndTime = EndTime.Value.AddSeconds(1);
                whereExpression = whereExpression.And((pg) => pg.CreateTime < EndTime.Value);
            }
            var pglst = await _BllWMQuoteInfo.QueryTabsPage<WMQuoteInfo, WMSupplier, sysUserInfo, sysUserInfo, WMQuoteInfo>(
                    (pg, s, ue, uc) => new object[] {
                        JoinType.Left, pg.SupplierId==s.Id,
                        JoinType.Left,pg.EmployeeId==ue.uID,
                        JoinType.Left,pg.CheckAdminId==uc.uID
                    },
                    (pg, s, ue, uc) => new WMQuoteInfo
                    {
                        ActualAmount = pg.ActualAmount,
                        AddRemark = pg.AddRemark,
                        CheckAdminId = pg.CheckAdminId,
                        Bank = pg.Bank,
                        BankAccount = pg.BankAccount,
                        CheckKey = pg.CheckKey,
                        CheckRank = pg.CheckRank,
                        CheckRemark = pg.CheckRemark,
                        CreateTime = pg.CreateTime,
                        EmployeeId = pg.EmployeeId,
                        ExpectedArrivalTime = pg.ExpectedArrivalTime,
                        FileUrl = pg.FileUrl,
                        Payee = pg.Payee,
                        Id = pg.Id,
                        PayPic = pg.PayPic,
                        PayType = pg.PayType,
                        QuoteCode = pg.QuoteCode,
                        Remark = pg.Remark,
                        SettlementAmount = pg.SettlementAmount,
                        Status = pg.Status,
                        SupplierId = pg.SupplierId,
                        UpdateTime = pg.UpdateTime,
                        EmployeeName = ue.uRealName,
                        SupplierName = s.Name,
                        SysOrgId = pg.SysOrgId
                    },
                    whereExpression, PageIndex, PageSize, "pg.Id desc");
 
            var dicstatus = typeof(EnumWMPurchaseDetailStatus)._GetValueDescriptionDicFromEnumType<EnumWMPurchaseDetailStatus>();
            foreach (var i in pglst.data)
            {
                string name;
                i.StatusName = dicstatus.TryGetValue((EnumWMPurchaseDetailStatus)i.Status, out name) ? name : string.Empty;
            }
 
            //测试接口
            //var dsdfss = await _BllWMQuoteInfo.QueryById(2121);
            //var sdd = await _BllWMPurchaseQuoteDetail.Query(x => x.QuoteId == dsdfss.Id);
            //foreach (var item in sdd)
            //{
            //    item.inputNum = 2;
            //}
            //var s = await _BllWMQuoteInfo.BeginPurchaseinInNC(dsdfss, sdd);
            //var s = await _BllWMPurchaseQuoteDetail.BeginPurchaseinInNC(dsdfss, sdd);
 
            return new MessageModel<PageModel<WMQuoteInfo>>()
            {
                msg = "获取成功",
                success = true,
                response = pglst
            };
 
        }
 
        [HttpGet]
        public async Task<MessageModel<object>> GetParams()
        {
            var dicStatus = typeof(EnumWMPurchaseDetailStatus)._GetValueDescriptionDicFromEnumType<EnumWMPurchaseDetailStatus>().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
            var dicSupplierId = await _BllWMSupplier.Query();
            var dicEmployeeId = await _BllSysUserInfo.Query(t => t.tdIsDelete == false);
            var dicPurchaseUnit = await _BllWMProductUnit.Query();
            var dicSysOrgId = await _BllSysOrg.Query();
            var dicPayType = typeof(EnumPayType)._GetValueDescriptionDicFromEnumType<EnumPayType>().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
 
            return new MessageModel<object>()
            {
                msg = "获取成功",
                success = true,
                response = new
                {
                    dicStatus,
                    dicSupplierId,
                    dicEmployeeId,
                    dicPurchaseUnit,
                    dicSysOrgId,
                    dicPayType
                }
            };
        }
 
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] WMQuoteInfo request)
        {
            var data = new MessageModel<string>();
 
            var id = await _BllWMQuoteInfo.Add(request);
            data.success = id > 0;
 
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
 
            return data;
        }
 
        [HttpPut]
        public async Task<MessageModel<string>> Edit([FromBody] VMWMQuoteInfo request)
        {
            string msg = string.Empty;
            if (request.Id <= 0)
            {
                msg = "采购单编号有误";
                goto ErrorResult;
            }
 
            switch (request.Status)
            {
                case 40:
                    msg = await _BllWMQuoteInfo.ConfirmQuoteInfo(request, _User.ID);
                    break;
                case 46:
                    msg = await _BllWMQuoteInfo.ConfirmPayInfo(request, _User.ID);
                    break;
                case 78:
                    msg = await _BllWMQuoteInfo.Purchase(request.Id, _User.ID);
                    break;
                default:
                    msg = await _BllWMQuoteInfo.EditInfo(request, _User.ID);
                    break;
            }
            if (!string.IsNullOrEmpty(msg))
            {
                goto ErrorResult;
            }
            return new MessageModel<string>
            {
                success = true,
                msg = "更新成功"
            };
 
        ErrorResult:
            return new MessageModel<string>
            {
                success = false,
                msg = msg
            };
        }
 
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id = 0)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var detail = await _BllWMQuoteInfo.QueryById(id);
 
                if (detail != null)
                {
                    data.success = await _BllWMQuoteInfo.Delete(detail);
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = detail?.Id.ObjToString();
                    }
                }
            }
            return data;
        }
 
        [HttpDelete]
        public async Task<MessageModel<string>> DeleteMore(string Ids = "")
        {
            var data = new MessageModel<string>();
            var lstid = Ids._ToIntList();
            if (lstid.Count > 0)
            {
                var ok = await _BllWMQuoteInfo.DeleteByIds(lstid.Select(t => (object)t).ToArray());
                if (ok)
                {
                    data.msg = "删除成功";
                    data.response = string.Empty;
                }
            }
            return data;
        }
 
        [HttpGet]
        public async Task<MessageModel<VMWMQuoteInfo>> Detail(int Id)
        {
            var detail = new VMWMQuoteInfo();
            var quoteinfo = await _BllWMQuoteInfo.GetModel(t => t.Id == Id);
            if (quoteinfo == null)
            {
                quoteinfo = new WMQuoteInfo { Id = 0 };
            }
            var purchasedetails = await _BllWMPurchaseQuoteDetail.QueryTabsPage<WMPurchaseQuoteDetail, WMProductList, WMProductInfo, WMProductSpec, Department, WMPurchaseQuoteDetail>(
                (pg, pl, pi, ps, d) => new object[] {
                    JoinType.Left, pg.ProductId == pl.Id,
                    JoinType.Left, pg.QuoteId == pi.Id,
                    JoinType.Left, pl.SpecIds == ps.Id,
                    JoinType.Left, pg.DepartmentId == d.Id },
                (pg, pl, pi, ps, d) => new WMPurchaseQuoteDetail
                {
                    Id = pg.Id,
                    Amount = pg.Amount,
                    CheckNum = pg.CheckNum,
                    CreateTime = pg.CreateTime,
                    DepartmentId = pg.DepartmentId,
                    DepartmentName = d.Name,
                    DisAmount = pg.DisAmount,
                    Discount = pg.Discount,
                    DiscountPrice = pg.DiscountPrice,
                    Price = pg.Price,
                    ProductCode = pl.Code,
                    ProductId = pg.ProductId,
                    ProductName = pl.Name,
                    ProductSpec = ps.Name,
                    PurchaseId = pg.PurchaseId,
                    PurchaseNum = pg.PurchaseNum,
                    PurchaseUnit = pg.PurchaseUnit,
                    Quantity = pg.Quantity,
                    QuoteId = pg.QuoteId,
                    Remark = pg.Remark,
                    Status = pg.Status,
                    StorageNum = pg.StorageNum,
                    SysOrgId = pg.SysOrgId,
                    TypeId = pg.TypeId,
                    UpdateTime = pg.UpdateTime
                },
               pg => pg.QuoteId == quoteinfo.Id
            );
            detail.ListDetail = purchasedetails.data.Select(t =>
            {
                t.StatusName = ((EnumWMPurchaseDetailStatus)t.Status)._GetDescriptionByEnumValue<EnumWMPurchaseDetailStatus>();
                return t;
            }).ToList();
            return new MessageModel<VMWMQuoteInfo>()
            {
                msg = "获取成功",
                success = true,
                response = detail
            };
        }
 
        /// <summary>
        /// 报价审核
        /// </summary>
        /// <param name="QuoteCode">代码</param>
        /// <param name="IsPass">是否通过</param>
        /// <param name="Msg">不通过原因</param>
        ///  <param name="RejectType">拒绝类型 0 通过 1临时 2永久</param>
        /// <returns>结果</returns>
        [HttpGet, AllowAnonymous, AuthKeySecret]
        public async Task<MessageModel<string>> SetQuoteCheckResult(string QuoteCode, bool? IsPass, string Msg,int RejectType)
        {
            var msg = string.Empty;
            if (!IsPass.HasValue)
            {
                msg = "IsPass必须设置值";
                goto ErrorResult;
            }
            if (!IsPass.Value && string.IsNullOrWhiteSpace(Msg))
            {
                msg = "审核失败必须设置原因";
                goto ErrorResult;
            }
            if (RejectType<0)
            {
                msg = "RejectType必须设置值";
                goto ErrorResult;
            }
 
            msg = await _BllWMQuoteInfo.SetCheckQuoteIsPass(QuoteCode, IsPass.Value, Msg,RejectType);
 
            if (!string.IsNullOrEmpty(msg))
            {
                goto ErrorResult;
            }
 
            return new MessageModel<string>()
            {
                msg = string.Empty,
                success = true
            };
 
        ErrorResult:
            return new MessageModel<string>()
            {
                msg = msg,
                success = false
            };
        }
 
        /// <summary>
        /// 请款审核
        /// </summary>
        /// <param name="QuoteCode">代码</param>
        /// <param name="IsPass">是否通过</param>
        /// <param name="Msg">不通过原因</param>
        /// <param name="RejectType">拒绝类型 0 通过 1临时 2永久</param>
        /// <returns>结果</returns>
        [HttpGet, AllowAnonymous, AuthKeySecret]
        public async Task<MessageModel<string>> SetPayCheckResult(string QuoteCode, bool? IsPass, string Msg,int RejectType)
        {
            var msg = string.Empty;
            if (!IsPass.HasValue)
            {
                msg = "IsPass必须设置值";
                goto ErrorResult;
            }
            if (!IsPass.Value && string.IsNullOrWhiteSpace(Msg))
            {
                msg = "审核失败必须设置原因";
                goto ErrorResult;
            }
            if (RejectType < 0)
            {
                msg = "RejectType必须设置值";
                goto ErrorResult;
            }
 
            msg = await _BllWMQuoteInfo.SetCheckPayIsPass(QuoteCode, IsPass.Value, Msg,RejectType);
 
            if (!string.IsNullOrEmpty(msg))
            {
                goto ErrorResult;
            }
 
            return new MessageModel<string>()
            {
                msg = string.Empty,
                success = true
            };
 
        ErrorResult:
            return new MessageModel<string>()
            {
                msg = msg,
                success = false
            };
        }
 
        /// <summary>
        /// 下单采购
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<string>> Purchase(int Id)
        {
            var msg = await _BllWMQuoteInfo.Purchase(Id, _User.ID);
 
            if (string.IsNullOrEmpty(msg))
            {
                goto ErrorResult;
            }
 
            return new MessageModel<string>()
            {
                success = true
            };
 
        ErrorResult:
            return new MessageModel<string>()
            {
                msg = msg,
                success = false
            };
        }
 
        /// <summary>
        /// 完成交易
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<string>> Done(int Id)
        {
            var msg = await _BllWMQuoteInfo.Done(Id, _User.ID);
 
            if (!string.IsNullOrEmpty(msg))
            {
                goto ErrorResult;
            }
 
            return new MessageModel<string>()
            {
                msg = "设置完成",
                success = true
            };
 
        ErrorResult:
            return new MessageModel<string>()
            {
                msg = msg,
                success = false
            };
        }
    }
}