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
using JiepeiWMS.Common.HttpContextUser;
using JiepeiWMS.Extends;
using JiepeiWMS.IServices;
using JiepeiWMS.Model;
using JiepeiWMS.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
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 WMProductLocationController : ControllerBase
    {
        /// <summary>
        /// 商品库位
        /// </summary>
        private readonly IWMProductLocationServices _wMProductLocationServices;
        private readonly ISysUserInfoServices _sysUserInfoServices;
        private readonly ISysOrgServices _BllSysOrg;
        readonly IUser _user;
 
        public WMProductLocationController(IWMProductLocationServices WMProductLocationServices, ISysUserInfoServices SysUserInfoServices, ISysOrgServices BllSysOrg, IUser user)
        {
            _wMProductLocationServices = WMProductLocationServices;
            _sysUserInfoServices = SysUserInfoServices;
            _BllSysOrg = BllSysOrg;
            _user = user;
        }
 
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="key">关键字</param>
        /// <param name="intPageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<WMProductLocation>>> Index(int page = 1, string key = "", int intPageSize = 20)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
 
            Expression<Func<WMProductLocation, bool>> whereExpression = s => s.Id > 0;
 
            //筛选主体Id
            var userInfoModel = await _sysUserInfoServices.QueryById(_user.ID);
            if (userInfoModel != null)
            {
                whereExpression = whereExpression.And(s => s.SysOrgId == userInfoModel.SysOrgId);
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                whereExpression = whereExpression.And(s => s.LocationName != null && s.LocationName.Contains(key.Trim()));
            }
 
            var productLocation = await _wMProductLocationServices.QueryPage(whereExpression, page, intPageSize, " Id desc ");
 
            foreach (var item in productLocation.data)
            {
                var UserInfoModel = await _sysUserInfoServices.QueryById(item.AdminId);
                if (UserInfoModel != null)
                {
                    item.AdminName = UserInfoModel.uRealName;
                }
            }
 
            return new MessageModel<PageModel<WMProductLocation>>()
            {
                msg = "获取成功",
                success = productLocation.dataCount >= 0,
                response = productLocation
            };
 
        }
 
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<object>> Get(int id = 0)
        {
            var locationList = await _wMProductLocationServices.Query(x => x.IsUse == true);
 
            locationList = locationList.Where(x => x.SysOrgId == _user.SysOrgId).ToList();
 
            return new MessageModel<object>()
            {
                msg = "获取成功",
                success = true,
                response = new
                {
                    locationList
                }
            };
        }
 
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] WMProductLocation request)
        {
            var data = new MessageModel<string>();
 
            var ProductLocation = await _wMProductLocationServices.GetModel(w => w.LocationName == request.LocationName);
            if (ProductLocation != null)
            {
                data.msg = "库位名称已存在";
                return data;
            }
 
            request.AdminId = _user.ID;
            request.CreateTime = DateTime.Now;
            request.LocationNumFree = request.LocationStockNum;
 
            request.SysOrgId = _user.SysOrgId;
            var id = await _wMProductLocationServices.Add(request);
            data.success = id > 0;
 
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
 
            return data;
        }
 
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Update([FromBody] WMProductLocation request)
        {
            var data = new MessageModel<string>();
 
            if (request.Id > 0)
            {
                var ProductLocation = await _wMProductLocationServices.GetModel(w => w.LocationName == request.LocationName && w.Id != request.Id);
                if (ProductLocation != null)
                {
                    data.msg = "库位名称已存在";
                    return data;
                }
 
                request.UpdateTime = DateTime.Now;
                request.LocationNumFree = request.LocationStockNum;
                data.success = await _wMProductLocationServices.Update(request);
                if (data.success)
                {
                    data.msg = "更新成功";
                    data.response = request?.Id.ObjToString();
                }
            }
 
            return data;
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpDelete]
        //public async Task<MessageModel<string>> Delete(int id = 0)
        //{
        //    var data = new MessageModel<string>();
        //    if (id > 0)
        //    {
        //        var detail = await _wMProductLocationServices.QueryById(id);
 
        //        detail.IsUse = false;
 
        //        if (detail != null)
        //        {
        //            data.success = await _wMProductLocationServices.Update(detail);
        //            if (data.success)
        //            {
        //                data.msg = "删除成功";
        //                data.response = detail?.Id.ObjToString();
        //            }
        //        }
        //    }
 
        //    return data;
        //}
    }
}