StoreBargainServices.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\bargain;
  13. use app\dao\activity\bargain\StoreBargainDao;
  14. use app\jobs\ProductLogJob;
  15. use app\Request;
  16. use app\services\BaseServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\product\product\StoreCategoryServices;
  19. use app\services\product\product\StoreDescriptionServices;
  20. use app\services\product\product\StoreProductServices;
  21. use app\services\product\sku\StoreProductAttrResultServices;
  22. use app\services\product\sku\StoreProductAttrServices;
  23. use app\services\product\sku\StoreProductAttrValueServices;
  24. use app\services\system\attachment\SystemAttachmentServices;
  25. use app\services\user\UserServices;
  26. use app\services\wechat\WechatServices;
  27. use crmeb\exceptions\AdminException;
  28. use crmeb\services\CacheService;
  29. use crmeb\services\MiniProgramService;
  30. use crmeb\services\UploadService;
  31. use crmeb\services\UtilService;
  32. use Guzzle\Http\EntityBody;
  33. use think\exception\ValidateException;
  34. /**
  35. *
  36. * Class StoreBargainServices
  37. * @package app\services\activity
  38. * @method get(int $id, array $field) 获取一条数据
  39. * @method getBargainIdsArray(array $ids, array $field)
  40. * @method sum(array $where, string $field)
  41. * @method update(int $id, array $data)
  42. * @method addBargain(int $id, string $field)
  43. * @method value(array $where, string $field)
  44. * @method validWhere()
  45. * @method getList(array $where, int $page = 0, int $limit = 0) 获取砍价列表
  46. */
  47. class StoreBargainServices extends BaseServices
  48. {
  49. /**
  50. * StoreCombinationServices constructor.
  51. * @param StoreBargainDao $dao
  52. */
  53. public function __construct(StoreBargainDao $dao)
  54. {
  55. $this->dao = $dao;
  56. }
  57. /**
  58. * 判断砍价商品是否开启
  59. * @param int $bargainId
  60. * @return int|string
  61. */
  62. public function validBargain($bargainId = 0)
  63. {
  64. $where = [];
  65. $time = time();
  66. $where[] = ['is_del', '=', 0];
  67. $where[] = ['status', '=', 1];
  68. $where[] = ['start_time', '<', $time];
  69. $where[] = ['stop_time', '>', $time - 85400];
  70. if ($bargainId) $where[] = ['id', '=', $bargainId];
  71. return $this->dao->getCount($where);
  72. }
  73. /**
  74. * 获取后台列表
  75. * @param array $where
  76. * @return array
  77. */
  78. public function getStoreBargainList(array $where)
  79. {
  80. [$page, $limit] = $this->getPageValue();
  81. $list = $this->dao->getList($where, $page, $limit);
  82. $count = $this->dao->count($where);
  83. /** @var StoreBargainUserServices $storeBargainUserServices */
  84. $storeBargainUserServices = app()->make(StoreBargainUserServices::class);
  85. $ids = array_column($list, 'id');
  86. $countAll = $storeBargainUserServices->getAllCount([['bargain_id', 'in', $ids]]);
  87. $countSuccess = $storeBargainUserServices->getAllCount([
  88. ['status', '=', 3],
  89. ['bargain_id', 'in', $ids]
  90. ]);
  91. /** @var StoreBargainUserHelpServices $storeBargainUserHelpServices */
  92. $storeBargainUserHelpServices = app()->make(StoreBargainUserHelpServices::class);
  93. $countHelpAll = $storeBargainUserHelpServices->getHelpAllCount([['bargain_id', 'in', $ids]]);
  94. foreach ($list as &$item) {
  95. $item['count_people_all'] = $countAll[$item['id']] ?? 0;//参与人数
  96. $item['count_people_help'] = $countHelpAll[$item['id']] ?? 0;//帮忙砍价人数
  97. $item['count_people_success'] = $countSuccess[$item['id']] ?? 0;//砍价成功人数
  98. $item['stop_status'] = $item['stop_time'] < time() ? 1 : 0;
  99. if ($item['status']) {
  100. if ($item['start_time'] > time())
  101. $item['start_name'] = '未开始';
  102. else if ($item['stop_time'] < time())
  103. $item['start_name'] = '已结束';
  104. else if ($item['stop_time'] > time() && $item['start_time'] < time()) {
  105. $item['start_name'] = '进行中';
  106. }
  107. } else $item['start_name'] = '已结束';
  108. }
  109. return compact('list', 'count');
  110. }
  111. /**
  112. * 保存数据
  113. * @param int $id
  114. * @param array $data
  115. */
  116. public function saveData(int $id, array $data)
  117. {
  118. $description = $data['description'];
  119. $detail = $data['attrs'];
  120. $items = $data['items'];
  121. $data['start_time'] = strtotime($data['section_time'][0]);
  122. $data['stop_time'] = strtotime($data['section_time'][1]);
  123. $data['image'] = $data['images'][0];
  124. $data['images'] = json_encode($data['images']);
  125. $data['stock'] = $detail[0]['stock'];
  126. $data['quota'] = $detail[0]['quota'];
  127. $data['quota_show'] = $detail[0]['quota'];
  128. $data['price'] = $detail[0]['price'];
  129. $data['min_price'] = $detail[0]['min_price'];
  130. $data['logistics'] = implode(',', $data['logistics']);
  131. if ($detail[0]['min_price'] < 0 || $detail[0]['price'] <= 0 || $detail[0]['min_price'] === '' || $detail[0]['price'] === '') throw new ValidateException('金额不能小于0');
  132. if ($detail[0]['min_price'] >= $detail[0]['price']) throw new ValidateException('砍价最低价不能大于或等于起始金额');
  133. if ($detail[0]['quota'] > $detail[0]['stock']) throw new ValidateException('限量不能超过商品库存');
  134. //按照能砍掉的金额计算最大设置人数,并判断填写的砍价人数是否大于最大设置人数
  135. $bNum = bcmul(bcsub((string)$data['price'], (string)$data['min_price'], 2), '100');
  136. if ($data['people_num'] > $bNum) throw new ValidateException('砍价人数不能大于' . $bNum . '人');
  137. unset($data['section_time'], $data['description'], $data['attrs'], $data['items'], $detail[0]['min_price'], $detail[0]['_index'], $detail[0]['_rowKey']);
  138. /** @var StoreDescriptionServices $storeDescriptionServices */
  139. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  140. /** @var StoreProductAttrServices $storeProductAttrServices */
  141. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  142. /** @var StoreProductServices $storeProductServices */
  143. $storeProductServices = app()->make(StoreProductServices::class);
  144. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  145. if ($id) {
  146. $res = $this->dao->update($id, $data);
  147. $storeDescriptionServices->saveDescription((int)$id, $description, 2);
  148. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 2);
  149. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 2);
  150. if (!$res) throw new AdminException('修改失败');
  151. } else {
  152. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  153. throw new AdminException('原商品已下架或移入回收站');
  154. }
  155. $data['add_time'] = time();
  156. $res = $this->dao->save($data);
  157. $storeDescriptionServices->saveDescription((int)$res->id, $description, 2);
  158. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 2);
  159. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 2);
  160. if (!$res) throw new AdminException('添加失败');
  161. }
  162. $res = true;
  163. foreach ($valueGroup->toArray() as $item) {
  164. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 2);
  165. }
  166. if (!$res) {
  167. throw new AdminException('占用库存失败');
  168. }
  169. });
  170. }
  171. /**
  172. * 获取砍价详情
  173. * @param int $id
  174. * @return array|\think\Model|null
  175. */
  176. public function getInfo(int $id)
  177. {
  178. $info = $this->dao->get($id);
  179. if ($info) {
  180. if ($info['start_time'])
  181. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  182. if ($info['stop_time'])
  183. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  184. if (isset($start_time) && isset($stop_time))
  185. $info['section_time'] = [$start_time, $stop_time];
  186. else
  187. $info['section_time'] = [];
  188. unset($info['start_time'], $info['stop_time']);
  189. }
  190. $info['give_integral'] = intval($info['give_integral']);
  191. $info['price'] = floatval($info['price']);
  192. $info['postage'] = floatval($info['postage']);
  193. $info['cost'] = floatval($info['cost']);
  194. $info['bargain_max_price'] = floatval($info['bargain_max_price']);
  195. $info['bargain_min_price'] = floatval($info['bargain_min_price']);
  196. $info['min_price'] = floatval($info['min_price']);
  197. $info['weight'] = floatval($info['weight']);
  198. $info['volume'] = floatval($info['volume']);
  199. $info['logistics'] = explode(',', $info['logistics']);
  200. /** @var StoreDescriptionServices $storeDescriptionServices */
  201. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  202. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 2]);
  203. $info['attrs'] = $this->attrList($id, $info['product_id']);
  204. return $info;
  205. }
  206. /**
  207. * 获取规格
  208. * @param int $id
  209. * @param int $pid
  210. * @return mixed
  211. */
  212. public function attrList(int $id, int $pid)
  213. {
  214. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  215. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  216. $bargainResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 2], 'result');
  217. $items = json_decode($bargainResult, true)['attr'];
  218. $productAttr = $this->getattr($items, $pid, 0);
  219. $bargainAttr = $this->getattr($items, $id, 2);
  220. foreach ($productAttr as $pk => $pv) {
  221. foreach ($bargainAttr as &$sv) {
  222. if ($pv['detail'] == $sv['detail']) {
  223. $productAttr[$pk] = $sv;
  224. }
  225. }
  226. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  227. }
  228. $attrs['items'] = $items;
  229. $attrs['value'] = $productAttr;
  230. foreach ($items as $key => $item) {
  231. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  232. }
  233. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  234. $header[] = ['title' => '砍价起始金额', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  235. $header[] = ['title' => '砍价最低价', 'slot' => 'min_price', 'align' => 'center', 'minWidth' => 80];
  236. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  237. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  238. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  239. $header[] = ['title' => '限量', 'slot' => 'quota', 'align' => 'center', 'minWidth' => 80];
  240. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  241. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  242. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  243. $attrs['header'] = $header;
  244. return $attrs;
  245. }
  246. /**
  247. * 获取规格
  248. * @param $attr
  249. * @param $id
  250. * @param $type
  251. * @return array
  252. */
  253. public function getattr($attr, $id, $type)
  254. {
  255. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  256. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  257. $value = attr_format($attr)[1];
  258. $valueNew = [];
  259. $count = 0;
  260. if ($type == 2) {
  261. $min_price = $this->dao->value(['id' => $id], 'min_price');
  262. } else {
  263. $min_price = 0;
  264. }
  265. foreach ($value as $key => $item) {
  266. $detail = $item['detail'];
  267. // sort($item['detail'], SORT_STRING);
  268. $suk = implode(',', $item['detail']);
  269. $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  270. if (count($sukValue)) {
  271. foreach (array_values($detail) as $k => $v) {
  272. $valueNew[$count]['value' . ($k + 1)] = $v;
  273. }
  274. $valueNew[$count]['detail'] = json_encode($detail);
  275. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  276. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  277. $valueNew[$count]['min_price'] = $min_price ? floatval($min_price) : 0;
  278. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  279. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  280. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  281. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  282. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  283. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  284. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  285. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  286. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  287. $valueNew[$count]['opt'] = $type != 0;
  288. $count++;
  289. }
  290. }
  291. return $valueNew;
  292. }
  293. // /**
  294. // * TODO 获取砍价表ID
  295. // * @param int $bargainId $bargainId 砍价商品
  296. // * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  297. // * @param int $status $status 砍价状态 1参与中 2 活动结束参与失败 3活动结束参与成功
  298. // * @return mixed
  299. // */
  300. // public function getBargainUserTableId($bargainId = 0, $bargainUserUid = 0)
  301. // {
  302. // return $this->dao->value(['bargain_id' => $bargainId, 'uid' => $bargainUserUid, 'is_del' => 0], 'id');
  303. // }
  304. // /**
  305. // * TODO 获取用户可以砍掉的价格
  306. // * @param $id $id 用户参与砍价表编号
  307. // * @return float
  308. // * @throws \think\db\exception\DataNotFoundException
  309. // * @throws \think\db\exception\ModelNotFoundException
  310. // * @throws \think\exception\DbException
  311. // */
  312. // public function getBargainUserDiffPriceFloat($id)
  313. // {
  314. // $price = $this->dao->get($id, ['bargain_price,bargain_price_min']);
  315. // return (float)bcsub($price['bargain_price'], $price['bargain_price_min'], 2);
  316. // }
  317. // /**
  318. // * TODO 获取用户砍掉的价格
  319. // * @param int $id $id 用户参与砍价表编号
  320. // * @return float
  321. // */
  322. // public function getBargainUserPrice($id = 0)
  323. // {
  324. // return (float)$this->dao->value(['id' => $id], 'price');
  325. // }
  326. // /**
  327. // * 获取一条砍价商品
  328. // * @param int $bargainId
  329. // * @param string $field
  330. // * @return array
  331. // */
  332. // public function getBargainOne($bargainId = 0, $field = 'id,product_id,title,price,min_price,image')
  333. // {
  334. // if (!$bargainId) return [];
  335. // $bargain = $this->dao->getOne(['id' => $bargainId], $field);
  336. // if ($bargain) return $bargain->toArray();
  337. // else return [];
  338. // }
  339. /**
  340. * 砍价列表
  341. * @return array
  342. */
  343. public function getBargainList()
  344. {
  345. /** @var StoreBargainUserServices $bargainUserService */
  346. $bargainUserService = app()->make(StoreBargainUserServices::class);
  347. [$page, $limit] = $this->getPageValue();
  348. $field = 'id,product_id,title,min_price,image,price';
  349. $list = $this->dao->BargainList($page, $limit, $field);
  350. foreach ($list as &$item) {
  351. $item['people'] = $bargainUserService->getUserIdList($item['id']);
  352. $item['price'] = floatval($item['price']);
  353. }
  354. return $list;
  355. }
  356. /**
  357. * 后台页面设计获取砍价列表
  358. * @param $where
  359. * @return array
  360. * @throws \think\db\exception\DataNotFoundException
  361. * @throws \think\db\exception\DbException
  362. * @throws \think\db\exception\ModelNotFoundException
  363. */
  364. public function getDiyBargainList($where)
  365. {
  366. $where['status'] = 1;
  367. unset($where['is_show']);
  368. [$page, $limit] = $this->getPageValue();
  369. $list = $this->dao->DiyBargainList($where, $page, $limit);
  370. $count = $this->dao->getCount($where);
  371. $cateIds = implode(',', array_column($list, 'cate_id'));
  372. /** @var StoreCategoryServices $storeCategoryServices */
  373. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  374. $cateList = $storeCategoryServices->getCateArray($cateIds);
  375. foreach ($list as &$item) {
  376. $cateName = array_filter($cateList, function ($val) use ($item) {
  377. if (in_array($val['id'], explode(',', $item['cate_id']))) {
  378. return $val;
  379. }
  380. });
  381. $item['cate_name'] = implode(',', array_column($cateName, 'cate_name'));
  382. $item['store_name'] = $item['title'];
  383. $item['price'] = floatval($item['price']);
  384. $item['is_product_type'] = 1;
  385. }
  386. return compact('count', 'list');
  387. }
  388. /**
  389. * 首页砍价商品
  390. * @param $where
  391. * @return array
  392. */
  393. public function getHomeList($where)
  394. {
  395. [$page, $limit] = $this->getPageValue();
  396. $where['is_del'] = 0;
  397. $where['is_show'] = 1;
  398. $data = [];
  399. $list = $this->dao->getHomeList($where, $page, $limit);
  400. foreach ($list as &$item) {
  401. $item['price'] = floatval($item['price']);
  402. }
  403. $data['list'] = $list;
  404. return $data;
  405. }
  406. /**
  407. * 前端获取砍价详情
  408. * @param Request $request
  409. * @param int $id
  410. * @param int $bargainUid
  411. * @return array
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\DbException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function getBargain(Request $request, int $id, int $bargainUid)
  417. {
  418. /** @var StoreProductAttrServices $storeProductAttrServices */
  419. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  420. /** @var StoreOrderServices $orderService */
  421. $orderService = app()->make(StoreOrderServices::class);
  422. /** @var StoreBargainUserServices $bargainUserService */
  423. $bargainUserService = app()->make(StoreBargainUserServices::class);
  424. //获取砍价商品信息
  425. $bargain = $this->dao->getOne(['id' => $id], '*', ['description']);
  426. if (!$bargain) throw new ValidateException('砍价商品不存在');
  427. if ($bargain['stop_time'] < time()) throw new ValidateException('砍价已结束');
  428. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $request->uid(), 0, 2, $bargain['product_id']);
  429. foreach ($productValue as $v) {
  430. $bargain['attr'] = $v;
  431. }
  432. $bargain['time'] = time();
  433. $bargain = get_thumb_water($bargain);
  434. $bargain['small_image'] = $bargain['image'];
  435. $data['bargain'] = $bargain;
  436. //写入查看和分享数据
  437. $this->dao->addBargain($id, 'look');
  438. //用户数据
  439. $user = $request->user();
  440. $data['userInfo']['uid'] = $user['uid'];
  441. $data['userInfo']['nickname'] = $user['nickname'];
  442. $data['userInfo']['avatar'] = $user['avatar'];
  443. //砍价数据
  444. $userBargainInfo = $bargainUserService->helpCount($request, $id, $bargainUid);
  445. //用户已经生成砍价订单的总数
  446. $userBargainInfo['bargainOrderCount'] = $orderService->count(['bargain_id' => $id, 'uid' => $user['uid']]);
  447. //用户砍价的总数
  448. $userBargainInfo['bargainCount'] = $bargainUserService->count(['bargain_id' => $id, 'uid' => $user['uid'], 'is_del' => 0]);
  449. //判断砍价状态
  450. if (($userBargainInfo['bargainCount'] == 0 || $userBargainInfo['bargainCount'] == $userBargainInfo['bargainOrderCount']) //没有发起过砍价或者发起的砍价数量等于对应砍价商品的订单数量
  451. && $bargain['people_num'] > $userBargainInfo['bargainCount'] //商品的可发起砍价数量大于已经发起过的砍价数量
  452. && $userBargainInfo['price'] > 0 //剩余金额大于0
  453. && $request->uid() == $bargainUid) { //是自己砍价
  454. $userBargainInfo['bargainType'] = 1; //用户发起砍价
  455. } elseif ($userBargainInfo['bargainCount'] > $userBargainInfo['bargainOrderCount'] //发起的砍价数量大于生成的订单数量
  456. && $userBargainInfo['price'] > 0 //剩余金额大于0
  457. && $request->uid() == $bargainUid) { //是自己砍价
  458. $userBargainInfo['bargainType'] = 2; //发送给好友邀请砍价
  459. } elseif ($userBargainInfo['userBargainStatus'] //用户可以砍价
  460. && $userBargainInfo['price'] > 0 //剩余金额大于0
  461. && $request->uid() != $bargainUid) { //不是自己的砍价
  462. $userBargainInfo['bargainType'] = 3; //帮朋友砍价
  463. } elseif ($userBargainInfo['userBargainStatus'] //用户可以砍价
  464. && $userBargainInfo['price'] == 0 //剩余金额大于0
  465. && $request->uid() != $bargainUid) { //不是自己的砍价
  466. $userBargainInfo['bargainType'] = 4; //好友已经完成
  467. } elseif (!$userBargainInfo['userBargainStatus'] //用户不可以砍价
  468. && $request->uid() != $bargainUid) { //不是自己的砍价
  469. $userBargainInfo['bargainType'] = 5; //已经帮好友砍价
  470. } elseif ($userBargainInfo['price'] == 0 //剩余金额等于0
  471. && $request->uid() == $bargainUid //是自己砍价
  472. && $userBargainInfo['status'] != 3) { //未生成订单
  473. $userBargainInfo['bargainType'] = 6; //立即支付
  474. }
  475. $data['userBargainInfo'] = $userBargainInfo;
  476. //用户访问事件
  477. event('user.userVisit', [$user['uid'], $id, 'bargain', $bargain['product_id'], 'view']);
  478. //浏览记录
  479. ProductLogJob::dispatch(['visit', ['uid' => $user['uid'], 'product_id' => $bargain['product_id']]]);
  480. return $data;
  481. }
  482. /**
  483. * 验证砍价是否能支付
  484. * @param int $bargainId
  485. * @param int $uid
  486. */
  487. public function checkBargainUser(int $bargainId, int $uid)
  488. {
  489. /** @var StoreBargainUserServices $bargainUserServices */
  490. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  491. $bargainUserInfo = $bargainUserServices->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  492. if (!$bargainUserInfo)
  493. throw new ValidateException('砍价失败');
  494. $bargainUserTableId = $bargainUserInfo['id'];
  495. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  496. throw new ValidateException('砍价未成功');
  497. }
  498. if ($bargainUserInfo['status'] == 3)
  499. throw new ValidateException('砍价已支付');
  500. /** @var StoreBargainServices $bargainService */
  501. $bargainService = app()->make(StoreBargainServices::class);
  502. /** @var StoreProductAttrValueServices $attrValueServices */
  503. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  504. $res = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  505. if (!$bargainService->validBargain($bargainId) || !$res) {
  506. throw new ValidateException('该商品已下架或删除');
  507. }
  508. $StoreBargainInfo = $bargainService->get($bargainId);
  509. if (1 > $res['quota']) {
  510. throw new ValidateException('该商品库存不足');
  511. }
  512. $product_stock = $attrValueServices->value(['product_id' => $StoreBargainInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  513. if ($product_stock < 1) {
  514. throw new ValidateException('该商品库存不足');
  515. }
  516. //修改砍价状态
  517. $this->setBargainUserStatus($bargainId, $uid, $bargainUserTableId);
  518. return true;
  519. }
  520. /**
  521. * 修改砍价状态
  522. * @param int $bargainId
  523. * @param int $uid
  524. * @param int $bargainUserTableId
  525. * @return bool|\crmeb\basic\BaseModel
  526. */
  527. public function setBargainUserStatus(int $bargainId, int $uid, int $bargainUserTableId)
  528. {
  529. if (!$bargainId || !$uid) return false;
  530. if (!$bargainUserTableId) return false;
  531. /** @var StoreBargainUserServices $bargainUserServices */
  532. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  533. $count = $bargainUserServices->count(['id' => $bargainUserTableId, 'uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1]);
  534. if (!$count) return false;
  535. $userPrice = $bargainUserServices->value(['id' => $bargainUserTableId, 'uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1], 'price');
  536. $price = $bargainUserServices->get($bargainUserTableId, ['bargain_price', 'bargain_price_min']);
  537. $price = bcsub($price['bargain_price'], $price['bargain_price_min'], 2);
  538. if (bcsub($price, $userPrice, 2) > 0) {
  539. return false;
  540. }
  541. return $bargainUserServices->updateBargainStatus($bargainUserTableId);
  542. }
  543. /**
  544. * 发起砍价
  545. * @param int $uid
  546. * @param int $bargainId
  547. * @return string
  548. * @throws \think\db\exception\DataNotFoundException
  549. * @throws \think\db\exception\DbException
  550. * @throws \think\db\exception\ModelNotFoundException
  551. */
  552. public function setBargain(int $uid, int $bargainId)
  553. {
  554. if (!$bargainId) throw new ValidateException('参数错误');
  555. $bargainInfo = $this->dao->getOne([
  556. ['is_del', '=', 0],
  557. ['status', '=', 1],
  558. ['start_time', '<', time()],
  559. ['stop_time', '>', time()],
  560. ['id', '=', $bargainId],
  561. ]);
  562. if (!$bargainInfo) throw new ValidateException('砍价已结束');
  563. $bargainInfo = $bargainInfo->toArray();
  564. /** @var StoreBargainUserServices $bargainUserService */
  565. $bargainUserService = app()->make(StoreBargainUserServices::class);
  566. $count = $bargainUserService->count(['bargain_id' => $bargainId, 'uid' => $uid, 'is_del' => 0, 'status' => 1]);
  567. if ($count === false) {
  568. throw new ValidateException('参数错误');
  569. } else {
  570. /** @var StoreBargainUserHelpServices $bargainUserHelpService */
  571. $bargainUserHelpService = app()->make(StoreBargainUserHelpServices::class);
  572. $count = $bargainUserService->count(['uid' => $uid, 'bargain_id' => $bargainId, 'is_del' => 0]);
  573. if ($count >= $bargainInfo['num']) throw new ValidateException('您不能再发起此件商品砍价');
  574. return $this->transaction(function () use ($bargainUserService, $bargainUserHelpService, $bargainId, $uid, $bargainInfo) {
  575. $bargainUserInfo = $bargainUserService->setBargain($bargainId, $uid, $bargainInfo);
  576. $price = $bargainUserHelpService->setBargainRecord($uid, $bargainUserInfo->toArray(), $bargainInfo);
  577. return ['bargainUserInfo' => $bargainUserInfo, 'price' => $price];
  578. });
  579. }
  580. }
  581. /**
  582. * 参与砍价
  583. * @param int $uid
  584. * @param int $bargainId
  585. * @param int $bargainUserUid
  586. * @return array
  587. * @throws \think\db\exception\DataNotFoundException
  588. * @throws \think\db\exception\DbException
  589. * @throws \think\db\exception\ModelNotFoundException
  590. */
  591. public function setHelpBargain(int $uid, int $bargainId, int $bargainUserUid)
  592. {
  593. if (!$bargainId || !$bargainUserUid) throw new ValidateException('参数错误');
  594. $bargainInfo = $this->dao->getOne([
  595. ['is_del', '=', 0],
  596. ['status', '=', 1],
  597. ['start_time', '<', time()],
  598. ['stop_time', '>', time()],
  599. ['id', '=', $bargainId],
  600. ]);
  601. if (!$bargainInfo) throw new ValidateException('砍价已结束');
  602. $bargainInfo = $bargainInfo->toArray();
  603. /** @var StoreBargainUserHelpServices $userHelpService */
  604. $userHelpService = app()->make(StoreBargainUserHelpServices::class);
  605. /** @var StoreBargainUserServices $bargainUserService */
  606. $bargainUserService = app()->make(StoreBargainUserServices::class);
  607. $bargainUserTableId = $bargainUserService->getBargainUserTableId($bargainId, $bargainUserUid);
  608. if (!$bargainUserTableId) throw new ValidateException('该分享未开启砍价');
  609. $bargainUserInfo = $bargainUserService->get($bargainUserTableId)->toArray();
  610. $count = $userHelpService->isBargainUserHelpCount($bargainId, $bargainUserTableId, $uid);
  611. if (!$count) throw new ValidateException('您已经帮砍过此砍价');
  612. $price = $userHelpService->setBargainRecord($uid, $bargainUserInfo, $bargainInfo);
  613. if ($price) {
  614. if (!$bargainUserService->getSurplusPrice($bargainUserTableId, 1)) {
  615. event('notice.notice', [['uid' => $bargainUserUid, 'bargainInfo' => $bargainInfo, 'bargainUserInfo' => $bargainUserInfo,], 'bargain_success']);
  616. }
  617. }
  618. return ['bargainUserInfo' => $bargainUserInfo, 'price' => $price];
  619. }
  620. /**
  621. * 减库存加销量
  622. * @param int $num
  623. * @param int $bargainId
  624. * @param string $unique
  625. * @return bool
  626. */
  627. public function decBargainStock(int $num, int $bargainId, string $unique)
  628. {
  629. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  630. if ($unique) {
  631. /** @var StoreProductAttrValueServices $skuValueServices */
  632. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  633. //减去砍价商品sku的库存增加销量
  634. $res = false !== $skuValueServices->decProductAttrStock($bargainId, $unique, $num, 2);
  635. //减去砍价商品的库存和销量
  636. $res = $res && $this->dao->decStockIncSales(['id' => $bargainId, 'type' => 2], $num);
  637. //减掉普通商品sku的库存加销量
  638. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId], 'suk');
  639. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  640. if ($productUnique) {
  641. $res = $res && $skuValueServices->decProductAttrStock($product_id, $productUnique, $num);
  642. }
  643. } else {
  644. //减去砍价商品的库存和销量
  645. $res = false !== $this->dao->decStockIncSales(['id' => $bargainId, 'type' => 2], $num);
  646. }
  647. /** @var StoreProductServices $services */
  648. $services = app()->make(StoreProductServices::class);
  649. //减掉普通商品的库存加销量
  650. $res = $res && $services->decProductStock($num, $product_id);
  651. return $res;
  652. }
  653. /**
  654. * 减销量加库存
  655. * @param int $num
  656. * @param int $bargainId
  657. * @param string $unique
  658. * @return bool
  659. */
  660. public function incBargainStock(int $num, int $bargainId, string $unique)
  661. {
  662. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  663. if ($unique) {
  664. /** @var StoreProductAttrValueServices $skuValueServices */
  665. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  666. //减去砍价商品sku的销量,增加库存和限购数量
  667. $res = false !== $skuValueServices->incProductAttrStock($bargainId, $unique, $num, 2);
  668. //减去砍价商品的销量,增加库存
  669. $res = $res && $this->dao->incStockDecSales(['id' => $bargainId, 'type' => 2], $num);
  670. //减掉普通商品sku的销量,增加库存
  671. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId], 'suk');
  672. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  673. if ($productUnique) {
  674. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  675. }
  676. } else {
  677. //减去砍价商品的销量,增加库存
  678. $res = false !== $this->dao->incStockDecSales(['id' => $bargainId, 'type' => 2], $num);
  679. }
  680. /** @var StoreProductServices $services */
  681. $services = app()->make(StoreProductServices::class);
  682. //减掉普通商品的库存加销量
  683. $res = $res && $services->incProductStock($num, $product_id);
  684. return $res;
  685. }
  686. /**
  687. * 砍价分享
  688. * @param $bargainId
  689. * @param $user
  690. * @return bool|string
  691. * @throws \think\db\exception\DataNotFoundException
  692. * @throws \think\db\exception\DbException
  693. * @throws \think\db\exception\ModelNotFoundException
  694. */
  695. public function poster($bargainId, $user, $from)
  696. {
  697. $storeBargainInfo = $this->dao->get($bargainId, ['title', 'image', 'price']);
  698. if (!$storeBargainInfo) {
  699. throw new ValidateException('砍价信息没有查到');
  700. }
  701. /** @var StoreBargainUserServices $services */
  702. $services = app()->make(StoreBargainUserServices::class);
  703. $bargainUser = $services->get(['bargain_id' => $bargainId, 'uid' => $user['uid']], ['price', 'bargain_price_min']);
  704. if (!$bargainUser) {
  705. throw new ValidateException('用户砍价信息未查到');
  706. }
  707. try {
  708. $siteUrl = sys_config('site_url');
  709. $data['title'] = $storeBargainInfo['title'];
  710. $data['image'] = $storeBargainInfo['image'];
  711. $data['price'] = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  712. $data['label'] = '已砍至';
  713. $price = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  714. $data['msg'] = '还差' . (bcsub($price, $bargainUser['bargain_price_min'], 2)) . '元即可砍价成功';
  715. /** @var SystemAttachmentServices $systemAttachmentServices */
  716. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  717. if ($from == 'wechat') {
  718. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_wap.jpg';
  719. //公众号
  720. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  721. if (!$imageInfo) {
  722. $codeUrl = set_http_type($siteUrl . '/pages/activity/goods_bargain_details/index?id=' . $bargainId . '&bargain=' . $user['uid'] . '&spread=' . $user['uid'], 1);//二维码链接
  723. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  724. if (is_string($imageInfo)) {
  725. throw new ValidateException('二维码生成失败');
  726. }
  727. $systemAttachmentServices->save([
  728. 'name' => $imageInfo['name'],
  729. 'att_dir' => $imageInfo['dir'],
  730. 'satt_dir' => $imageInfo['thumb_path'],
  731. 'att_size' => $imageInfo['size'],
  732. 'att_type' => $imageInfo['type'],
  733. 'image_type' => $imageInfo['image_type'],
  734. 'module_type' => 2,
  735. 'time' => $imageInfo['time'],
  736. 'pid' => 1,
  737. 'type' => 1
  738. ]);
  739. $url = $imageInfo['dir'];
  740. } else $url = $imageInfo['att_dir'];
  741. $data['url'] = $url;
  742. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  743. $posterImage = UtilService::setShareMarketingPoster($data, 'wap/activity/bargain/poster');
  744. if (!is_array($posterImage)) {
  745. throw new ValidateException('海报生成失败');
  746. }
  747. $systemAttachmentServices->save([
  748. 'name' => $posterImage['name'],
  749. 'att_dir' => $posterImage['dir'],
  750. 'satt_dir' => $posterImage['thumb_path'],
  751. 'att_size' => $posterImage['size'],
  752. 'att_type' => $posterImage['type'],
  753. 'image_type' => $posterImage['image_type'],
  754. 'module_type' => 2,
  755. 'time' => $posterImage['time'],
  756. 'pid' => 1,
  757. 'type' => 1
  758. ]);
  759. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  760. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  761. return $wapPosterImage;
  762. } else {
  763. //小程序
  764. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  765. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  766. if (!$imageInfo) {
  767. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  768. /** @var UserServices $userServices */
  769. $userServices = app()->make(UserServices::class);
  770. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  771. $valueData .= '&spread=' . $user['uid'];
  772. }
  773. $res = MiniProgramService::qrcodeService()->appCodeUnlimit($valueData, 'pages/activity/goods_bargain_details/index', 280);
  774. if (!$res) throw new ValidateException('二维码生成失败');
  775. $uploadType = (int)sys_config('upload_type', 1);
  776. $upload = UploadService::init();
  777. $res = (string)EntityBody::factory($res);
  778. $res = $upload->to('routine/activity/bargain/code')->validate()->setAuthThumb(false)->stream($res, $name);
  779. if ($res === false) {
  780. throw new ValidateException($upload->getError());
  781. }
  782. $imageInfo = $upload->getUploadInfo();
  783. $imageInfo['image_type'] = $uploadType;
  784. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  785. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  786. if (!$remoteImage['status']) throw new ValidateException($remoteImage['msg']);
  787. $systemAttachmentServices->save([
  788. 'name' => $imageInfo['name'],
  789. 'att_dir' => $imageInfo['dir'],
  790. 'satt_dir' => $imageInfo['thumb_path'],
  791. 'att_size' => $imageInfo['size'],
  792. 'att_type' => $imageInfo['type'],
  793. 'image_type' => $imageInfo['image_type'],
  794. 'module_type' => 2,
  795. 'time' => time(),
  796. 'pid' => 1,
  797. 'type' => 1
  798. ]);
  799. $url = $imageInfo['dir'];
  800. } else $url = $imageInfo['att_dir'];
  801. $data['url'] = $url;
  802. if ($imageInfo['image_type'] == 1)
  803. $data['url'] = $siteUrl . $url;
  804. $posterImage = UtilService::setShareMarketingPoster($data, 'routine/activity/bargain/poster');
  805. if (!is_array($posterImage)) throw new ValidateException('海报生成失败');
  806. $systemAttachmentServices->save([
  807. 'name' => $posterImage['name'],
  808. 'att_dir' => $posterImage['dir'],
  809. 'satt_dir' => $posterImage['thumb_path'],
  810. 'att_size' => $posterImage['size'],
  811. 'att_type' => $posterImage['type'],
  812. 'image_type' => $posterImage['image_type'],
  813. 'module_type' => 2,
  814. 'time' => $posterImage['time'],
  815. 'pid' => 1,
  816. 'type' => 1
  817. ]);
  818. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  819. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  820. return $routinePosterImage;
  821. }
  822. } catch (\Exception $e) {
  823. return false;
  824. }
  825. }
  826. /**
  827. * 获取砍价海报信息
  828. * @param int $bargainId
  829. * @param $user
  830. * @throws \think\db\exception\DataNotFoundException
  831. * @throws \think\db\exception\DbException
  832. * @throws \think\db\exception\ModelNotFoundException
  833. */
  834. public function posterInfo(int $bargainId, $user)
  835. {
  836. $storeBargainInfo = $this->dao->get($bargainId, ['title', 'image', 'price']);
  837. if (!$storeBargainInfo) {
  838. throw new ValidateException('砍价信息没有查到');
  839. }
  840. /** @var StoreBargainUserServices $services */
  841. $services = app()->make(StoreBargainUserServices::class);
  842. $bargainUser = $services->get(['bargain_id' => $bargainId, 'uid' => $user['uid']], ['price', 'bargain_price_min']);
  843. if (!$bargainUser) {
  844. throw new ValidateException('用户砍价信息未查到');
  845. }
  846. $data['url'] = '';
  847. $data['title'] = $storeBargainInfo['title'];
  848. $data['image'] = $storeBargainInfo['image'];
  849. $data['price'] = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  850. $data['label'] = '已砍至';
  851. $price = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  852. $data['msg'] = '还差' . (bcsub($price, $bargainUser['bargain_price_min'], 2)) . '元即可砍价成功';
  853. //只有在小程序端,才会生成二维码
  854. if (\request()->isRoutine()) {
  855. try {
  856. /** @var SystemAttachmentServices $systemAttachmentServices */
  857. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  858. //小程序
  859. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  860. $siteUrl = sys_config('site_url');
  861. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  862. if (!$imageInfo) {
  863. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  864. /** @var UserServices $userServices */
  865. $userServices = app()->make(UserServices::class);
  866. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  867. $valueData .= '&spread=' . $user['uid'];
  868. }
  869. $res = MiniProgramService::qrcodeService()->appCodeUnlimit($valueData, 'pages/activity/goods_bargain_details/index', 280);
  870. if (!$res) throw new ValidateException('二维码生成失败');
  871. $uploadType = (int)sys_config('upload_type', 1);
  872. $upload = UploadService::init();
  873. $res = (string)EntityBody::factory($res);
  874. $res = $upload->to('routine/activity/bargain/code')->validate()->setAuthThumb(false)->stream($res, $name);
  875. if ($res === false) {
  876. throw new ValidateException($upload->getError());
  877. }
  878. $imageInfo = $upload->getUploadInfo();
  879. $imageInfo['image_type'] = $uploadType;
  880. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  881. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  882. if (!$remoteImage['status']) throw new ValidateException($remoteImage['msg']);
  883. $systemAttachmentServices->save([
  884. 'name' => $imageInfo['name'],
  885. 'att_dir' => $imageInfo['dir'],
  886. 'satt_dir' => $imageInfo['thumb_path'],
  887. 'att_size' => $imageInfo['size'],
  888. 'att_type' => $imageInfo['type'],
  889. 'image_type' => $imageInfo['image_type'],
  890. 'module_type' => 2,
  891. 'time' => time(),
  892. 'pid' => 1,
  893. 'type' => 1
  894. ]);
  895. $url = $imageInfo['dir'];
  896. } else $url = $imageInfo['att_dir'];
  897. if ($imageInfo['image_type'] == 1) {
  898. $data['url'] = $siteUrl . $url;
  899. } else {
  900. $data['url'] = $url;
  901. }
  902. } catch (\Throwable $e) {
  903. }
  904. }
  905. return $data;
  906. }
  907. /**
  908. * 验证砍价下单库存限量
  909. * @param int $uid
  910. * @param int $bargainId
  911. * @param int $cartNum
  912. * @param string $unique
  913. * @return array
  914. * @throws \think\db\exception\DataNotFoundException
  915. * @throws \think\db\exception\DbException
  916. * @throws \think\db\exception\ModelNotFoundException
  917. */
  918. public function checkBargainStock(int $uid, int $bargainId, int $cartNum = 1, string $unique = '')
  919. {
  920. if (!$this->validBargain($bargainId)) {
  921. throw new ValidateException('该商品已下架或删除');
  922. }
  923. /** @var StoreProductAttrValueServices $attrValueServices */
  924. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  925. $attrInfo = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  926. if (!$attrInfo || $attrInfo['product_id'] != $bargainId) {
  927. throw new ValidateException('请选择有效的商品属性');
  928. }
  929. $productInfo = $this->dao->get($bargainId, ['*', 'title as store_name']);
  930. /** @var StoreBargainUserServices $bargainUserService */
  931. $bargainUserService = app()->make(StoreBargainUserServices::class);
  932. $bargainUserInfo = $bargainUserService->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  933. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  934. throw new ValidateException('砍价未成功');
  935. }
  936. $unique = $attrInfo['unique'];
  937. if ($cartNum > $attrInfo['quota']) {
  938. throw new ValidateException('该商品库存不足' . $cartNum);
  939. }
  940. return [$attrInfo, $unique, $productInfo, $bargainUserInfo];
  941. }
  942. }