StoreBargainServices.php 50 KB

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