find($templateId); if (!$tpl) { throw new Exception('模板不存在'); } if ((int)$tpl['is_system'] === 1) { throw new Exception('官方模板不能上架'); } if ($tpl['source'] === 'market' || strpos((string)$tpl['license_type'], 'market_') === 0) { throw new Exception('商城购入的模板不能再次上架'); } if ((int)$tpl['current_version_id'] <= 0) { throw new Exception('请先完成识版并保存排版后再上架'); } $title = mb_substr(trim($title !== '' ? $title : (string)$tpl['name']), 0, 100); if ($title === '') { $title = '流程单模板'; } $intro = mb_substr(trim((string)$intro), 0, 500); $cover = trim((string)$tpl['cover']); $payload = [ 'factory_id' => $factoryId, 'seller_user_id' => $userId, 'template_id' => (int)$tpl['id'], 'template_version_id' => (int)$tpl['current_version_id'], 'title' => $title, 'cover' => $cover, 'category' => '流程单模板', 'intro' => $intro, 'price_single' => $priceSingle, 'price_buyout' => $priceBuyout, 'status' => 'on_sale', ]; $goods = (new MarketGoods()) ->where('factory_id', $factoryId) ->where('template_id', (int)$tpl['id']) ->find(); if ($goods) { $goods->allowField(true)->save($payload); return $goods; } $goods = new MarketGoods(); $goods->allowField(true)->save($payload); return $goods; } /** * @param int $goodsId * @return MarketGoods * @throws Exception */ public static function unpublish($goodsId) { if (!FactoryContext::isOwner()) { throw new Exception('只有工厂管理员可以下架模板'); } $factoryId = FactoryContext::requireId(); $goods = MarketGoods::get($goodsId); if (!$goods || (int)$goods['factory_id'] !== (int)$factoryId) { throw new Exception('商品不存在'); } $goods->save(['status' => 'off']); return $goods; } /** * 删除模板时同步下架 * @param int $templateId * @param int $factoryId */ public static function offByTemplate($templateId, $factoryId) { (new MarketGoods()) ->where('template_id', (int)$templateId) ->where('factory_id', (int)$factoryId) ->update(['status' => 'off', 'updatetime' => time()]); } }