| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\common\library\ps;
- use app\common\model\ps\Factory;
- use app\common\model\ps\MarketGoods;
- use app\common\model\ps\MarketLicense;
- use app\common\model\ps\MarketOrder;
- use app\common\model\ps\Template;
- use app\common\model\ps\TemplateVersion;
- use fast\Random;
- use think\Config;
- use think\Db;
- use think\Exception;
- /**
- * 模板商城购买:积分扣减(本期不接支付渠道),导入本厂私有库。
- * 单次:确认套打后锁定。买断:可反复套打。副本禁止再上架。
- */
- class MarketBuyer
- {
- /**
- * @param int $goodsId
- * @param string $licenseType single|buyout
- * @param int $userId
- * @return array
- * @throws Exception
- */
- public static function buy($goodsId, $licenseType, $userId)
- {
- $licenseType = $licenseType === 'buyout' ? 'buyout' : 'single';
- $factoryId = FactoryContext::requireId();
- $goods = MarketGoods::get($goodsId);
- if (!$goods || $goods['status'] !== 'on_sale') {
- throw new Exception('商品不存在或已下架');
- }
- if ((int)$goods['factory_id'] === (int)$factoryId) {
- throw new Exception('不能购买本厂上架的模板');
- }
- $price = $licenseType === 'buyout' ? (int)$goods['price_buyout'] : (int)$goods['price_single'];
- $source = Db::name('ps_template')->where('id', (int)$goods['template_id'])->find();
- if (!$source) {
- throw new Exception('商品模板已失效');
- }
- if ($licenseType === 'buyout') {
- $dup = (new Template())->where('source_template_id', $source['id'])
- ->where('license_type', 'market_buyout')
- ->find();
- if ($dup) {
- throw new Exception('本厂已买断该模板,无需重复购买');
- }
- }
- $marketCfg = Config::get('ps.market') ?: [];
- $commission = isset($marketCfg['commission_percent']) ? (int)$marketCfg['commission_percent'] : 0;
- if ($commission < 0) {
- $commission = 0;
- }
- if ($commission > 100) {
- $commission = 100;
- }
- Db::startTrans();
- try {
- if ($price > 0) {
- Factory::changeScore($factoryId, -$price, 'market_buy', $goodsId, '购买模板:' . $goods['title'], $userId);
- $sellerId = (int)$goods['factory_id'];
- if ($sellerId > 0) {
- $sellerGain = (int)floor($price * (100 - $commission) / 100);
- if ($sellerGain > 0) {
- Factory::changeScore($sellerId, $sellerGain, 'market_sell', $goodsId, '模板售出:' . $goods['title'], 0);
- }
- }
- }
- $copy = self::importCopy($source, $goods, $licenseType, $userId, $factoryId);
- $order = new MarketOrder();
- $order->allowField(true)->save([
- 'order_no' => 'M' . date('YmdHis') . Random::numeric(4),
- 'goods_id' => $goods['id'],
- 'license_type' => $licenseType,
- 'score' => $price,
- 'buyer_factory_id' => $factoryId,
- 'buyer_user_id' => $userId,
- 'seller_factory_id' => (int)$goods['factory_id'],
- 'copy_template_id' => $copy['id'],
- 'status' => 'paid',
- ]);
- $license = new MarketLicense();
- $license->allowField(true)->save([
- 'order_id' => $order->id,
- 'template_id' => $copy['id'],
- 'license_type' => $licenseType,
- 'remain_times' => $licenseType === 'buyout' ? -1 : 1,
- 'locked' => 0,
- ]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- throw $e;
- }
- return [
- 'order_no' => $order['order_no'],
- 'template_id' => (int)$copy['id'],
- 'score' => $price,
- 'license' => $licenseType,
- ];
- }
- /**
- * @param array $source
- * @param MarketGoods $goods
- * @param string $licenseType
- * @param int $userId
- * @param int $factoryId
- * @return Template
- */
- protected static function importCopy(array $source, $goods, $licenseType, $userId, $factoryId)
- {
- $versionId = (int)$goods['template_version_id'] ?: (int)$source['current_version_id'];
- $schema = '';
- if ($versionId) {
- $ver = Db::name('ps_template_version')->where('id', $versionId)->find();
- $schema = $ver ? (string)$ver['schema_json'] : '';
- }
- $tpl = new Template();
- $tpl->allowField(true)->save([
- 'creator_user_id' => $userId,
- 'name' => $goods['title'] ?: $source['name'],
- 'cover' => $goods['cover'] ?: $source['cover'],
- 'source' => 'market',
- 'license_type' => $licenseType === 'buyout' ? 'market_buyout' : 'market_single',
- 'locked' => 0,
- 'source_template_id' => (int)$source['id'],
- 'source_version_id' => $versionId,
- 'is_system' => 0,
- 'status' => 'normal',
- ]);
- $verRow = new TemplateVersion();
- $verRow->allowField(true)->save([
- 'template_id' => $tpl->id,
- 'version' => 1,
- 'schema_json' => $schema,
- 'remark' => '商城导入',
- ]);
- $tpl->save(['current_version_id' => $verRow->id]);
- return $tpl;
- }
- }
|