移动系统liao
2025-01-24 f4a0b32daa9e021227707bcfb70b67738bd1b000
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
using cylsg.Model.ECTEModel;
using cylsg.Model.utilityViewModel;
using ECTESTOA;
using Furion.LinqBuilder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
 
namespace cylsg.Application.CyOS
{
    /// <summary>
    /// 报销主管相关控制器
    /// </summary>
    [Authorize]
    public class CyOSBaoXiaoZHuGuanController: IDynamicApiController
    {
        private readonly IOAServices _OAServices;
        private readonly ISqlSugarClient _SugarClient;
        private ECTESTOAPermissions Permissions;
        public CyOSBaoXiaoZHuGuanController(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.unApproval);
            //SearchList = SearchList.And(x =>  x.OaWorkPlanShenpis.Where(y => y.Buzhou == 1 && y.ApprovalStaffId == Permissions.MemberId).Count()<=0);
            //SearchList = SearchList.And(x => x.DepartId == Permissions.DepartId); 部门筛选
            if (!string.IsNullOrEmpty(param.StaffName))
            {
                SearchList = SearchList.And(x => x.SentStaff.Name.Contains(param.StaffName));
            }
            if (param.StaffMemberIdId != null)
            {
                SearchList = SearchList.And(x => x.MemberId == 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)
            {
                if (param.ApprovalStatus == PlanStatusType.Yes)
                    SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus || ((x.OaWorkPlanShenpis.Count(y => y.ApprovalStatus == PlanStatusType.Yes&&y.ApprovalStaffId==Permissions.MemberId && y.Buzhou == 1) >0)));
                else  if(param.ApprovalStatus == PlanStatusType.No)
                {
                    SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus );
                }
                else
                {
                    SearchList= SearchList.And(x=>x.ApprovalStatus== PlanStatusType.unApproval&&(x.OaWorkPlanShenpis.Count(y => y.Buzhou == 1&& y.ApprovalStaffId == Permissions.MemberId) <=0));
                }
 
            }
            //报销状态
            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.Depart)
                    .Includes(x=>x.OaWorkPlanShenpis.Where(y=>y.ApprovalStaffId==Permissions.MemberId&&y.Buzhou==1).ToList())
                    .Where(SearchList).OrderByDescending(x => x.PlanStartTime).Mapper(
                    x =>
                    {
                        if (x.ApprovalStatus == PlanStatusType.unApproval)
                        {
                            var oashenpi = x.OaWorkPlanShenpis?.FirstOrDefault();
                            if (oashenpi != null)
                                x.ApprovalStatus = oashenpi.ApprovalStatus;
                            
 
                        }
                            ;
                    }
                    ).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.Where(y => y.Buzhou == 1).ToList(), y => y.ApprovalStaff).Includes(x => x.SentStaff).Includes(x => x.Depart).Includes(x => x.WorkPlanAttachments).Where(x=>x.Keyid==id).Mapper(x=>
            {
                if (x.ApprovalStatus == PlanStatusType.unApproval)
                {
                    var oashenpi = x.OaWorkPlanShenpis?.Where(x=>x.ApprovalStaffId==Permissions.MemberId).FirstOrDefault();
                    if (oashenpi != null)
                        x.ApprovalStatus = oashenpi.ApprovalStatus;
 
 
                }
                            ;
            }).FirstAsync();
        }
 
        /// <summary>
        /// 审批权限
        /// </summary>       
        /// <returns></returns>
         [HttpPost]
        public async Task<bool> ApprovalPlan(OaWorkPlanShenpi param)
        {
            if (param.ApprovalStatus == PlanStatusType.unApproval)
                throw Oops.Oh("不可设置为未审批状态");
 
            if (!await CheckRols())
            {
                throw Oops.Oh("没有权限");
            }
            var data = await _SugarClient.Queryable<OaWorkPlan>().Includes(x=>x.OaWorkPlanShenpis).Where(x => x.Keyid == param.OA_WorkPlanId).FirstAsync();
            if (data == null)
                throw Oops.Oh("没有找到报销单");
            if (data.ApprovalStatus != null && data.ApprovalStatus != PlanStatusType.unApproval)
                throw Oops.Oh("报销单已经批复完毕");
            if(data.ApprovalStatus== PlanStatusType.Uncommitted)
            {
                throw Oops.Oh("报销单未提交不可审批");
            }
            OaWorkPlanShenpi shenpi = data.OaWorkPlanShenpis.Where(x => x.ApprovalStaffId == Permissions.MemberId&&x.Buzhou==1).FirstOrDefault();
             if (shenpi!=null)
            {
                //自己已经批复,不可更改
                throw Oops.Oh("报销单已经批复");
                //shenpi.ApprovalStatus = PlanStatusType.Yes;
                //await _SugarClient.Updateable(shenpi).ExecuteCommandAsync();
            }
             else
            {
    
 
                data.RewardResult = param.RewardResult;
                data.PlanContent = param.PlanContent;
                if (param.PlanMoney > 0 && param.PlanMoney <= data.PlanMoney)
                    data.PlanMoney = param.PlanMoney;
 
                await   _SugarClient.AsTenant().BeginTranAsync();
                try
                {
                    if (param.ApprovalStatus == PlanStatusType.No)
                    {
                        //当有人审批拒绝后直接将申请单设置为拒绝,不在进行其他审核
                        data.ApprovalStatus = PlanStatusType.No;
                        data.ApprovalTime = DateTime.Now;
                        data.LastUpdateTime = DateTime.Now;
                        data.Operator = Permissions.Name;
                       await  _SugarClient.Updateable(data).ExecuteCommandAsync();
 
                    }
 
                    shenpi = new OaWorkPlanShenpi()
                    {
                        ApprovalStaffId = Permissions.MemberId,
                        ApprovalContent = param.ApprovalContent,
                        ApprovalStatus = param.ApprovalStatus,
                        ApprovalTime = DateTime.Now,
                        Buzhou = 1,
                        CreateTime = DateTime.Now,
                        Operator = Permissions.Name,
                        OA_WorkPlanId = param.OA_WorkPlanId,
 
                    };
                    await _SugarClient.Insertable(shenpi).ExecuteCommandAsync();
 
                    await _SugarClient.AsTenant().CommitTranAsync();
                }
                catch (Exception)
                {
                    await _SugarClient.AsTenant().RollbackTranAsync();
                    throw;
                }
             
            }
            return true;
        }
 
        /// <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_IsBaoxiaozhuguan)
                    return true;
                else return false;
 
            }
            else
                return false;
        }
    }
 
   
}