TemplateLicense.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\common\library\ps;
  3. use app\common\model\ps\MarketLicense;
  4. use app\common\model\ps\Template;
  5. use think\Exception;
  6. /**
  7. * 单次购买:导入本厂库一次,确认套打后锁定,不可再套打。
  8. */
  9. class TemplateLicense
  10. {
  11. /**
  12. * 套打前校验
  13. * @param Template $template
  14. * @param int $factoryId
  15. * @throws Exception
  16. */
  17. public static function assertPrintable(Template $template, $factoryId)
  18. {
  19. if ((int)$template->is_system === 1) {
  20. return;
  21. }
  22. if ((int)$template->factory_id !== (int)$factoryId) {
  23. throw new Exception('模板不属于当前工厂');
  24. }
  25. if ($template->license_type === 'market_single' && (int)$template->locked === 1) {
  26. throw new Exception('该模板为单次购买,已使用过,不允许再次套打');
  27. }
  28. }
  29. /**
  30. * 确认套打后消耗单次授权
  31. * @param Template $template
  32. * @param int $factoryId
  33. */
  34. public static function consumeOnConfirm(Template $template, $factoryId)
  35. {
  36. if ($template->license_type !== 'market_single' || (int)$template->locked === 1) {
  37. return;
  38. }
  39. $template->save([
  40. 'locked' => 1,
  41. 'locked_time' => time(),
  42. ]);
  43. MarketLicense::where('factory_id', $factoryId)
  44. ->where('template_id', $template->id)
  45. ->where('license_type', 'single')
  46. ->update([
  47. 'remain_times' => 0,
  48. 'locked' => 1,
  49. 'used_time' => time(),
  50. ]);
  51. }
  52. }