public virtual async Task GenerateLicenses(SoftLicenseGenerateItemInput input) { if (input.Count < 1) { throw new UserFriendlyException("生成卡密数量不能小于1!"); } var licenseOption = await _softLicenseOptionRepository.FirstOrDefaultAsync(t => t.Id == input.SoftLicenseOptionId && t.SoftId == input.SoftId); if (licenseOption == null) { throw new UserFriendlyException("价格类型不存在!"); } for (int i = 0; i < input.Count; i++) { SoftLicense license = new SoftLicense(); license.SoftId = licenseOption.SoftId; license.LicenseType = licenseOption.LicenseType; license.LicenseNo = MD5Helper.EnCode16Bit(Guid.NewGuid().ToString()); license.ApplyTime = DateTime.Now; license.Status = SoftLicenseStatus.Normal; license.SellType = SoftLicenseSellType.None; license.Price = licenseOption.Price; await _softLicenseRepository.InsertAsync(license); } }
public static int Insert(SoftLicense softLicense) { using (var connection = new DBConnection()) using (var command = DBConnection.CreateCommand()) { command.CommandText = InsertQuery; if (softLicense == null) { MessageBox.Show("В метод Insert не передана ссылка на объект здания", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(-1); } command.Parameters.Add(DBConnection.CreateParameter("IDVersion", softLicense.IdVersion)); command.Parameters.Add(DBConnection.CreateParameter("IDLicType", softLicense.IdLicType)); command.Parameters.Add(DBConnection.CreateParameter("IDDocType", softLicense.IdDocType)); command.Parameters.Add(DBConnection.CreateParameter("IDSupplier", softLicense.IdSupplier)); command.Parameters.Add(DBConnection.CreateParameter("IDDepartment", softLicense.IdDepartment)); command.Parameters.Add(DBConnection.CreateParameter("DocNumber", softLicense.DocNumber)); command.Parameters.Add(DBConnection.CreateParameter("InstallationsCount", softLicense.InstallationsCount)); command.Parameters.Add(DBConnection.CreateParameter("BuyLicenseDate", softLicense.BuyLicenseDate)); command.Parameters.Add(DBConnection.CreateParameter("ExpireLicenseDate", softLicense.ExpireLicenseDate)); command.Parameters.Add(DBConnection.CreateParameter("Description", softLicense.Description)); try { return(Convert.ToInt32(connection.SqlExecuteScalar(command), CultureInfo.InvariantCulture)); } catch (SqlException e) { connection.SqlRollbackTransaction(); MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Не удалось добавить лицензию на программное обеспечение в базу данных. Подробная ошибка: {0}", e.Message), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(-1); } } }
public static int Update(SoftLicense softLicense) { using (var connection = new DBConnection()) using (var command = DBConnection.CreateCommand()) { command.CommandText = UpdateQuery; if (softLicense == null) { MessageBox.Show("В метод Update не передана ссылка на объект лицензии", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(-1); } command.Parameters.Add(DBConnection.CreateParameter("IDVersion", softLicense.IdVersion)); command.Parameters.Add(DBConnection.CreateParameter("IDLicType", softLicense.IdLicType)); command.Parameters.Add(DBConnection.CreateParameter("IDDocType", softLicense.IdDocType)); command.Parameters.Add(DBConnection.CreateParameter("IDSupplier", softLicense.IdSupplier)); command.Parameters.Add(DBConnection.CreateParameter("IDDepartment", softLicense.IdDepartment)); command.Parameters.Add(DBConnection.CreateParameter("DocNumber", softLicense.DocNumber)); command.Parameters.Add(DBConnection.CreateParameter("InstallationsCount", softLicense.InstallationsCount)); command.Parameters.Add(DBConnection.CreateParameter("BuyLicenseDate", softLicense.BuyLicenseDate)); command.Parameters.Add(DBConnection.CreateParameter("ExpireLicenseDate", softLicense.ExpireLicenseDate)); command.Parameters.Add(DBConnection.CreateParameter("Description", softLicense.Description)); command.Parameters.Add(DBConnection.CreateParameter("IDLicense", softLicense.IdLicense)); try { return(connection.SqlExecuteNonQuery(command)); } catch (SqlException e) { MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Не удалось изменить данные о лицензии на программное обеспечение. Подробная ошибка: {0}", e.Message), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(-1); } } }
public virtual async Task Authorize(SoftLicense softLicense, SoftUser softUser) { #region 检查参数是否有效 if (softLicense == null) { throw new UserFriendlyException("卡密数据不存在!"); } if (softUser == null) { throw new UserFriendlyException("用户不存在!"); } if (softLicense.Status != SoftLicenseStatus.Sell) { throw new UserFriendlyException("卡密不存在或已被使用!"); } if (!softUser.IsActive) { throw new UserFriendlyException("用户账号被停用,无法授权!"); } //判断用户是否已经授权本软件至永久 var userLicenseInfo = await _softUserLicenseRepository.FirstOrDefaultAsync(t => t.SoftId == softLicense.SoftId && t.SoftUserId == softUser.Id); if (userLicenseInfo != null && !userLicenseInfo.ExpireTime.HasValue) { throw new UserFriendlyException("软件已经授权到永久,无需授权!"); } #endregion #region 授权 if (userLicenseInfo == null) { userLicenseInfo = new SoftUserLicense(); userLicenseInfo.SoftId = softLicense.SoftId; userLicenseInfo.SoftUserId = softUser.Id; userLicenseInfo.AuthorizeTime = DateTime.Now; userLicenseInfo.IsActive = true; userLicenseInfo.Type = SoftUserLicenseType.Fee; switch (softLicense.LicenseType) { case SoftLicenseType.Day: userLicenseInfo.ExpireTime = DateTime.Now.AddDays(1); break; case SoftLicenseType.Forever: userLicenseInfo.ExpireTime = null; break; case SoftLicenseType.Hour: userLicenseInfo.ExpireTime = DateTime.Now.AddHours(1); break; case SoftLicenseType.Month: userLicenseInfo.ExpireTime = DateTime.Now.AddMonths(1); break; case SoftLicenseType.Week: userLicenseInfo.ExpireTime = DateTime.Now.AddDays(7); break; case SoftLicenseType.Year: userLicenseInfo.ExpireTime = DateTime.Now.AddYears(1); break; } await _softUserLicenseRepository.InsertAsync(userLicenseInfo); } else { userLicenseInfo.Type = SoftUserLicenseType.Fee; switch (softLicense.LicenseType) { case SoftLicenseType.Day: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddDays(1); break; case SoftLicenseType.Forever: userLicenseInfo.ExpireTime = null; break; case SoftLicenseType.Hour: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddHours(1); break; case SoftLicenseType.Month: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddMonths(1); break; case SoftLicenseType.Week: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddDays(7); break; case SoftLicenseType.Year: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddYears(1); break; } await _softUserLicenseRepository.UpdateAsync(userLicenseInfo); } #endregion softLicense.Status = SoftLicenseStatus.HasUse; softLicense.SoftUserId = softUser.Id; softLicense.UseTime = DateTime.Now; await _softLicenseRepository.UpdateAsync(softLicense); }