username@email.com
2024-07-12 4a37150b246e610a583a190b6ffd4b7f017c6600
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
using IServices;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using zhengcaioa.Models;
 
 
namespace Services
{
    public class CooperVisitService: ICooperVisitService
    {
        private readonly zhengcaioaContext _context;
        private readonly IMapper _mapper;
        public CooperVisitService(zhengcaioaContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }
 
 
        public ResultEntity save(CooperVisitDTO dto)
        {
            ResultEntity resultEntity = new ResultEntity();
            try
            {
 
 
                var entity = _mapper.Map<CooperVisit>(dto);
 
 
                if (String.IsNullOrEmpty(entity.Id))
                {
                    entity.Id = Guid.NewGuid().ToString();
                    _context.CooperVisits.Add(entity);
                }
                else
                {
                    var updateproject = _context.CooperVisits.Find(entity.Id);
 
                    updateproject.Vistime = entity.Vistime;
                    updateproject.VisType = entity.VisType;
                    updateproject.Viscustomer = entity.Viscustomer;
                    updateproject.Jtype = entity.Jtype;
                    updateproject.Jdr = entity.Jdr;
                    updateproject.Zw = entity.Zw;
                    updateproject.Talk = entity.Talk;
                    updateproject.Khfk = entity.Khfk;
                    updateproject.Yx = entity.Yx;
                    updateproject.Jy = entity.Jy;
 
 
                    updateproject.RecStatus = entity.RecStatus;
                    updateproject.Modifier = entity.Modifier;
                    updateproject.Modifytime = entity.Modifytime;
 
                }
 
                _context.SaveChanges();
                resultEntity.ReturnID = entity.Id;
                resultEntity.Result = true;
            }
            catch (Exception ex)
            {
                resultEntity.Result = false;
                resultEntity.Message = "保存失败,请联系管理员";
 
            }
            return resultEntity;
        }
 
        public CooperVisitDTO Get(string id)
        {
 
            var entity = _context.CooperVisits.Find(id);
 
            if (entity.RecStatus != "A")
            {
                entity = new CooperVisit();
            }
 
            var result = _mapper.Map<CooperVisitDTO>(entity);
 
 
            return result;
        }
 
        public ResultDataEntity<CooperVisitDTO> SearchByPaging(CooperVisitDTOSearch searchEntity)
        {
 
 
 
            ResultDataEntity<CooperVisitDTO> data = new ResultDataEntity<CooperVisitDTO>();
            List<CooperVisitDTO> list = new List<CooperVisitDTO>();
 
 
 
 
            var listCode = (from a in _context.SysCodeDtls
                            join b in _context.SysCodes
                            on a.CodeId equals b.Id
                            where a.RecStatus == "A"
                                          && b.RecStatus == "A"
                            select new CodeDataEntity()
                            {
                                CodeId = b.Id,
                                CodeTable = b.CodeTable,
                                CodeField = b.CodeField,
                                CodeSn = a.CodeSn,
                                Comments = a.Comments,
                                Contents = a.Contents,
                                RecStatus = a.RecStatus,
                                Sort = a.Sort
                            }
                      );
            DateTime Vistimestart = DateTime.Now;
            DateTime Vistimeend = DateTime.Now;
            if (!string.IsNullOrWhiteSpace(searchEntity.Vistime))
            {
                string[] Vistimes = searchEntity.Vistime.Split("|");
                DateTime.TryParse(Vistimes[0], out Vistimestart);
                DateTime.TryParse(Vistimes[1], out Vistimeend);
                //Vistimeend = Vistimeend.AddDays(1);
            }
 
 
            var query = (from a in _context.CooperVisits
                         join b in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "visType")
                           on a.VisType equals b.CodeSn
                            into bzsssss
                         from bzzzzz in bzsssss.DefaultIfEmpty()
 
                         join c in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "jtype")
                         on a.Jtype equals c.CodeSn
                           into czsssss
                         from czzzzz in czsssss.DefaultIfEmpty()
 
                         join d in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "zw")
                          on a.Zw equals d.CodeSn
                            into dzsssss
                         from dzzzzz in dzsssss.DefaultIfEmpty()
 
                         join e in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "yx")
                         on a.Yx equals e.CodeSn
                           into ezsssss
                         from ezzzzz in ezsssss.DefaultIfEmpty()
 
                         join f in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "jy")
                          on a.Jy equals f.CodeSn
                            into fzsssss
                         from fzzzzz in fzsssss.DefaultIfEmpty()
 
                         join zz in _context.CooperatecustomCustomers
                         on a.Viscustomer equals zz.Id
 
                         //join g in _context.Areas on zz.Sheng equals g.CodeId
                         //join h in _context.Areas on zz.City equals h.CodeId
                         //join i in _context.Areas on zz.AreaId equals i.CodeId
 
                         join zzz in _context.PltUsers.Where(x => x.RecStatus == "A")
                        on a.Creater equals zzz.Id
                        into zsssss
                         from zzzzz in zsssss.DefaultIfEmpty()
 
 
                         where a.RecStatus == "A"
                          && (string.IsNullOrWhiteSpace(searchEntity.Vistime) || (a.Vistime >= Vistimestart && a.Vistime <= Vistimeend))
                         && (string.IsNullOrWhiteSpace(searchEntity.Name) || zz.Name.Contains(searchEntity.Name.Trim()))
                          && (string.IsNullOrWhiteSpace(searchEntity.Khly) || zz.Khly == searchEntity.Khly.Trim())
                           && (string.IsNullOrWhiteSpace(searchEntity.Hyfl) || zz.Hyfl == searchEntity.Hyfl.Trim())
                            && (string.IsNullOrWhiteSpace(searchEntity.Khlx) || zz.Khlx == searchEntity.Khlx.Trim())
                             && (string.IsNullOrWhiteSpace(searchEntity.Ywjl) || zz.Ywjl == searchEntity.Ywjl.Trim())
                              && (string.IsNullOrWhiteSpace(searchEntity.Sheng) || zz.Sheng == searchEntity.Sheng.Trim())
                               && (string.IsNullOrWhiteSpace(searchEntity.City) || zz.City == searchEntity.City.Trim())
                                && (string.IsNullOrWhiteSpace(searchEntity.AreaId) || zz.AreaId == searchEntity.AreaId.Trim())
                                 && (string.IsNullOrWhiteSpace(searchEntity.Viscustomer) || a.Viscustomer == searchEntity.Viscustomer.Trim())
                         select new CooperVisitDTO
                         {
                             Id = a.Id,
                             Vistime = a.Vistime,
                             VisType = a.VisType,
                             Viscustomer = a.Viscustomer,
                             Name = zz.Name,
                             Jtype = a.Jtype,
                             Jdr = a.Jdr,
                             //ShengName = g.Name + "-" + h.Name + "-" + i.Name,
                             VisTypeName = bzzzzz.Comments,
                             JtypeName = czzzzz.Comments,
                             ZwName = dzzzzz.Comments,
                             YxName = ezzzzz.Comments,
                             JyName = fzzzzz.Comments,
                             Zw = a.Zw,
                             Talk = a.Talk,
                             Khfk = a.Khfk,
                             Yx = a.Yx,
 
                             Jy = a.Jy,
                             Creater = a.Creater,
                             Createtime = a.Createtime,
                             CreaterName = zzzzz.UserName,
                             RecStatus = a.RecStatus,
                             Modifier = a.Modifier,
                             Modifytime = a.Modifytime,
                             VistimeName = a.Vistime.ToString("yyyy-MM-dd"),
                         }
                ).OrderByDescending(x => x.Vistime).ToList();
 
 
 
            //if (searchEntity.totalrows == 0)
                searchEntity.totalrows = query.Count();
            var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
            data.LoadData(searchEntity, lianlist);
            return data;
        }
 
 
 
 
 
 
        /// <summary>
        /// 修改主表状态
        /// </summary>
        /// <param name="id">主id</param>
        /// <param name="userid">用户</param>
        /// <returns></returns>
        public ResultEntity ModifyStatus(string id, string userid)
        {
            ResultEntity result = new ResultEntity();
            result.Result = true;
 
            var model = _context.CooperVisits.Find(id);
            if (model != null)
            {
                model.RecStatus = "D";
                model.Modifier = userid;
                model.Modifytime = DateTime.Now;
                _context.SaveChanges();
            }
 
            return result;
        }
 
        /// <summary>
        /// 获取所有有效合作客户
        /// </summary>
        /// <returns></returns>
        public List<CooperatecustomCustomerDTO> GetList(string[] id=null)
        {
            List<CooperatecustomCustomerDTO> result = new List<CooperatecustomCustomerDTO>();
            if (id != null)
            {
               // var listCooperVisits = _context.CooperVisits.Where(r => id.Contains(r.Id) && r.RecStatus == "A").Select(x => x.Viscustomer).ToList();
 
                var listCooperatecustomCustomers = _context.CooperatecustomCustomers.Where(r => id.Contains(r.Id) && r.RecStatus == "A").ToList();
 
                result = _mapper.Map<List<CooperatecustomCustomerDTO>>(listCooperatecustomCustomers);
            }
            else
            {
                var listCooperatecustomCustomers = _context.CooperatecustomCustomers.ToList();
 
                result = _mapper.Map<List<CooperatecustomCustomerDTO>>(listCooperatecustomCustomers);
            }
 
 
 
 
            return result;
        }
 
        public List<CooperVisitDTO> GetListsalary(string userid, DateTime datemin, DateTime datemax)
        {
            
            
                // var listCooperVisits = _context.CooperVisits.Where(r => id.Contains(r.Id) && r.RecStatus == "A").Select(x => x.Viscustomer).ToList();
 
                var listCooperatecustomCustomers = _context.CooperVisits.Where(r =>  r.RecStatus == "A"&& r.Creater == userid && r.Vistime >= datemin && r.Vistime < datemax).ToList();
 
               var result = _mapper.Map<List<CooperVisitDTO>>(listCooperatecustomCustomers);
             
 
 
 
 
            return result;
        }
    }
}