StoreBargainServices.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. //用户访问事件
  479. event('user.userVisit', [$user['uid'], $id, 'bargain', $bargain['product_id'], 'view']);
  480. //浏览记录
  481. ProductLogJob::dispatch(['visit', ['uid' => $user['uid'], 'product_id' => $bargain['product_id']]]);
  482. return $data;
  483. }
  484. /**
  485. * 验证砍价是否能支付
  486. * @param int $bargainId
  487. * @param int $uid
  488. */
  489. public function checkBargainUser(int $bargainId, int $uid)
  490. {
  491. /** @var StoreBargainUserServices $bargainUserServices */
  492. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  493. $bargainUserInfo = $bargainUserServices->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  494. if (!$bargainUserInfo)
  495. throw new ApiException(410307);
  496. $bargainUserTableId = $bargainUserInfo['id'];
  497. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  498. throw new ApiException(410308);
  499. }
  500. if ($bargainUserInfo['status'] == 3)
  501. throw new ApiException(410309);
  502. /** @var StoreProductAttrValueServices $attrValueServices */
  503. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  504. $res = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  505. if (!$this->validBargain($bargainId) || !$res) {
  506. throw new ApiException(410295);
  507. }
  508. $StoreBargainInfo = $this->dao->get($bargainId);
  509. if (1 > $res['quota']) {
  510. throw new ApiException(410296);
  511. }
  512. $product_stock = $attrValueServices->value(['product_id' => $StoreBargainInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  513. if ($product_stock < 1) {
  514. throw new ApiException(410296);
  515. }
  516. //修改砍价状态
  517. $this->setBargainUserStatus($bargainId, $uid, $bargainUserTableId);
  518. return true;
  519. }
  520. /**
  521. * 修改砍价状态
  522. * @param int $bargainId
  523. * @param int $uid
  524. * @param int $bargainUserTableId
  525. * @return bool|\crmeb\basic\BaseModel
  526. */
  527. public function setBargainUserStatus(int $bargainId, int $uid, int $bargainUserTableId)
  528. {
  529. if (!$bargainId || !$uid) return false;
  530. if (!$bargainUserTableId) return false;
  531. /** @var StoreBargainUserServices $bargainUserServices */
  532. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  533. $count = $bargainUserServices->count(['id' => $bargainUserTableId, 'uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1]);
  534. if (!$count) return false;
  535. $userPrice = $bargainUserServices->value(['id' => $bargainUserTableId, 'uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1], 'price');
  536. $price = $bargainUserServices->get($bargainUserTableId, ['bargain_price', 'bargain_price_min']);
  537. $price = bcsub($price['bargain_price'], $price['bargain_price_min'], 2);
  538. if (bcsub($price, $userPrice, 2) > 0) {
  539. return false;
  540. }
  541. return $bargainUserServices->updateBargainStatus($bargainUserTableId);
  542. }
  543. /**
  544. * 发起砍价
  545. * @param int $uid
  546. * @param int $bargainId
  547. * @return string
  548. * @throws \think\db\exception\DataNotFoundException
  549. * @throws \think\db\exception\DbException
  550. * @throws \think\db\exception\ModelNotFoundException
  551. */
  552. public function setBargain(int $uid, int $bargainId)
  553. {
  554. if (!$bargainId) throw new ApiException(100101);
  555. $bargainInfo = $this->dao->getOne([
  556. ['is_del', '=', 0],
  557. ['status', '=', 1],
  558. ['start_time', '<', time()],
  559. ['stop_time', '>', time()],
  560. ['id', '=', $bargainId],
  561. ]);
  562. if (!$bargainInfo) throw new ApiException(410299);
  563. $bargainInfo = $bargainInfo->toArray();
  564. /** @var StoreBargainUserServices $bargainUserService */
  565. $bargainUserService = app()->make(StoreBargainUserServices::class);
  566. $count = $bargainUserService->count(['bargain_id' => $bargainId, 'uid' => $uid, 'is_del' => 0, 'status' => 1]);
  567. if ($count === false) {
  568. throw new ApiException(100101);
  569. } else {
  570. /** @var StoreBargainUserHelpServices $bargainUserHelpService */
  571. $bargainUserHelpService = app()->make(StoreBargainUserHelpServices::class);
  572. $count = $bargainUserService->count(['uid' => $uid, 'bargain_id' => $bargainId, 'is_del' => 0]);
  573. if ($count >= $bargainInfo['num']) throw new ApiException(410300);
  574. return $this->transaction(function () use ($bargainUserService, $bargainUserHelpService, $bargainId, $uid, $bargainInfo) {
  575. $bargainUserInfo = $bargainUserService->setBargain($bargainId, $uid, $bargainInfo);
  576. $price = $bargainUserHelpService->setBargainRecord($uid, $bargainUserInfo->toArray(), $bargainInfo);
  577. return ['bargainUserInfo' => $bargainUserInfo, 'price' => $price];
  578. });
  579. }
  580. }
  581. /**
  582. * 参与砍价
  583. * @param int $uid
  584. * @param int $bargainId
  585. * @param int $bargainUserUid
  586. * @return array
  587. * @throws \think\db\exception\DataNotFoundException
  588. * @throws \think\db\exception\DbException
  589. * @throws \think\db\exception\ModelNotFoundException
  590. */
  591. public function setHelpBargain(int $uid, int $bargainId, int $bargainUserUid)
  592. {
  593. if (!$bargainId || !$bargainUserUid) throw new ApiException(100100);
  594. $bargainInfo = $this->dao->getOne([
  595. ['is_del', '=', 0],
  596. ['status', '=', 1],
  597. ['start_time', '<', time()],
  598. ['stop_time', '>', time()],
  599. ['id', '=', $bargainId],
  600. ]);
  601. if (!$bargainInfo) throw new ApiException(410299);
  602. $bargainInfo = $bargainInfo->toArray();
  603. /** @var StoreBargainUserHelpServices $userHelpService */
  604. $userHelpService = app()->make(StoreBargainUserHelpServices::class);
  605. /** @var StoreBargainUserServices $bargainUserService */
  606. $bargainUserService = app()->make(StoreBargainUserServices::class);
  607. $bargainUserTableId = $bargainUserService->getBargainUserTableId($bargainId, $bargainUserUid);
  608. if (!$bargainUserTableId) throw new ApiException(410301);
  609. $bargainUserInfo = $bargainUserService->get($bargainUserTableId)->toArray();
  610. $count = $userHelpService->isBargainUserHelpCount($bargainId, $bargainUserTableId, $uid);
  611. if (!$count) throw new ApiException(410302);
  612. $price = $userHelpService->setBargainRecord($uid, $bargainUserInfo, $bargainInfo);
  613. if ($price) {
  614. if (!$bargainUserService->getSurplusPrice($bargainUserTableId, 1)) {
  615. event('notice.notice', [['uid' => $bargainUserUid, 'bargainInfo' => $bargainInfo, 'bargainUserInfo' => $bargainUserInfo,], 'bargain_success']);
  616. }
  617. }
  618. return ['bargainUserInfo' => $bargainUserInfo, 'price' => $price];
  619. }
  620. /**
  621. * 减库存加销量
  622. * @param int $num
  623. * @param int $bargainId
  624. * @param string $unique
  625. * @return bool
  626. */
  627. public function decBargainStock(int $num, int $bargainId, string $unique)
  628. {
  629. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  630. if ($unique) {
  631. /** @var StoreProductAttrValueServices $skuValueServices */
  632. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  633. //减去砍价商品sku的库存增加销量
  634. $res = false !== $skuValueServices->decProductAttrStock($bargainId, $unique, $num, 2);
  635. //减去砍价商品的库存和销量
  636. $res = $res && $this->dao->decStockIncSales(['id' => $bargainId, 'type' => 2], $num);
  637. //减掉普通商品sku的库存加销量
  638. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId], 'suk');
  639. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  640. if ($productUnique) {
  641. $res = $res && $skuValueServices->decProductAttrStock($product_id, $productUnique, $num);
  642. }
  643. } else {
  644. //减去砍价商品的库存和销量
  645. $res = false !== $this->dao->decStockIncSales(['id' => $bargainId, 'type' => 2], $num);
  646. }
  647. /** @var StoreProductServices $services */
  648. $services = app()->make(StoreProductServices::class);
  649. //减掉普通商品的库存加销量
  650. $res = $res && $services->decProductStock($num, $product_id);
  651. return $res;
  652. }
  653. /**
  654. * 减销量加库存
  655. * @param int $num
  656. * @param int $bargainId
  657. * @param string $unique
  658. * @return bool
  659. */
  660. public function incBargainStock(int $num, int $bargainId, string $unique)
  661. {
  662. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  663. if ($unique) {
  664. /** @var StoreProductAttrValueServices $skuValueServices */
  665. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  666. //减去砍价商品sku的销量,增加库存和限购数量
  667. $res = false !== $skuValueServices->incProductAttrStock($bargainId, $unique, $num, 2);
  668. //减去砍价商品的销量,增加库存
  669. $res = $res && $this->dao->incStockDecSales(['id' => $bargainId, 'type' => 2], $num);
  670. //减掉普通商品sku的销量,增加库存
  671. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId], 'suk');
  672. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  673. if ($productUnique) {
  674. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  675. }
  676. } else {
  677. //减去砍价商品的销量,增加库存
  678. $res = false !== $this->dao->incStockDecSales(['id' => $bargainId, 'type' => 2], $num);
  679. }
  680. /** @var StoreProductServices $services */
  681. $services = app()->make(StoreProductServices::class);
  682. //减掉普通商品的库存加销量
  683. $res = $res && $services->incProductStock($num, $product_id);
  684. return $res;
  685. }
  686. /**
  687. * 砍价分享
  688. * @param $bargainId
  689. * @param $user
  690. * @return bool|string
  691. * @throws \think\db\exception\DataNotFoundException
  692. * @throws \think\db\exception\DbException
  693. * @throws \think\db\exception\ModelNotFoundException
  694. */
  695. public function poster($bargainId, $user, $from)
  696. {
  697. $storeBargainInfo = $this->dao->get($bargainId, ['title', 'image', 'price']);
  698. if (!$storeBargainInfo) {
  699. throw new ApiException(410303);
  700. }
  701. /** @var StoreBargainUserServices $services */
  702. $services = app()->make(StoreBargainUserServices::class);
  703. $bargainUser = $services->get(['bargain_id' => $bargainId, 'uid' => $user['uid']], ['price', 'bargain_price_min']);
  704. if (!$bargainUser) {
  705. throw new ApiException(410304);
  706. }
  707. try {
  708. $siteUrl = sys_config('site_url');
  709. $data['title'] = $storeBargainInfo['title'];
  710. $data['image'] = $storeBargainInfo['image'];
  711. $data['price'] = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  712. $data['label'] = '已砍至';
  713. $price = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  714. $data['msg'] = '还差' . (bcsub($price, $bargainUser['bargain_price_min'], 2)) . '元即可砍价成功';
  715. /** @var SystemAttachmentServices $systemAttachmentServices */
  716. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  717. if ($from == 'wechat') {
  718. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_wap.jpg';
  719. //公众号
  720. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  721. if (!$imageInfo) {
  722. $codeUrl = set_http_type($siteUrl . '/pages/activity/goods_bargain_details/index?id=' . $bargainId . '&bargain=' . $user['uid'] . '&spread=' . $user['uid'], 1);//二维码链接
  723. $imageInfo = PosterServices::getQRCodePath($codeUrl, $name);
  724. if (is_string($imageInfo)) {
  725. throw new ApiException(410167);
  726. }
  727. $systemAttachmentServices->save([
  728. 'name' => $imageInfo['name'],
  729. 'att_dir' => $imageInfo['dir'],
  730. 'satt_dir' => $imageInfo['thumb_path'],
  731. 'att_size' => $imageInfo['size'],
  732. 'att_type' => $imageInfo['type'],
  733. 'image_type' => $imageInfo['image_type'],
  734. 'module_type' => 2,
  735. 'time' => $imageInfo['time'],
  736. 'pid' => 1,
  737. 'type' => 1
  738. ]);
  739. $url = $imageInfo['dir'];
  740. } else $url = $imageInfo['att_dir'];
  741. $data['url'] = $url;
  742. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  743. $posterImage = PosterServices::setShareMarketingPoster($data, 'wap/activity/bargain/poster');
  744. if (!is_array($posterImage)) {
  745. throw new ApiException(410172);
  746. }
  747. $systemAttachmentServices->save([
  748. 'name' => $posterImage['name'],
  749. 'att_dir' => $posterImage['dir'],
  750. 'satt_dir' => $posterImage['thumb_path'],
  751. 'att_size' => $posterImage['size'],
  752. 'att_type' => $posterImage['type'],
  753. 'image_type' => $posterImage['image_type'],
  754. 'module_type' => 2,
  755. 'time' => $posterImage['time'],
  756. 'pid' => 1,
  757. 'type' => 1
  758. ]);
  759. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  760. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  761. return $wapPosterImage;
  762. } else {
  763. //小程序
  764. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  765. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  766. if (!$imageInfo) {
  767. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  768. /** @var UserServices $userServices */
  769. $userServices = app()->make(UserServices::class);
  770. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  771. $valueData .= '&spread=' . $user['uid'];
  772. }
  773. $res = MiniProgramService::appCodeUnlimitService($valueData, 'pages/activity/goods_bargain_details/index', 280);
  774. if (!$res) throw new ApiException(400237);
  775. $uploadType = (int)sys_config('upload_type', 1);
  776. $upload = UploadService::init();
  777. $res = (string)EntityBody::factory($res);
  778. $res = $upload->to('routine/activity/bargain/code')->validate()->setAuthThumb(false)->stream($res, $name);
  779. if ($res === false) {
  780. throw new ApiException($upload->getError());
  781. }
  782. $imageInfo = $upload->getUploadInfo();
  783. $imageInfo['image_type'] = $uploadType;
  784. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  785. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  786. if (!$remoteImage['status']) throw new ApiException(410167);
  787. $systemAttachmentServices->save([
  788. 'name' => $imageInfo['name'],
  789. 'att_dir' => $imageInfo['dir'],
  790. 'satt_dir' => $imageInfo['thumb_path'],
  791. 'att_size' => $imageInfo['size'],
  792. 'att_type' => $imageInfo['type'],
  793. 'image_type' => $imageInfo['image_type'],
  794. 'module_type' => 2,
  795. 'time' => time(),
  796. 'pid' => 1,
  797. 'type' => 1
  798. ]);
  799. $url = $imageInfo['dir'];
  800. } else $url = $imageInfo['att_dir'];
  801. $data['url'] = $url;
  802. if ($imageInfo['image_type'] == 1)
  803. $data['url'] = $siteUrl . $url;
  804. $posterImage = PosterServices::setShareMarketingPoster($data, 'routine/activity/bargain/poster');
  805. if (!is_array($posterImage)) throw new ApiException(410172);
  806. $systemAttachmentServices->save([
  807. 'name' => $posterImage['name'],
  808. 'att_dir' => $posterImage['dir'],
  809. 'satt_dir' => $posterImage['thumb_path'],
  810. 'att_size' => $posterImage['size'],
  811. 'att_type' => $posterImage['type'],
  812. 'image_type' => $posterImage['image_type'],
  813. 'module_type' => 2,
  814. 'time' => $posterImage['time'],
  815. 'pid' => 1,
  816. 'type' => 1
  817. ]);
  818. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  819. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  820. return $routinePosterImage;
  821. }
  822. } catch (\Exception $e) {
  823. return false;
  824. }
  825. }
  826. /**
  827. * 获取砍价海报信息
  828. * @param int $bargainId
  829. * @param $user
  830. * @throws \think\db\exception\DataNotFoundException
  831. * @throws \think\db\exception\DbException
  832. * @throws \think\db\exception\ModelNotFoundException
  833. */
  834. public function posterInfo(int $bargainId, $user)
  835. {
  836. $storeBargainInfo = $this->dao->get($bargainId, ['title', 'image', 'price']);
  837. if (!$storeBargainInfo) {
  838. throw new ApiException(410303);
  839. }
  840. /** @var StoreBargainUserServices $services */
  841. $services = app()->make(StoreBargainUserServices::class);
  842. $bargainUser = $services->get(['bargain_id' => $bargainId, 'uid' => $user['uid']], ['price', 'bargain_price_min']);
  843. if (!$bargainUser) {
  844. throw new ApiException(410304);
  845. }
  846. $data['url'] = '';
  847. $data['title'] = $storeBargainInfo['title'];
  848. $data['image'] = $storeBargainInfo['image'];
  849. $data['price'] = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  850. $data['label'] = '已砍至';
  851. $price = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  852. $data['msg'] = '还差' . (bcsub($price, $bargainUser['bargain_price_min'], 2)) . '元即可砍价成功';
  853. //只有在小程序端,才会生成二维码
  854. if (\request()->isRoutine()) {
  855. try {
  856. /** @var SystemAttachmentServices $systemAttachmentServices */
  857. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  858. //小程序
  859. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  860. $siteUrl = sys_config('site_url');
  861. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  862. if (!$imageInfo) {
  863. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  864. /** @var UserServices $userServices */
  865. $userServices = app()->make(UserServices::class);
  866. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  867. $valueData .= '&spread=' . $user['uid'];
  868. }
  869. $res = MiniProgramService::appCodeUnlimitService($valueData, 'pages/activity/goods_bargain_details/index', 280);
  870. if (!$res) throw new ApiException(410167);
  871. $uploadType = (int)sys_config('upload_type', 1);
  872. $upload = UploadService::init();
  873. $res = (string)EntityBody::factory($res);
  874. $res = $upload->to('routine/activity/bargain/code')->validate()->setAuthThumb(false)->stream($res, $name);
  875. if ($res === false) {
  876. throw new ApiException($upload->getError());
  877. }
  878. $imageInfo = $upload->getUploadInfo();
  879. $imageInfo['image_type'] = $uploadType;
  880. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  881. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  882. if (!$remoteImage['status']) throw new ApiException($remoteImage['msg']);
  883. $systemAttachmentServices->save([
  884. 'name' => $imageInfo['name'],
  885. 'att_dir' => $imageInfo['dir'],
  886. 'satt_dir' => $imageInfo['thumb_path'],
  887. 'att_size' => $imageInfo['size'],
  888. 'att_type' => $imageInfo['type'],
  889. 'image_type' => $imageInfo['image_type'],
  890. 'module_type' => 2,
  891. 'time' => time(),
  892. 'pid' => 1,
  893. 'type' => 1
  894. ]);
  895. $url = $imageInfo['dir'];
  896. } else $url = $imageInfo['att_dir'];
  897. if ($imageInfo['image_type'] == 1) {
  898. $data['url'] = $siteUrl . $url;
  899. } else {
  900. $data['url'] = $url;
  901. }
  902. } catch (\Throwable $e) {
  903. }
  904. }
  905. return $data;
  906. }
  907. /**
  908. * 验证砍价下单库存限量
  909. * @param int $uid
  910. * @param int $bargainId
  911. * @param int $cartNum
  912. * @param string $unique
  913. * @return array
  914. * @throws \think\db\exception\DataNotFoundException
  915. * @throws \think\db\exception\DbException
  916. * @throws \think\db\exception\ModelNotFoundException
  917. */
  918. public function checkBargainStock(int $uid, int $bargainId, int $cartNum = 1, string $unique = '')
  919. {
  920. if (!$this->validBargain($bargainId)) {
  921. throw new ApiException(410295);
  922. }
  923. /** @var StoreProductAttrValueServices $attrValueServices */
  924. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  925. $attrInfo = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  926. if (!$attrInfo || $attrInfo['product_id'] != $bargainId) {
  927. throw new ApiException(410305);
  928. }
  929. $productInfo = $this->dao->get($bargainId, ['*', 'title as store_name']);
  930. /** @var StoreBargainUserServices $bargainUserService */
  931. $bargainUserService = app()->make(StoreBargainUserServices::class);
  932. $bargainUserInfo = $bargainUserService->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  933. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  934. throw new ApiException(413103);
  935. }
  936. $unique = $attrInfo['unique'];
  937. if ($cartNum > $attrInfo['quota']) {
  938. throw new ApiException(410296);
  939. }
  940. return [$attrInfo, $unique, $productInfo, $bargainUserInfo];
  941. }
  942. /**
  943. * 砍价统计
  944. * @param $id
  945. * @return array
  946. */
  947. public function bargainStatistics($id)
  948. {
  949. /** @var StoreBargainUserServices $bargainUser */
  950. $bargainUser = app()->make(StoreBargainUserServices::class);
  951. /** @var StoreBargainUserHelpServices $bargainUserHelp */
  952. $bargainUserHelp = app()->make(StoreBargainUserHelpServices::class);
  953. /** @var StoreOrderServices $orderServices */
  954. $orderServices = app()->make(StoreOrderServices::class);
  955. $people_count = $bargainUserHelp->count(['bargain_id' => $id]);
  956. $spread_count = $bargainUserHelp->count(['bargain_id' => $id, 'type' => 0]);
  957. $start_count = $bargainUser->count(['bargain_id' => $id]);
  958. $success_count = $bargainUser->count(['bargain_id' => $id, 'status' => 3]);
  959. $pay_price = $orderServices->sum(['bargain_id' => $id, 'paid' => 1], 'pay_price', true);
  960. $pay_count = $orderServices->count(['bargain_id' => $id, 'paid' => 1]);
  961. $pay_rate = $start_count > 0 ? bcmul(bcdiv((string)$pay_count, (string)$start_count, 2), '100', 2) : 0;
  962. return compact('people_count', 'spread_count', 'start_count', 'success_count', 'pay_price', 'pay_count', 'pay_rate');
  963. }
  964. /**
  965. * 砍价列表
  966. * @param $id
  967. * @param array $where
  968. * @return array
  969. */
  970. public function bargainStatisticsList($id, $where = [])
  971. {
  972. /** @var StoreBargainUserServices $bargainUser */
  973. $bargainUser = app()->make(StoreBargainUserServices::class);
  974. $where['bargain_id'] = $id;
  975. return $bargainUser->bargainUserList($where);
  976. }
  977. /**
  978. * 砍价订单
  979. * @param $id
  980. * @param array $where
  981. * @return array
  982. */
  983. public function bargainStatisticsOrder($id, $where = [])
  984. {
  985. /** @var StoreOrderServices $orderServices */
  986. $orderServices = app()->make(StoreOrderServices::class);
  987. [$page, $limit] = $this->getPageValue();
  988. $list = $orderServices->bargainStatisticsOrder($id, $where, $page, $limit);
  989. $where['bargain_id'] = $id;
  990. $count = $orderServices->count($where);
  991. foreach ($list as &$item) {
  992. if ($item['status'] == 0) {
  993. if ($item['paid'] == 0) {
  994. $item['status'] = '未支付';
  995. } else {
  996. $item['status'] = '未发货';
  997. }
  998. } elseif ($item['status'] == 1) {
  999. $item['status'] = '待收货';
  1000. } elseif ($item['status'] == 2) {
  1001. $item['status'] = '待评价';
  1002. } elseif ($item['status'] == 3) {
  1003. $item['status'] = '已完成';
  1004. } elseif ($item['status'] == -2) {
  1005. $item['status'] = '已退款';
  1006. } else {
  1007. $item['status'] = '未知';
  1008. }
  1009. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  1010. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  1011. }
  1012. return compact('list', 'count');
  1013. }
  1014. }