MarketPublisher.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\common\library\ps;
  3. use app\common\model\ps\MarketGoods;
  4. use app\common\model\ps\Template;
  5. use think\Exception;
  6. /**
  7. * 工厂把本厂识版模板上架到商城。
  8. */
  9. class MarketPublisher
  10. {
  11. /**
  12. * @param int $templateId
  13. * @param int $priceSingle
  14. * @param int $priceBuyout
  15. * @param string $title
  16. * @param string $intro
  17. * @return MarketGoods
  18. * @throws Exception
  19. */
  20. public static function publish($templateId, $priceSingle, $priceBuyout, $title = '', $intro = '')
  21. {
  22. if (!FactoryContext::isOwner()) {
  23. throw new Exception('只有工厂管理员可以上架模板');
  24. }
  25. $factoryId = FactoryContext::requireId();
  26. $userId = FactoryContext::userId();
  27. $priceSingle = (int)$priceSingle;
  28. $priceBuyout = (int)$priceBuyout;
  29. if ($priceSingle < 0 || $priceBuyout < 0) {
  30. throw new Exception('积分不能为负数');
  31. }
  32. if ($priceSingle === 0 && $priceBuyout === 0) {
  33. throw new Exception('请设置单次或买断积分');
  34. }
  35. $tpl = (new Template())->find($templateId);
  36. if (!$tpl) {
  37. throw new Exception('模板不存在');
  38. }
  39. if ((int)$tpl['is_system'] === 1) {
  40. throw new Exception('官方模板不能上架');
  41. }
  42. if ($tpl['source'] === 'market' || strpos((string)$tpl['license_type'], 'market_') === 0) {
  43. throw new Exception('商城购入的模板不能再次上架');
  44. }
  45. if ((int)$tpl['current_version_id'] <= 0) {
  46. throw new Exception('请先完成识版并保存排版后再上架');
  47. }
  48. $title = mb_substr(trim($title !== '' ? $title : (string)$tpl['name']), 0, 100);
  49. if ($title === '') {
  50. $title = '流程单模板';
  51. }
  52. $intro = mb_substr(trim((string)$intro), 0, 500);
  53. $cover = trim((string)$tpl['cover']);
  54. $payload = [
  55. 'factory_id' => $factoryId,
  56. 'seller_user_id' => $userId,
  57. 'template_id' => (int)$tpl['id'],
  58. 'template_version_id' => (int)$tpl['current_version_id'],
  59. 'title' => $title,
  60. 'cover' => $cover,
  61. 'category' => '流程单模板',
  62. 'intro' => $intro,
  63. 'price_single' => $priceSingle,
  64. 'price_buyout' => $priceBuyout,
  65. 'status' => 'on_sale',
  66. ];
  67. $goods = (new MarketGoods())
  68. ->where('factory_id', $factoryId)
  69. ->where('template_id', (int)$tpl['id'])
  70. ->find();
  71. if ($goods) {
  72. $goods->allowField(true)->save($payload);
  73. return $goods;
  74. }
  75. $goods = new MarketGoods();
  76. $goods->allowField(true)->save($payload);
  77. return $goods;
  78. }
  79. /**
  80. * @param int $goodsId
  81. * @return MarketGoods
  82. * @throws Exception
  83. */
  84. public static function unpublish($goodsId)
  85. {
  86. if (!FactoryContext::isOwner()) {
  87. throw new Exception('只有工厂管理员可以下架模板');
  88. }
  89. $factoryId = FactoryContext::requireId();
  90. $goods = MarketGoods::get($goodsId);
  91. if (!$goods || (int)$goods['factory_id'] !== (int)$factoryId) {
  92. throw new Exception('商品不存在');
  93. }
  94. $goods->save(['status' => 'off']);
  95. return $goods;
  96. }
  97. /**
  98. * 删除模板时同步下架
  99. * @param int $templateId
  100. * @param int $factoryId
  101. */
  102. public static function offByTemplate($templateId, $factoryId)
  103. {
  104. (new MarketGoods())
  105. ->where('template_id', (int)$templateId)
  106. ->where('factory_id', (int)$factoryId)
  107. ->update(['status' => 'off', 'updatetime' => time()]);
  108. }
  109. }