songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using sbcLabSystem.Data.Domain.Backstage;
using sbcLabSystem.Service.QC;
using sbcLabSystem.Service.Account;
using PalGain.Core;
using BatchService.Framework.Utility;
using System.Web.UI.WebControls;
 
namespace sbcLabSystem.Models.Backstage
{
    public class QCDistributionInfoViewModel
    {
        public int Id { get; set; }
        public string DistNo { get; set; }
        public DateTime? IssuedDate { get; set; }
        public DateTime? CloseDate { get; set; }
        public DateTime? ModifyDate { get; set; }
        public string Remark { get; set; }
        public string IssuedDateString { get; set; }
        public string CloseDateString { get; set; }
        public int UsedLabCount { get; set; }
        public List<QCDistributionRegisterInfoViewModel> LabList { get; set; }
        public int CurrentPageIndex { get; set; }
        public int LastPageIndex { get; set; }
        public AnswerInfoViewModel AnswerInfo { get; set; }
        public string AnswerJSON { get; set; }
        public bool IsSubmitAll { get; set; }
        /// <summary>
        /// 不包含质控参与记录,仅仅为质控发布对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static QCDistributionInfoViewModel FromEntity(QCDistribution entity)
        {
            //var usedLabList = new List<QCDistributionRegisterInfo>();
            //if (entity.QCDistributionRegisters != null && entity.QCDistributionRegisters.Count > 0)
            //{
            //    entity.QCDistributionRegisters.ForEach(x =>
            //    {
            //        var tempInfo = PalGainEngine.Instance.Resolve<AccountService>().GetUserRequestInfoQueryable()
            //            .FirstOrDefault(p => p.Id == x.LabId);
            //        if (tempInfo != null)
            //        {
            //            usedLabList.Add(x);
            //        }
            //    });
            //} 
            QCDistributionInfoViewModel viewModel = new QCDistributionInfoViewModel()
            { 
                Id = entity.Id,
                DistNo = entity.DistNo,
                Remark = entity.Remark,
                CloseDate = entity.CloseDate,
                IssuedDate = entity.IssuedDate,
                IssuedDateString = entity.IssuedDate.GetValueOrDefault().ToString("yyyy-MM-dd"),
                CloseDateString = entity.CloseDate.GetValueOrDefault().ToString("yyyy-MM-dd"),
                UsedLabCount = entity.QCDistributionRegisters != null ? 
                entity.QCDistributionRegisters.Where(p => p.ProjectId <= 4).Count() : 0,
                ModifyDate = entity.ModifyDate.GetValueOrDefault(), 
                AnswerJSON = entity.AnswerJSON,
                IsSubmitAll = entity.IsAllSubmit,
            };
            return viewModel;
        }
        /// <summary>
        /// 包括质控参与记录列表对象
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="maxItemCount"></param>
        /// <returns></returns>
        public static QCDistributionInfoViewModel FromEntity(QCDistribution entity, int currentPageIndex, int maxItemCount)
        {
            //QCDistributionInfoViewModel viewModel = FromEntity(entity);
            //viewModel.LabList = PalGainEngine.Instance.Resolve<AccountService>().GetUserRequestInfoQueryable().OrderBy(p => p.Id)
            //    .Skip((currentPageIndex - 1) * maxItemCount).Take(maxItemCount).ToList()
            //    .Select(p => UserRequestViewModel.FromEntity(p)).ToList();
            //viewModel.LabList.ForEach(x =>
            //{ 
            //    QCDistributionRegisterInfo registerInfo = entity.QCDistributionRegisters.FirstOrDefault(p => p.LabId == x.Id && p.QCDistributionId == entity.Id);
            //    if (registerInfo != null)
            //    {
            //        x.IsSelected = true;
            //        x.IsCharged = registerInfo.IsCharged;
            //        x.ProjectId = registerInfo.ProjectId;
            //        x.SampleNo = registerInfo.SampleNo;
            //        x.IsSendEMS = registerInfo.IsSendEMS;
            //        x.EMSNo = registerInfo.EMSNo;
            //        x.PacketContent = registerInfo.PacketContent;
            //        x.QCDistributionRegisterId = registerInfo.Id;
            //    }
            //});
            //viewModel.LabRegisterList = entity.QCDistributionRegisters.ToList().Select(p => QCDistributionRegisterInfoViewModel.FromEntity(p)).ToList();
            //return viewModel;
            return null;
        }
        public static List<QCDistributionInfoViewModel> ToList()
        {
            int itemesCountOnePage = 100;
            var list = PalGainEngine.Instance.Resolve<QCService>().GetQcDistributions().OrderByDescending(p => p.Id)
                .Take(itemesCountOnePage).ToList();
            return list.Select(p => QCDistributionInfoViewModel.FromEntity(p)).ToList();
        }
        public static QCDistribution ToEntityForAnswerInfo(QCDistributionInfoViewModel viewModel)
        {
            QCDistribution exist = PalGainEngine.Instance.Resolve<QCService>().GetQcDistributions().FirstOrDefault(p => p.Id == viewModel.Id);
            if (viewModel.AnswerInfo == null)
            {
                viewModel.AnswerInfo = new AnswerInfoViewModel();
            }
            exist.AnswerJSON = JsonHelper.JsonSerializer<AnswerInfoViewModel>(viewModel.AnswerInfo);
            return exist;
        }
        public static QCDistribution ToEntity(QCDistributionInfoViewModel viewModel)
        {
            QCDistribution exist = PalGainEngine.Instance.Resolve<QCService>().GetQcDistributions().FirstOrDefault(p => p.Id == viewModel.Id);
            if (exist == null)
            {
                exist = new QCDistribution();
            }
            if (viewModel.CloseDate != null)
            {
                exist.CloseDate = viewModel.CloseDate;
            }
            if (viewModel.IssuedDate != null) 
            {
                exist.IssuedDate = viewModel.IssuedDate;
            }
            exist.DistNo = viewModel.DistNo; 
            exist.Remark = viewModel.Remark;
            exist.ModifyDate = DateTime.Now;
 
            if (viewModel.LabList != null && viewModel.LabList.Count > 0)
            {
                viewModel.LabList.ForEach(x =>
                {
                    QCDistributionRegisterInfo registerInfo = QCDistributionRegisterInfoViewModel.ToEntity(x);
                    registerInfo.QCDistributionId = viewModel.Id;
                    registerInfo.LabId = x.LabId;
                    registerInfo.ProjectId = x.ProjectId;
                    if (x.IsSelected)
                    { 
                        registerInfo.IsCharged = x.IsCharged;
                        PalGainEngine.Instance.Resolve<QCService>().SaveQcDistributionRegister(registerInfo);
                    }
                    else
                    {
                        var entity = PalGainEngine.Instance.Resolve<QCService>().GetQcDistributionRegisters()
                                .FirstOrDefault(p => p.Id == x.Id);
                        if (entity != null)
                        {
                            PalGainEngine.Instance.Resolve<QCService>().DeleteQcDistributionRegister(entity);
                        }
                    }
                });
            }
            return exist;
        }
    }
}