StoreBargainServices.php 53 KB

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