bklLiudl
2024-08-23 62b3bf8205277b9bdceaa83f0d7e5d73312e1780
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
using AutoMapper;
using Model.ModelDto.SysDto;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
using SqlSugar;
using WMS.Entity.Context;
using Utility;
using Microsoft.AspNetCore.Identity;
using System.Data;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// 部门服务实践
    /// </summary>
    public class DepartmentServer : IDepartmentServer
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly IDepartmentRepository _department;
        readonly IMapper _mapper;
        private static readonly SqlSugarScope Db = DataContext.Db;
        private readonly UserManager _userManager;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="server">部门仓储接口</param>
        /// <param name="mapper">automapper</param>
        public DepartmentServer(IDepartmentRepository department, IMapper mapper, UserManager userManager)
        {
            _department = department;//部门
            _mapper = mapper;//automapper
            _userManager = userManager;
        }
 
        /// <summary>
        /// 获取部门信息列表
        /// </summary>
        /// <param name="DepartmentName">部门名称</param>
        /// <param name="DepartmentNo">部门号</param>
        /// <returns></returns>
        public async Task<List<DepartmentDto>> GetDepartmentList(string DepartmentName, string DepartmentNo)
        {
            return await Db.Queryable<SysDepartment>()
                           .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id)
                           .WhereIF(!string.IsNullOrEmpty(DepartmentNo), a => a.DepartmentNo.Contains(DepartmentNo))
                           .WhereIF(!string.IsNullOrEmpty(DepartmentName), a => a.DepartmentName.Contains(DepartmentName))
                           .Where(a => a.IsDel == "0")
                           .Select<DepartmentDto>((a, b) => new DepartmentDto() { CreateUserName = b.RealName }, true)
                           .ToListAsync();
        }
 
        /// <summary>
        /// 根据id获取部门信息
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        public async Task<SysDepartment> GetDepartmentById(int id)
        {
            return await Db.Queryable<SysDepartment>().FirstAsync(s => s.Id == id);
        }
 
        /// <summary>
        /// 根据部门号获取部门信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetDepartmentByNo(string DepartmentNo)
        {
            return await Db.Queryable<SysDepartment>().CountAsync(s => s.DepartmentNo == DepartmentNo && s.IsDel == "0");
        }
 
        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="departmentdto">部门dto</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> AddDepartment(DepartmentDto departmentdto)
        {
            //模型映射
            SysDepartment department = _mapper.Map<SysDepartment>(departmentdto);
            //判断部门号是否唯一
            int count = await GetDepartmentByNo(department.DepartmentNo);
            if (count > 0)
            {
                throw Oops.Bah("部门号必须唯一");
            }
            department.CreateTime = DateTime.Now;
            department.CreateUser = _userManager.UserId;
            return await Db.Insertable(department).ExecuteCommandAsync();
 
        }
 
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="department">部门实体模型</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> DelDepartment(SysDepartment department)
        {
            return await Db.Updateable<SysDepartment>()
                           .Where(s => s.Id == department.Id)
                           .SetColumns(s => s.IsDel == "1")
                           .SetColumns(s => s.UpdateTime == DateTime.Now)
                           .SetColumns(s => s.UpdateUser == _userManager.UserId)
                           .ExecuteCommandAsync();
        }
 
        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <param name="departmentdto">部门dto</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> ExitDepartment(DepartmentDto departmentdto)
        {
            SysDepartment department = _mapper.Map<SysDepartment>(departmentdto);
            int count = await Db.Queryable<SysDepartment>().CountAsync(s => s.DepartmentNo == departmentdto.DepartmentNo && s.Id != departmentdto.Id && s.IsDel == "0");
            if (count > 0)
                throw Oops.Bah("部门号必须唯一");
            department.UpdateUser = _userManager.UserId;
            department.UpdateTime = DateTime.Now;
            return await Db.Updateable(department)
                .UpdateColumns(s => new { s.DepartmentNo, s.DepartmentName, s.Demo, s.UpdateUser, s.UpdateTime })
                .ExecuteCommandAsync();
        }
    }
}