StoreBargainServices.php 51 KB

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