| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace app\common\library\ps;
- use app\common\model\ps\MarketGoods;
- use app\common\model\ps\Template;
- use think\Exception;
- /**
- * 工厂把本厂识版模板上架到商城。
- */
- class MarketPublisher
- {
- /**
- * @param int $templateId
- * @param int $priceSingle
- * @param int $priceBuyout
- * @param string $title
- * @param string $intro
- * @return MarketGoods
- * @throws Exception
- */
- public static function publish($templateId, $priceSingle, $priceBuyout, $title = '', $intro = '')
- {
- if (!FactoryContext::isOwner()) {
- throw new Exception('只有工厂管理员可以上架模板');
- }
- $factoryId = FactoryContext::requireId();
- $userId = FactoryContext::userId();
- $priceSingle = (int)$priceSingle;
- $priceBuyout = (int)$priceBuyout;
- if ($priceSingle < 0 || $priceBuyout < 0) {
- throw new Exception('积分不能为负数');
- }
- if ($priceSingle === 0 && $priceBuyout === 0) {
- throw new Exception('请设置单次或买断积分');
- }
- $tpl = (new Template())->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()]);
- }
- }
|