StoreBargainServices.php 50 KB

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