StoreBargainServices.php 50 KB

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