移动系统liao
2025-05-01 a247547df86f0fad8f03aebb91de68d3f2bc7918
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
using cylsg.Model.ECTEModel;
using cylsg.Model.utilityViewModel;
using ECTESTOA;
using Furion.LinqBuilder;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.UnionShopTargetAddPlanRequest.Types;
 
namespace cylsg.Application.CyOS
{
    /// <summary>
    /// 财务报销控制器
    /// </summary>
    [Authorize]
    [ApiDescriptionSettings("CYOA")]
    public  class CyOSBaoXiaoCaiWuController: IDynamicApiController
    {
        private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
        private readonly IOAServices _OAServices;
        private readonly ISqlSugarClient _SugarClient;
        private ECTESTOAPermissions Permissions;
        public CyOSBaoXiaoCaiWuController(IOAServices oaservices, ISqlSugarClient sugarClient)
        {
            _OAServices = oaservices;
            _SugarClient = sugarClient.AsTenant().GetConnection("ECTESTOADB");
        }
 
 
        /// <summary>
        ///  查询
        /// </summary>
        [HttpPost]
        public async Task<WorekPlandto> getplanelist(SearchOaWorekPlan param)
        {
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
 
            Expression<Func<OaWorkPlan, bool>> SearchList = (x) => true;
            SearchList = SearchList.And(x => x.ApprovalStatus != PlanStatusType.Uncommitted);
            //川印工作单
            SearchList = SearchList.And(x => x.MemberId == _OAServices.firmId);
            SearchList = SearchList.And(x => x.PlanType == 3);
            SearchList = SearchList.And(x => x.PlanDataType == 17);
            //必须是审批完成后的单
            SearchList = SearchList.And(x=>x.ApprovalStatus== PlanStatusType.Yes);
            if (!string.IsNullOrEmpty(param.StaffName))
            {
                SearchList = SearchList.And(x => x.SentStaff.Name.Contains(param.StaffName));
            }
            if (param.StaffMemberIdId != null)
            {
                SearchList = SearchList.And(x => x.SentStaffId == param.StaffMemberIdId);
            }
            if (param.PlanDataType > 0)
            {
                SearchList = SearchList.And(x => x.PlanDataType == param.PlanDataType);
            }
            if (param.PlanStartTimeStart != null)
            {
                SearchList = SearchList.And(x => x.PlanStartTime > param.PlanStartTimeStart);
            }
            if (param.PlanStartTimeEnd != null)
            {
                SearchList = SearchList.And(x => x.PlanStartTime <= param.PlanStartTimeEnd.Value.AddDays(1));
            }
            ////审批状态
            //if (param.ApprovalStatus != null)
            //{
 
            //    SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus);
 
 
            //}
 
            //报销状态
            if (param.EvaluationStatus != null)
            {
                SearchList = SearchList.And(x => x.EvaluationStatus == param.EvaluationStatus);
            }
            //强制增加员工过滤
            RefAsync<int> totle = 0;
            var data = await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.OaWorkPlanShenpis).Includes(x=>x.SentStaff)
                .Where(SearchList).OrderByDescending(x => x.MemberId).ToPageListAsync(param.page.PageIndex, param.page.PageSize, totle);
            
            param.page.TotalCount = totle;
            var sum = await _SugarClient.Queryable<OaWorkPlan>()
                .Where(SearchList).SumAsync(x => x.PlanMoney);
            return new WorekPlandto
 
            {
                list = data,
                page = param.page,
                TotalMoney = sum ?? 0
            };
 
 
        }
 
        /// <summary>
        ///  获取报销单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OaWorkPlan> GetPlanInfo(int id)
        {
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
            return await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.OaWorkPlanShenpis,y=>y.ApprovalStaff).Includes(x => x.SentStaff).Includes(x => x.Depart).Includes(x=>x.WorkPlanAttachments).Where(x => x.Keyid == id).FirstAsync();
        }
 
        /// <summary>
        /// 报销
        /// </summary>       
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> ApprovalPlan(OaWorkPlan plan)
        {
 
 
            // 等待信号量
            await _semaphore.WaitAsync();
            try
            {
                if (plan.Keyid == 0)
                    throw Oops.Oh("ID错误");
                if (plan.FirmAccountId == null)
                    throw Oops.Oh("必须选择银行账号");
 
                if (!await CheckRols())
                {
                    throw Oops.Oh("没有权限");
                }
                var FirmAccount = await _SugarClient.Queryable<OA_FirmAccount>().Where(x => x.FirmId == _OAServices.firmId && x.Keyid == plan.FirmAccountId && x.Status == true).FirstAsync();
                if (FirmAccount == null)
                    throw Oops.Oh("账号错误");
                var data = await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.SentStaff).Where(x => x.Keyid == plan.Keyid).FirstAsync();
                if (data == null)
                    throw Oops.Oh("没有找到报销单");
 
                if (data.ApprovalStatus != PlanStatusType.Yes)
                    throw Oops.Oh("未审批完成,不可报销");
 
 
 
 
                if (data.EvaluationStatus != BaoxiaoStausType.Reimbursed)
                {
                    await _SugarClient.AsTenant().BeginTranAsync();
                    try
                    {
                        data.EvaluationStatus = BaoxiaoStausType.Reimbursed;
                        //data.PlanContent = plan.PlanContent;
                        data.LastUpdateTime = DateTime.Now;
                        await _SugarClient.Updateable(data).ExecuteCommandAsync();
 
                        FirmAccount.LastUpdateTime = DateTime.Now;
                        FirmAccount.Balance -= (Math.Round(data.PlanMoney ?? 0, 2));
                        FirmAccount.AllExpenses += (Math.Round(data.PlanMoney ?? 0, 2));
 
                        await _SugarClient.Updateable(FirmAccount).ExecuteCommandAsync();
                        OA_FirmAccountRecord rec = new OA_FirmAccountRecord
                        {
                            AccountId = FirmAccount.Keyid,
                            Money = data.PlanMoney ?? 0,
                            LastUpdateTime = DateTime.Now,
                            Department = data.PlanContent,
                            RecordTypeId = 2,
                            SubjectId = 3,//报销必须为3
                            Operator = Permissions.Name,
                            PaymentUnit = data.SentStaff.Name,
                            ResidualAmount = FirmAccount.Balance,
                            OperationalMatters = data.PlanContent,
 
 
                        };
                        await _SugarClient.Insertable(rec).ExecuteCommandAsync();
                        await _SugarClient.Updateable(data).ExecuteCommandAsync();
                        await _SugarClient.AsTenant().CommitTranAsync();
                        return true;
                    }
                    catch (Exception)
                    {
                        await _SugarClient.AsTenant().RollbackTranAsync();
                        throw;
                    }
 
 
 
 
 
 
 
 
                }
 
 
 
                return true;
            }
            catch(Exception e)
            {
                throw;
            }
            finally
            {
                // 释放信号量
                _semaphore.Release();
            }
 
         
           
 
        }
        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<OA_FirmAccount>> GetFirmAccountList()
        {
            return await _SugarClient.Queryable<OA_FirmAccount>().Where(x => x.Status == true && x.FirmId == _OAServices.firmId).Select(x => new OA_FirmAccount
            {
                AccountName = x.AccountName,
                 AccountType = x.AccountType,
                  UserName = x.UserName,   
                  Keyid=x.Keyid,
               
               
            }).WithCache(3600).ToListAsync();
        }
 
        /// <summary>
        /// 判断是否具有权限
        /// </summary>
        /// <returns></returns>
        private async Task<bool> CheckRols()
        {
            var pr = await _OAServices.GetOAPermissions();
            Permissions = pr;
            if (pr != null && pr.MemberId != null)
            {
 
                if (pr.BF_IsCaiWu)
                    return true;
                else return false;
 
            }
            else
                return false;
        }
    }
}