bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
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
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;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// 部门服务实践
    /// </summary>
    public class DepartmentServer : IDepartmentServer
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly IDepartmentRepository _department;
        readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="server">部门仓储接口</param>
        /// <param name="mapper">automapper</param>
        public DepartmentServer(IDepartmentRepository department, IMapper mapper)
        {
            _department = department;//部门
            _mapper = mapper;//automapper
        }
 
        /// <summary>
        /// 获取部门信息列表
        /// </summary>
        /// <param name="DepartmentName">部门名称</param>
        /// <param name="DepartmentNo">部门号</param>
        /// <returns></returns>
        public List<DepartmentDto> GetDepartmentList(string DepartmentName, string DepartmentNo)
        {
            List<DepartmentDto> departmentlist = _department.GetDepartmentList(DepartmentName, DepartmentNo);
            return departmentlist;
        }
 
        /// <summary>
        /// 根据id获取部门信息
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        public SysDepartment GetDepartmentById(int id)
        {
            SysDepartment department = _department.GetDepartmentById(id);
            return department;
        }
 
        /// <summary>
        /// 根据部门号获取部门信息
        /// </summary>
        /// <returns></returns>
        public int GetDepartmentByNo(string DepartmentNo)
        {
            List<SysDepartment> department = _department.GetDepartmentByNo(DepartmentNo);
            return department.Count;
        }
      
        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="departmentdto">部门dto</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> AddDepartment(DepartmentDto departmentdto)
        {
            //捕获异常
            try
            {
                //模型映射
                SysDepartment department = _mapper.Map<SysDepartment>(departmentdto);
                //判断部门号是否唯一
                int count = GetDepartmentByNo(department.DepartmentNo);
                int i = 0;
                if (count > 0)
                {
                    i = 3;
                }
                else if (count == 0)
                {
                    i = await _department.AddDepartment(department);
                }
                 return i;
                
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("新增部门异常", ex);
            }
        }
 
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="department">部门实体模型</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> DelDepartment(SysDepartment department)
        {
            //捕获异常
            try
            {
                //删除
                int i = await _department.DelDepartment(department);
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("删除部门异常", ex);
            }
        }
 
        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <param name="departmentdto">部门dto</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> ExitDepartment(DepartmentDto departmentdto)
        {
            //捕获异常
            try
            {
                //模型映射
                SysDepartment department = _mapper.Map<SysDepartment>(departmentdto);
                //编辑
                int i = await _department.ExitDepartment(department);
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("编辑部门异常", ex);
            }
        }
    }
}