Пример #1
0
 public async Task <IApiResult> CarRequest([FromBody] Models.Parameter.P_Devices.P_CarRequest pars)
 {
     return(await ApiAsync(pars, new BLL.DeviceManager.Device().CarRequest));
 }
Пример #2
0
        public Task <IApiResult> CarRequest(Models.Parameter.P_Devices.P_CarRequest arg)
        {
            return(Task.Run(() =>
            {
                IApiResult hr = new IApiResult();
                using (CarRentEntities cre = new CarRentEntities())
                {
                    try
                    {
                        //var deviceQuery = from d in cre.Devices where d.DeviceID == arg.deviceid select new { d.Status,d.DeviceName };
                        //var device = deviceQuery.First();
                        //if (device.Status == 1)
                        //{

                        //}
                        var dev = from d in cre.Devices
                                  join l in cre.LeaseRecord
                                  on d.DeviceID equals l.DeviceID
                                  into lr
                                  from dli in lr.DefaultIfEmpty()
                                  where d.DeviceID == arg.deviceid && d.Deleted == false
                                  orderby dli.Created descending
                                  select new{ dli.Status, devStatus = d.Status, d.UserID, d.DeviceID };
                        var query = dev.FirstOrDefault();
                        //Status (1:已租,0:未租)
                        if (query.Status == 1)
                        {
                            hr.code = StatusCode.failure;
                            hr.message = "Occupied."; //已经被别人申请了
                        }
                        else if (query.Status == 2)
                        {
                            hr.code = StatusCode.failure;
                            hr.message = "Occupied.";//已经租出去了
                        }
                        else
                        {
                            LeaseRecord lr = new LeaseRecord();
                            lr.DeviceID = query.DeviceID;
                            //1 已申请,等待确认,2 正在出租,3出租完成,4已拒绝
                            lr.Status = 1;
                            lr.UserPhone = arg.phone;
                            lr.UserID = query.UserID;
                            lr.Created = DateTime.Now;
                            lr = cre.LeaseRecord.Add(lr);
                            //DAL.Devices device = new DAL.Devices();
                            //device.Status = 1;
                            cre.SaveChanges();
                            hr.message = "success!";
                        }
                    }
                    catch (InvalidOperationException ioe)
                    {
                        hr.message = "未找到元素";
                        hr.code = StatusCode.error;
                        Log.Error(this, ioe);
                    }
                    catch (Exception ex)
                    {
                        hr.message = ex.Message;
                        hr.code = StatusCode.error;
                        Log.Error(this, ex);
                    }
                    return hr;
                }
            }));
        }