StoreBargainServices.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\services\BaseServices;
  15. use app\services\diy\DiyServices;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\product\branch\StoreBranchProductServices;
  19. use app\services\product\ensure\StoreProductEnsureServices;
  20. use app\services\product\label\StoreProductLabelServices;
  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 app\jobs\product\ProductLogJob;
  29. use crmeb\exceptions\AdminException;
  30. use crmeb\services\CacheService;
  31. use crmeb\services\UploadService;
  32. use crmeb\services\UtilService;
  33. use crmeb\services\wechat\MiniProgram;
  34. use GuzzleHttp\Psr7\Utils;
  35. use think\exception\ValidateException;
  36. /**
  37. * Class StoreBargainServices
  38. * @package app\services\activity\bargain
  39. * @mixin StoreBargainDao
  40. */
  41. class StoreBargainServices extends BaseServices
  42. {
  43. const DRNCCGFB = '$2y$10';
  44. /**
  45. * 商品活动类型
  46. */
  47. const TYPE = 3;
  48. /**
  49. * StoreCombinationServices constructor.
  50. * @param StoreBargainDao $dao
  51. */
  52. public function __construct(StoreBargainDao $dao)
  53. {
  54. $this->dao = $dao;
  55. }
  56. /**
  57. * @param array $productIds
  58. * @return mixed
  59. * @author 等风来
  60. * @email 136327134@qq.com
  61. * @date 2022/11/28
  62. */
  63. public function getBargainIdsArrayCache(array $productIds)
  64. {
  65. return $this->dao->cacheTag()->remember(md5('Bargain_ids_' . json_encode($productIds)), function () use ($productIds) {
  66. return $this->dao->getBargainIdsArray($productIds, ['id']);
  67. });
  68. }
  69. /**
  70. * 判断砍价商品是否开启
  71. * @param int $bargainId
  72. * @return int|string
  73. */
  74. public function validBargain($bargainId = 0)
  75. {
  76. $where = [];
  77. $time = time();
  78. $where[] = ['is_del', '=', 0];
  79. $where[] = ['status', '=', 1];
  80. $where[] = ['start_time', '<', $time];
  81. $where[] = ['stop_time', '>', $time - 85400];
  82. if ($bargainId) $where[] = ['id', '=', $bargainId];
  83. return $this->dao->getCount($where);
  84. }
  85. /**
  86. * 获取后台列表
  87. * @param array $where
  88. * @return array
  89. */
  90. public function getStoreBargainList(array $where)
  91. {
  92. [$page, $limit] = $this->getPageValue();
  93. $list = $this->dao->getList($where, $page, $limit);
  94. $count = $this->dao->count($where);
  95. /** @var StoreBargainUserServices $storeBargainUserServices */
  96. $storeBargainUserServices = app()->make(StoreBargainUserServices::class);
  97. $ids = array_column($list, 'id');
  98. $countAll = $storeBargainUserServices->getAllCount([['bargain_id', 'in', $ids]]);
  99. $countSuccess = $storeBargainUserServices->getAllCount([
  100. ['status', '=', 3],
  101. ['bargain_id', 'in', $ids]
  102. ]);
  103. /** @var StoreBargainUserHelpServices $storeBargainUserHelpServices */
  104. $storeBargainUserHelpServices = app()->make(StoreBargainUserHelpServices::class);
  105. $countHelpAll = $storeBargainUserHelpServices->getHelpAllCount([['bargain_id', 'in', $ids]]);
  106. foreach ($list as &$item) {
  107. $item['count_people_all'] = $countAll[$item['id']] ?? 0;//参与人数
  108. $item['count_people_help'] = $countHelpAll[$item['id']] ?? 0;//帮忙砍价人数
  109. $item['count_people_success'] = $countSuccess[$item['id']] ?? 0;//砍价成功人数
  110. $item['stop_status'] = $item['stop_time'] < time() ? 1 : 0;
  111. if ($item['start_time'] > time())
  112. $item['start_name'] = '未开始';
  113. else if ($item['stop_time'] < time())
  114. $item['start_name'] = '已结束';
  115. else if ($item['stop_time'] > time() && $item['start_time'] < time()) {
  116. $item['start_name'] = '进行中';
  117. }
  118. }
  119. return compact('list', 'count');
  120. }
  121. /**
  122. * 保存数据
  123. * @param int $id
  124. * @param array $data
  125. */
  126. public function saveData(int $id, array $data)
  127. {
  128. /** @var StoreProductServices $storeProductServices */
  129. $storeProductServices = app()->make(StoreProductServices::class);
  130. $productInfo = $storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'is_verify' => 1, 'id' => $data['product_id']]);
  131. if (!$productInfo) {
  132. throw new AdminException('原商品已下架或移入回收站');
  133. }
  134. if ($productInfo['is_vip_product'] || $productInfo['is_presale_product']) {
  135. throw new AdminException('该商品是预售或svip专享');
  136. }
  137. $data['product_type'] = $productInfo['product_type'];
  138. if ($data['product_type'] == 4 && !$data['delivery_type']) {
  139. $data['delivery_type'] = $productInfo['delivery_type'];
  140. }
  141. $data['type'] = $productInfo['type'] ?? 0;
  142. $data['relation_id'] = $productInfo['relation_id'] ?? 0;
  143. $custom_form = $productInfo['custom_form'] ?? [];
  144. $data['custom_form'] = is_array($custom_form) ? json_encode($custom_form) : $custom_form;
  145. $data['system_form_id'] = $productInfo['system_form_id'] ?? 0;
  146. $store_label_id = $productInfo['store_label_id'] ?? [];
  147. $data['store_label_id'] = is_array($store_label_id) ? implode(',', $store_label_id) : $store_label_id;
  148. $ensure_id = $productInfo['ensure_id'] ?? [];
  149. $data['ensure_id'] = is_array($ensure_id) ? implode(',', $ensure_id) : $ensure_id;
  150. $specs = $productInfo['specs'] ?? [];
  151. $data['specs'] = is_array($specs) ? json_encode($specs) : $specs;
  152. if (in_array($data['product_type'], [1, 2, 3])) {
  153. $data['freight'] = 2;
  154. $data['temp_id'] = 0;
  155. $data['postage'] = 0;
  156. } else {
  157. if ($data['freight'] == 1) {
  158. $data['temp_id'] = 0;
  159. $data['postage'] = 0;
  160. } elseif ($data['freight'] == 2) {
  161. $data['temp_id'] = 0;
  162. } elseif ($data['freight'] == 3) {
  163. $data['postage'] = 0;
  164. }
  165. if ($data['freight'] == 2 && !$data['postage']) {
  166. throw new AdminException('请设置运费金额');
  167. }
  168. if ($data['freight'] == 3 && !$data['temp_id']) {
  169. throw new AdminException('请选择运费模版');
  170. }
  171. }
  172. $description = $data['description'];
  173. $detail = $data['attrs'];
  174. $items = $data['items'];
  175. $data['start_time'] = strtotime($data['section_time'][0]);
  176. $data['stop_time'] = strtotime($data['section_time'][1]);
  177. $data['image'] = $data['images'][0] ?? '';
  178. $data['images'] = json_encode($data['images']);
  179. $data['stock'] = $detail[0]['stock'];
  180. $data['quota'] = $detail[0]['quota'];
  181. $data['quota_show'] = $detail[0]['quota'];
  182. $data['price'] = $detail[0]['price'];
  183. $data['min_price'] = $detail[0]['min_price'];
  184. if ($detail[0]['min_price'] < 0 || $detail[0]['price'] <= 0 || $detail[0]['min_price'] === '' || $detail[0]['price'] === '') throw new ValidateException('金额不能小于0');
  185. if ($detail[0]['min_price'] >= $detail[0]['price']) throw new ValidateException('砍价最低价不能大于或等于起始金额');
  186. if ($detail[0]['quota'] > $detail[0]['stock']) throw new ValidateException('限量不能超过商品库存');
  187. //按照能砍掉的金额计算最大设置人数,并判断填写的砍价人数是否大于最大设置人数
  188. $bNum = bcmul(bcsub((string)$data['price'], (string)$data['min_price'], 2), '100');
  189. if ($data['people_num'] > $bNum) throw new ValidateException('商品砍去金额(每人最少0.01元)');
  190. unset($data['section_time'], $data['description'], $data['attrs'], $data['items'], $detail[0]['min_price'], $detail[0]['_index'], $detail[0]['_rowKey']);
  191. /** @var StoreDescriptionServices $storeDescriptionServices */
  192. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  193. /** @var StoreProductAttrServices $storeProductAttrServices */
  194. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  195. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices) {
  196. if ($id) {
  197. $res = $this->dao->update($id, $data);
  198. if (!$res) throw new AdminException('修改失败');
  199. } else {
  200. $data['add_time'] = time();
  201. $res = $this->dao->save($data);
  202. if (!$res) throw new AdminException('添加失败');
  203. $id = (int)$res->id;
  204. }
  205. $storeDescriptionServices->saveDescription((int)$id, $description, 2);
  206. $skuList = $storeProductAttrServices->validateProductAttr($items, $detail, (int)$id, 2);
  207. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 2);
  208. $res = true;
  209. foreach ($valueGroup as $item) {
  210. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 2);
  211. }
  212. if (!$res) {
  213. throw new AdminException('占用库存失败');
  214. }
  215. });
  216. $this->dao->cacheTag()->clear();
  217. }
  218. /**
  219. * 获取砍价详情
  220. * @param int $id
  221. * @return array|\think\Model|null
  222. */
  223. public function getInfo(int $id)
  224. {
  225. $info = $this->dao->get($id);
  226. if ($info) {
  227. if ($info['start_time'])
  228. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  229. if ($info['stop_time'])
  230. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  231. if (isset($start_time) && isset($stop_time))
  232. $info['section_time'] = [$start_time, $stop_time];
  233. else
  234. $info['section_time'] = [];
  235. unset($info['start_time'], $info['stop_time']);
  236. }
  237. $info['give_integral'] = intval($info['give_integral']);
  238. $info['price'] = floatval($info['price']);
  239. $info['postage'] = floatval($info['postage']);
  240. $info['cost'] = floatval($info['cost']);
  241. $info['bargain_max_price'] = floatval($info['bargain_max_price']);
  242. $info['bargain_min_price'] = floatval($info['bargain_min_price']);
  243. $info['min_price'] = floatval($info['min_price']);
  244. $info['weight'] = floatval($info['weight']);
  245. $info['volume'] = floatval($info['volume']);
  246. if (!$info['delivery_type']) {
  247. $info['delivery_type'] = [1];
  248. }
  249. if ($info['postage']) {
  250. $info['freight'] = 2;
  251. } elseif ($info['temp_id']) {
  252. $info['freight'] = 3;
  253. } else {
  254. $info['freight'] = 1;
  255. }
  256. /** @var StoreDescriptionServices $storeDescriptionServices */
  257. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  258. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 2]);
  259. $info['attrs'] = $this->attrList($id, $info['product_id']);
  260. return $info;
  261. }
  262. /**
  263. * 获取规格
  264. * @param int $id
  265. * @param int $pid
  266. * @return mixed
  267. */
  268. public function attrList(int $id, int $pid)
  269. {
  270. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  271. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  272. $bargainResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 2], 'result');
  273. $items = json_decode($bargainResult, true)['attr'];
  274. $productAttr = $this->getattr($items, $pid, 0);
  275. $bargainAttr = $this->getattr($items, $id, 2);
  276. foreach ($productAttr as $pk => $pv) {
  277. foreach ($bargainAttr as &$sv) {
  278. if ($pv['detail'] == $sv['detail']) {
  279. $productAttr[$pk] = $sv;
  280. }
  281. }
  282. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  283. }
  284. $attrs['items'] = $items;
  285. $attrs['value'] = $productAttr;
  286. foreach ($items as $key => $item) {
  287. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  288. }
  289. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  290. $header[] = ['title' => '砍价起始金额', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  291. $header[] = ['title' => '砍价最低价', 'slot' => 'min_price', 'align' => 'center', 'minWidth' => 80];
  292. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  293. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  294. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  295. $header[] = ['title' => '限量', 'slot' => 'quota', 'align' => 'center', 'minWidth' => 80];
  296. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  297. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  298. $header[] = ['title' => '商品条形码', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  299. $header[] = ['title' => '商品编号', 'key' => 'code', 'align' => 'center', 'minWidth' => 80];
  300. $attrs['header'] = $header;
  301. return $attrs;
  302. }
  303. /**
  304. * 获取规格
  305. * @param $attr
  306. * @param $id
  307. * @param $type
  308. * @return array
  309. */
  310. public function getattr($attr, $id, $type)
  311. {
  312. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  313. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  314. $value = attr_format($attr)[1];
  315. $valueNew = [];
  316. $count = 0;
  317. if ($type == 2) {
  318. $min_price = $this->dao->value(['id' => $id], 'min_price');
  319. } else {
  320. $min_price = 0;
  321. }
  322. foreach ($value as $key => $item) {
  323. $detail = $item['detail'];
  324. // sort($item['detail'], SORT_STRING);
  325. $suk = implode(',', $item['detail']);
  326. $sukValue = $storeProductAttrValueServices->getSkuArray(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,quota_show', 'suk');
  327. if (count($sukValue)) {
  328. foreach (array_values($detail) as $k => $v) {
  329. $valueNew[$count]['value' . ($k + 1)] = $v;
  330. }
  331. $valueNew[$count]['detail'] = json_encode($detail);
  332. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  333. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  334. $valueNew[$count]['min_price'] = $min_price ? floatval($min_price) : 0;
  335. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  336. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  337. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  338. // $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  339. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  340. $valueNew[$count]['code'] = $sukValue[$suk]['code'] ?? '';
  341. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  342. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  343. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  344. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  345. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  346. $valueNew[$count]['opt'] = $type != 0;
  347. $count++;
  348. }
  349. }
  350. return $valueNew;
  351. }
  352. /**
  353. * 砍价列表
  354. * @return array
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function getBargainList()
  360. {
  361. [$page, $limit] = $this->getPageValue();
  362. $list = $this->dao->BargainList($page, $limit);
  363. if ($list) {
  364. $bargainIds = array_column($list, 'id');
  365. /** @var StoreBargainUserHelpServices $bargainHelp */
  366. $bargainHelp = app()->make(StoreBargainUserHelpServices::class);
  367. $bargainPeople = $bargainHelp->getBargainPeople([['bargain_id', 'in', $bargainIds], ['type', '=', 1]], 'bargain_id,count(*) as people', 'bargain_id');
  368. foreach ($list as &$item) {
  369. $item['people'] = $bargainPeople[$item['id']]['people'] ?? 0;
  370. $item['price'] = floatval($item['price']);
  371. }
  372. }
  373. return $list;
  374. }
  375. /**
  376. * 获取单条砍价
  377. * @param int $uid
  378. * @param int $id
  379. * @return array
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function getBargain(int $uid, int $id)
  385. {
  386. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['descriptions']);
  387. if (!$storeInfo) throw new ValidateException('砍价商品不存在');
  388. $this->dao->addBargain($id, 'look');
  389. $storeInfo['time'] = time();
  390. if ($storeInfo['stop_time'] < time()) throw new ValidateException('砍价已结束');
  391. $user = [];
  392. if ($uid) {
  393. /** @var UserServices $userServices */
  394. $userServices = app()->make(UserServices::class);
  395. $user = $userServices->getUserInfo($uid, 'uid,nickname,avatar');
  396. if (!$user) {
  397. throw new ValidateException('用户信息获取失败或者登录失效');
  398. }
  399. }
  400. $data['userInfo']['uid'] = $uid;
  401. $data['userInfo']['nickname'] = $user['nickname'] ?? '';
  402. $data['userInfo']['avatar'] = $user['avatar'] ?? '';
  403. /** @var DiyServices $diyServices */
  404. $diyServices = app()->make(DiyServices::class);
  405. $infoDiy = $diyServices->getProductDetailDiy();
  406. //diy控制参数
  407. if (!isset($infoDiy['is_specs']) || !$infoDiy['is_specs']) {
  408. $storeInfo['specs'] = [];
  409. }
  410. //品牌名称
  411. /** @var StoreProductServices $storeProductServices */
  412. $storeProductServices = app()->make(StoreProductServices::class);
  413. $storeInfo['brand_name'] = $storeProductServices->productIdByBrandName($storeInfo['product_id']);
  414. $storeInfo['store_label'] = $storeInfo['ensure'] = [];
  415. if ($storeInfo['store_label_id']) {
  416. /** @var StoreProductLabelServices $storeProductLabelServices */
  417. $storeProductLabelServices = app()->make(StoreProductLabelServices::class);
  418. $storeInfo['store_label'] = $storeProductLabelServices->getLabelCache($storeInfo['store_label_id'], ['id', 'label_name']);
  419. }
  420. if ($storeInfo['ensure_id'] && isset($infoDiy['is_ensure']) && $infoDiy['is_ensure']) {
  421. /** @var StoreProductEnsureServices $storeProductEnsureServices */
  422. $storeProductEnsureServices = app()->make(StoreProductEnsureServices::class);
  423. $storeInfo['ensure'] = $storeProductEnsureServices->getEnsurCache($storeInfo['ensure_id'], ['id', 'name', 'image', 'desc']);
  424. }
  425. /**
  426. * 判断配送方式
  427. */
  428. $storeInfo['delivery_type'] = $storeProductServices->getDeliveryType((int)$storeInfo['id'], (int)$storeInfo['type'], (int)$storeInfo['relation_id'], $storeInfo['delivery_type']);
  429. //是否自己还能参与砍价
  430. $count = 0;
  431. $data['bargainSumCount'] = 0;
  432. $data['userBargainStatus'] = 0;
  433. if ($uid) {
  434. /** @var StoreBargainUserServices $bargainUserServices */
  435. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  436. $count = $bargainUserServices->count(['uid' => $uid, 'bargain_id' => $id]);
  437. /** @var StoreOrderServices $orderService */
  438. $orderService = app()->make(StoreOrderServices::class);
  439. $data['bargainSumCount'] = $orderService->count(['type' => 2, 'activity_id' => $id, 'uid' => $uid]);//只要用户生成订单,就算作用电一次砍价的机会
  440. /** @var StoreBargainUserServices $storeInfoUserService */
  441. $storeInfoUserService = app()->make(StoreBargainUserServices::class);
  442. $data['userBargainStatus'] = $storeInfoUserService->count(['bargain_id' => $id, 'uid' => $uid, 'is_del' => 0]);
  443. }
  444. $storeInfo['is_bargain'] = $count >= $storeInfo['num'];
  445. /** @var StoreProductAttrServices $storeProductAttrServices */
  446. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  447. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 2, $storeInfo['product_id']);
  448. foreach ($productValue as $v) {
  449. $storeInfo['attr'] = $v;
  450. }
  451. $data['bargain'] = get_thumb_water($storeInfo);
  452. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  453. $data['bargain']['small_image'] = $storeInfoNew['image'];
  454. $data['site_name'] = sys_config('site_name');
  455. $data['share_qrcode'] = sys_config('share_qrcode', 0);
  456. //浏览记录
  457. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'id' => $id, 'product_id' => $storeInfo['product_id']], 'bargain']);
  458. return $data;
  459. }
  460. /**
  461. * 验证砍价是否能支付
  462. * @param int $bargainId
  463. * @param int $uid
  464. */
  465. public function checkBargainUser(int $bargainId, int $uid)
  466. {
  467. /** @var StoreBargainUserServices $bargainUserServices */
  468. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  469. $bargainUserInfo = $bargainUserServices->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  470. if (!$bargainUserInfo)
  471. throw new ValidateException('砍价失败');
  472. $bargainUserTableId = $bargainUserInfo['id'];
  473. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  474. throw new ValidateException('砍价未成功');
  475. }
  476. if ($bargainUserInfo['status'] == 3)
  477. throw new ValidateException('砍价已支付');
  478. /** @var StoreProductAttrValueServices $attrValueServices */
  479. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  480. $res = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  481. if (!$this->validBargain($bargainId) || !$res) {
  482. throw new ValidateException('该商品已下架或删除');
  483. }
  484. $StoreBargainInfo = $this->dao->get($bargainId);
  485. if (1 > $res['quota']) {
  486. throw new ValidateException('该商品库存不足');
  487. }
  488. $product_stock = $attrValueServices->value(['product_id' => $StoreBargainInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  489. if ($product_stock < 1) {
  490. throw new ValidateException('该商品库存不足');
  491. }
  492. return true;
  493. }
  494. /**
  495. * 修改砍价状态
  496. * @param int $bargainId
  497. * @param int $uid
  498. * @param int $bargainUserTableId
  499. * @return \crmeb\basic\BaseModel|false
  500. * @throws \think\db\exception\DataNotFoundException
  501. * @throws \think\db\exception\DbException
  502. * @throws \think\db\exception\ModelNotFoundException
  503. */
  504. public function setBargainUserStatus(int $bargainId, int $uid, int $bargainUserTableId = 0)
  505. {
  506. if (!$bargainId || !$uid) return false;
  507. if (!$bargainUserTableId) {
  508. /** @var StoreBargainUserServices $bargainUserServices */
  509. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  510. $bargainUserInfo = $bargainUserServices->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  511. if (!$bargainUserInfo)
  512. throw new ValidateException('砍价失败');
  513. $bargainUserTableId = $bargainUserInfo['id'];
  514. }
  515. /** @var StoreBargainUserServices $bargainUserServices */
  516. $bargainUserServices = app()->make(StoreBargainUserServices::class);
  517. $bargainUser = $bargainUserServices->getOne(['id' => $bargainUserTableId, 'uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1], 'price,bargain_price,bargain_price_min');
  518. if (!$bargainUser) {
  519. return false;
  520. }
  521. $userPrice = $bargainUser['price'];
  522. $price = bcsub($bargainUser['bargain_price'], $bargainUser['bargain_price_min'], 2);
  523. if (bcsub($price, $userPrice, 2) > 0) {
  524. return false;
  525. }
  526. return $bargainUserServices->updateBargainStatus($bargainUserTableId);
  527. }
  528. /**
  529. * 参与砍价
  530. * @param int $uid
  531. * @param int $bargainId
  532. * @return array
  533. * @throws \think\db\exception\DataNotFoundException
  534. * @throws \think\db\exception\DbException
  535. * @throws \think\db\exception\ModelNotFoundException
  536. */
  537. public function setBargain(int $uid, int $bargainId)
  538. {
  539. $bargainInfo = $this->dao->getOne([
  540. ['is_del', '=', 0],
  541. ['status', '=', 1],
  542. ['start_time', '<', time()],
  543. ['stop_time', '>', time()],
  544. ['id', '=', $bargainId],
  545. ['quota', '>', 0],
  546. ]);
  547. if (!$bargainInfo) throw new ValidateException('砍价已结束');
  548. $bargainInfo = $bargainInfo->toArray();
  549. /** @var StoreBargainUserServices $bargainUserService */
  550. $bargainUserService = app()->make(StoreBargainUserServices::class);
  551. $count = $bargainUserService->count(['bargain_id' => $bargainId, 'uid' => $uid, 'is_del' => 0, 'status' => 1]);
  552. if ($count === false) {
  553. throw new ValidateException('参数错误');
  554. } else {
  555. $count = $bargainUserService->count(['uid' => $uid, 'bargain_id' => $bargainId, 'type' => 1]);
  556. if ($count >= $bargainInfo['num']) throw new ValidateException('您不能再发起此件商品砍价');
  557. [$bargainUserInfo, $price] = $this->transaction(function () use ($uid, $bargainId, $bargainInfo, $bargainUserService) {
  558. $res = $bargainUserService->setBargain($bargainId, $uid, $bargainInfo);
  559. if (!$res) {
  560. throw new ValidateException('参与失败');
  561. }
  562. /** @var StoreBargainUserHelpServices $bargainUserHelpService */
  563. $bargainUserHelpService = app()->make(StoreBargainUserHelpServices::class);
  564. $price = $bargainUserHelpService->setBargainUserHelp($bargainId, (int)$res['id'], $uid, $bargainInfo);
  565. $bargainUserInfo = $bargainUserService->get((int)$res['id']);
  566. return [$bargainUserInfo, $price];
  567. });
  568. $status = $this->getBargainStatus((int)$bargainUserInfo['id'], $bargainUserInfo);
  569. return compact('bargainUserInfo', 'price', 'status');
  570. }
  571. }
  572. /**
  573. * 获取砍价状态
  574. * @param int $bargainUserTableId
  575. * @param $bargainUserInfo
  576. * @return bool
  577. * @throws \think\db\exception\DataNotFoundException
  578. * @throws \think\db\exception\DbException
  579. * @throws \think\db\exception\ModelNotFoundException
  580. */
  581. public function getBargainStatus(int $bargainUserTableId, $bargainUserInfo)
  582. {
  583. if (!$bargainUserInfo) {
  584. /** @var StoreBargainUserServices $bargainUserService */
  585. $bargainUserService = app()->make(StoreBargainUserServices::class);
  586. $bargainUserInfo = $bargainUserService->get($bargainUserTableId);// 获取用户参与砍价信息
  587. }
  588. if (bcsub($bargainUserInfo['bargain_price'], $bargainUserInfo['price'], 2) == $bargainUserInfo['bargain_price_min']) {
  589. $status = true;
  590. } else {
  591. $status = false;
  592. }
  593. return $status;
  594. }
  595. /**
  596. * 保存砍价记录返回砍价金额
  597. * @param int $uid
  598. * @param int $bargainId
  599. * @param int $bargainUserUid
  600. * @return array
  601. * @throws \think\db\exception\DataNotFoundException
  602. * @throws \think\db\exception\DbException
  603. * @throws \think\db\exception\ModelNotFoundException
  604. */
  605. public function setHelpBargain(int $uid, int $bargainId, int $bargainUserUid)
  606. {
  607. if (!$bargainId || !$bargainUserUid) throw new ValidateException('参数错误');
  608. $bargainInfo = $this->dao->getOne([
  609. ['is_del', '=', 0],
  610. ['status', '=', 1],
  611. ['start_time', '<', time()],
  612. ['stop_time', '>', time()],
  613. ['id', '=', $bargainId],
  614. ['quota', '>', 0],
  615. ]);
  616. if (!$bargainInfo) throw new ValidateException('砍价已结束');
  617. $bargainInfo = $bargainInfo->toArray();
  618. /** @var StoreBargainUserServices $bargainUserService */
  619. $bargainUserService = app()->make(StoreBargainUserServices::class);
  620. $bargainUserTableId = $bargainUserService->getBargainUserTableId($bargainId, $bargainUserUid);
  621. if (!$bargainUserTableId) throw new ValidateException('该分享未开启砍价');
  622. /** @var StoreBargainUserHelpServices $userHelpService */
  623. $userHelpService = app()->make(StoreBargainUserHelpServices::class);
  624. $count = $userHelpService->isBargainUserHelpCount($bargainId, $bargainUserTableId, $uid);
  625. $bargainUserInfo = $bargainUserService->get($bargainUserTableId);
  626. if (!$count) {
  627. throw new ValidateException('您已经帮砍过此砍价');
  628. }
  629. $price = $userHelpService->setBargainUserHelp($bargainId, $bargainUserTableId, $uid, $bargainInfo);
  630. if (!(float)$price) {
  631. [$coverPrice, $alreadyPrice, $surplusPrice, $pricePercent, $bargainUserInfo] = $bargainUserService->getSurplusPrice($bargainUserTableId);
  632. if (0.00 === (float)$surplusPrice) {
  633. $bargainUserInfo = $bargainUserService->get($bargainUserTableId);// 获取用户参与砍价信息
  634. //用户发送消息
  635. event('notice.notice', [['uid' => $bargainUserUid, 'bargainInfo' => $bargainInfo, 'bargainUserInfo' => $bargainUserInfo,], 'bargain_success']);
  636. }
  637. }
  638. $status = $this->getBargainStatus($bargainUserTableId, $bargainUserInfo);
  639. return compact('bargainUserInfo', 'price', 'status');
  640. }
  641. /**
  642. * 减库存加销量
  643. * @param int $num
  644. * @param int $bargainId
  645. * @param string $unique
  646. * @param int $store_id
  647. * @return bool
  648. */
  649. public function decBargainStock(int $num, int $bargainId, string $unique, int $store_id = 0)
  650. {
  651. //平台商品ID
  652. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  653. $res = true;
  654. if ($product_id) {
  655. if ($unique) {
  656. /** @var StoreProductAttrValueServices $skuValueServices */
  657. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  658. //减去砍价商品sku的库存增加销量
  659. $res = $res && $skuValueServices->decProductAttrStock($bargainId, $unique, $num, 2);
  660. //砍价商品sku
  661. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId, 'type' => 2], 'suk');
  662. //平台商品sku unique
  663. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  664. /** @var StoreProductServices $services */
  665. $services = app()->make(StoreProductServices::class);
  666. //减掉普通商品sku的库存加销量
  667. $res = $res && $services->decProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  668. }
  669. //减去砍价商品的库存和销量
  670. $res = $res && $this->dao->decStockIncSales(['id' => $bargainId, 'type' => 2], $num);
  671. }
  672. return $res;
  673. }
  674. /**
  675. * 减销量加库存
  676. * @param int $num
  677. * @param int $bargainId
  678. * @param string $unique
  679. * @param int $store_id
  680. * @return bool
  681. */
  682. public function incBargainStock(int $num, int $bargainId, string $unique, int $store_id = 0)
  683. {
  684. $product_id = $this->dao->value(['id' => $bargainId], 'product_id');
  685. $res = true;
  686. if ($product_id) {
  687. if ($unique) {
  688. /** @var StoreProductAttrValueServices $skuValueServices */
  689. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  690. //减去砍价商品sku的销量,增加库存和限购数量
  691. $res = false !== $skuValueServices->incProductAttrStock($bargainId, $unique, $num, 2);
  692. //砍价商品sku
  693. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $bargainId, 'type' => 2], 'suk');
  694. //平台商品sku unique
  695. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  696. /** @var StoreProductServices $services */
  697. $services = app()->make(StoreProductServices::class);
  698. //减掉普通商品的销量加库存
  699. $res = $res && $services->incProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  700. }
  701. //减去砍价商品的销量,增加库存
  702. $res = $res && $this->dao->incStockDecSales(['id' => $bargainId, 'type' => 2], $num);
  703. }
  704. return $res;
  705. }
  706. /**
  707. * 获取砍价海报信息
  708. * @param int $bargainId
  709. * @param $user
  710. * @throws \think\db\exception\DataNotFoundException
  711. * @throws \think\db\exception\DbException
  712. * @throws \think\db\exception\ModelNotFoundException
  713. */
  714. public function posterInfo(int $bargainId, $user)
  715. {
  716. $storeBargainInfo = $this->dao->get($bargainId, ['title', 'image', 'price', 'quota']);
  717. if (!$storeBargainInfo) {
  718. throw new ValidateException('砍价信息没有查到');
  719. }
  720. if ($storeBargainInfo['quota'] <= 0) {
  721. throw new ValidateException('砍价已结束');
  722. }
  723. /** @var StoreBargainUserServices $services */
  724. $services = app()->make(StoreBargainUserServices::class);
  725. $bargainUser = $services->get(['bargain_id' => $bargainId, 'uid' => $user['uid']], ['price', 'bargain_price_min']);
  726. if (!$bargainUser) {
  727. throw new ValidateException('用户砍价信息未查到');
  728. }
  729. $data['url'] = '';
  730. $data['title'] = $storeBargainInfo['title'];
  731. $data['image'] = $storeBargainInfo['image'];
  732. $data['price'] = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  733. $data['label'] = '已砍至';
  734. $price = bcsub($storeBargainInfo['price'], $bargainUser['price'], 2);
  735. $data['msg'] = '还差' . (bcsub($price, $bargainUser['bargain_price_min'], 2)) . '元即可砍价成功';
  736. //只有在小程序端,才会生成二维码
  737. if (\request()->isRoutine()) {
  738. try {
  739. /** @var SystemAttachmentServices $systemAttachmentServices */
  740. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  741. //小程序
  742. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  743. $siteUrl = sys_config('site_url');
  744. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  745. if (!$imageInfo) {
  746. $valueData = 'id=' . $bargainId . '&spid=' . $user['uid'];
  747. $res = MiniProgram::appCodeUnlimit($valueData, 'pages/activity/goods_bargain_details/index', 280);
  748. if (!$res) throw new ValidateException('二维码生成失败');
  749. $uploadType = (int)sys_config('upload_type', 1);
  750. $upload = UploadService::init($uploadType);
  751. $res = (string)Utils::streamFor($res);
  752. $res = $upload->to('routine/activity/bargain/code')->validate()->stream($res, $name);
  753. if ($res === false) {
  754. throw new ValidateException($upload->getError());
  755. }
  756. $imageInfo = $upload->getUploadInfo();
  757. $imageInfo['image_type'] = $uploadType;
  758. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  759. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  760. if (!$remoteImage['status']) throw new ValidateException($remoteImage['msg']);
  761. $systemAttachmentServices->save([
  762. 'name' => $imageInfo['name'],
  763. 'att_dir' => $imageInfo['dir'],
  764. 'satt_dir' => $imageInfo['thumb_path'],
  765. 'att_size' => $imageInfo['size'],
  766. 'att_type' => $imageInfo['type'],
  767. 'image_type' => $imageInfo['image_type'],
  768. 'module_type' => 2,
  769. 'time' => time(),
  770. 'pid' => 1,
  771. 'type' => 1
  772. ]);
  773. $url = $imageInfo['dir'];
  774. } else $url = $imageInfo['att_dir'];
  775. if ($imageInfo['image_type'] == 1) {
  776. $data['url'] = $siteUrl . $url;
  777. } else {
  778. $data['url'] = $url;
  779. }
  780. } catch (\Throwable $e) {
  781. }
  782. } else {
  783. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  784. /** @var QrcodeServices $qrcodeService */
  785. $qrcodeService = app()->make(QrcodeServices::class);
  786. $data['url'] = $qrcodeService->getTemporaryQrcode('bargain-' . $bargainId . '-' . $user['uid'], $user['uid'])->url;
  787. }
  788. }
  789. return $data;
  790. }
  791. /**
  792. * 验证砍价下单库存限量
  793. * @param int $uid
  794. * @param int $bargainId
  795. * @param int $cartNum
  796. * @param string $unique
  797. * @return array
  798. * @throws \think\db\exception\DataNotFoundException
  799. * @throws \think\db\exception\DbException
  800. * @throws \think\db\exception\ModelNotFoundException
  801. */
  802. public function checkBargainStock(int $uid, int $bargainId, int $cartNum = 1, string $unique = '')
  803. {
  804. if (!$this->validBargain($bargainId)) {
  805. throw new ValidateException('该商品已下架或删除');
  806. }
  807. /** @var StoreProductAttrValueServices $attrValueServices */
  808. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  809. $attrInfo = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  810. if (!$attrInfo || $attrInfo['product_id'] != $bargainId) {
  811. throw new ValidateException('请选择有效的商品属性');
  812. }
  813. $productInfo = $this->dao->get($bargainId, ['*', 'title as store_name']);
  814. /** @var StoreBargainUserServices $bargainUserService */
  815. $bargainUserService = app()->make(StoreBargainUserServices::class);
  816. $bargainUserInfo = $bargainUserService->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  817. if (!$bargainUserInfo || $bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  818. throw new ValidateException('砍价未成功');
  819. }
  820. $unique = $attrInfo['unique'];
  821. if ($cartNum > $attrInfo['quota']) {
  822. throw new ValidateException('该商品库存不足' . $cartNum);
  823. }
  824. return [$attrInfo, $unique, $productInfo, $bargainUserInfo];
  825. }
  826. /**
  827. * 砍价统计
  828. * @param $id
  829. * @return array
  830. */
  831. public function bargainStatistics(int $id)
  832. {
  833. /** @var StoreBargainUserServices $bargainUser */
  834. $bargainUser = app()->make(StoreBargainUserServices::class);
  835. /** @var StoreBargainUserHelpServices $bargainUserHelp */
  836. $bargainUserHelp = app()->make(StoreBargainUserHelpServices::class);
  837. /** @var StoreOrderServices $orderServices */
  838. $orderServices = app()->make(StoreOrderServices::class);
  839. $people_count = $bargainUserHelp->count(['bargain_id' => $id]);
  840. $spread_count = $bargainUserHelp->count(['bargain_id' => $id, 'type' => 0]);
  841. $start_count = $bargainUser->count(['bargain_id' => $id]);
  842. $success_count = $bargainUser->count(['bargain_id' => $id, 'status' => 3]);
  843. $pay_price = $orderServices->sum(['type' => 2, 'activity_id' => $id, 'paid' => 1], 'pay_price', true);
  844. $pay_count = $orderServices->count(['type' => 2, 'activity_id' => $id, 'paid' => 1]);
  845. $pay_rate = $start_count > 0 ? bcmul(bcdiv((string)$pay_count, (string)$start_count, 2), '100', 2) : 0;
  846. return compact('people_count', 'spread_count', 'start_count', 'success_count', 'pay_price', 'pay_count', 'pay_rate');
  847. }
  848. /**
  849. * 砍价统计列表
  850. * @param $id
  851. * @param array $where
  852. * @return array
  853. */
  854. public function bargainStatisticsList(int $id, array $where = [])
  855. {
  856. /** @var StoreBargainUserServices $bargainUser */
  857. $bargainUser = app()->make(StoreBargainUserServices::class);
  858. $where['bargain_id'] = $id;
  859. return $bargainUser->bargainUserList($where);
  860. }
  861. /**
  862. * 砍价统计订单
  863. * @param $id
  864. * @param array $where
  865. * @return array
  866. */
  867. public function bargainStatisticsOrder(int $id, array $where = [])
  868. {
  869. /** @var StoreOrderServices $orderServices */
  870. $orderServices = app()->make(StoreOrderServices::class);
  871. [$page, $limit] = $this->getPageValue();
  872. $list = $orderServices->activityStatisticsOrder($id, 2, $where, $page, $limit);
  873. $where['type'] = 2;
  874. $where['activity_id'] = $id;
  875. $count = $orderServices->count($where);
  876. foreach ($list as &$item) {
  877. if ($item['is_del'] || $item['is_system_del']) {
  878. $item['status'] = '已删除';
  879. } else if ($item['paid'] == 0 && $item['status'] == 0) {
  880. $item['status'] = '未支付';
  881. } else if ($item['paid'] == 1 && $item['status'] == 4 && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  882. $item['status'] = '部分发货';
  883. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  884. $item['status'] = '已退款';
  885. } else if ($item['paid'] == 1 && $item['status'] == 5 && $item['refund_status'] == 0) {
  886. $item['status'] = $item['shipping_type'] == 2 ? '部分核销' : '部分收货';
  887. $item['_status'] = 12;//已支付 部分核销
  888. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  889. $item['status'] = '申请退款';
  890. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  891. $item['status'] = '退款中';
  892. } else if ($item['paid'] == 1 && $item['status'] == 0 && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  893. $item['status'] = '未发货';
  894. $item['_status'] = 2;//已支付 未发货
  895. } else if ($item['paid'] == 1 && in_array($item['status'], [0, 1]) && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  896. $item['status'] = '未核销';
  897. } else if ($item['paid'] == 1 && in_array($item['status'], [1, 5]) && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  898. $item['status'] = '待收货';
  899. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  900. $item['status'] = '待评价';
  901. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  902. $item['status'] = '已完成';
  903. } else if ($item['paid'] == 1 && $item['refund_status'] == 3) {
  904. $item['status'] = '部分退款';
  905. } else {
  906. $item['status'] = '未知';
  907. }
  908. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  909. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  910. }
  911. return compact('list', 'count');
  912. }
  913. }