StoreService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <?php
  2. namespace addons\Happiness\common\service;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessClerk;
  5. use addons\Happiness\common\models\HappinessGoods;
  6. use addons\Happiness\common\models\HappinessGoodsRanking;
  7. use addons\Happiness\common\models\HappinessHistoryStore;
  8. use addons\Happiness\common\models\HappinessStore;
  9. use addons\Happiness\common\models\HappinessStoreAdmin;
  10. use addons\Happiness\common\models\HappinessStoreCommission;
  11. use addons\Happiness\common\models\HappinessStoreGoods;
  12. use common\enums\StatusEnum;
  13. use common\helpers\ApiCode;
  14. use common\enums\AddonsEnum;
  15. use common\helpers\sms\Sms;
  16. use common\models\user\User;
  17. use yii\helpers\Json;
  18. use Exception;
  19. use Money\Exchange;
  20. use Swoole\Http\Status;
  21. use Yii;
  22. class StoreService extends BaseService
  23. {
  24. //获取用户历史自提门店列表数据
  25. public function getHistoryStore($params)
  26. {
  27. try {
  28. $where = [
  29. ['mall_id' => $this->mall_id],
  30. ['user_id' => $this->user_id],
  31. ['status' => StatusEnum::ENABLED]
  32. ];
  33. $list = HappinessHistoryStore::lists([
  34. 'where' => $where,
  35. 'order' => 'id desc',
  36. 'with' => ['store'],
  37. ]);
  38. if (!empty($list)) {
  39. $today = date('Y-m-d', time());
  40. foreach ($list as $key => &$item) {
  41. if (empty($item['store'])) {
  42. unset($list[$key]);
  43. continue;
  44. }
  45. if ($item['store']['shop_status'] == 0) {
  46. unset($list[$key]);
  47. continue;
  48. }
  49. if (!empty($params['longitude']) && !empty($params['longitude']) && !empty($item['store'])) {
  50. $distance = round(
  51. 2 * 6378.137 * ASIN(
  52. SQRT(
  53. POW(
  54. SIN(
  55. PI() * ($params['longitude'] - $item['store']['longitude']) / 360
  56. ),
  57. 2
  58. ) + COS(PI() * $params['latitude'] / 180) * COS($item['store']['latitude'] * PI() / 180) * POW(
  59. SIN(
  60. PI() * ($params['latitude'] - $item['store']['latitude']) / 360
  61. ),
  62. 2
  63. )
  64. )
  65. )
  66. ,1);
  67. $item['store']['distance'] = bcmul($distance, 1, 1);
  68. $item['distance'] = bcmul($distance, 1, 1);
  69. $business_time_start = $today. ' ' . $item['business_time_start'];
  70. $business_time_end = $today. ' ' . $item['business_time_end'];
  71. if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) $item['disabled'] = true;
  72. $item['store']['disabled'] = false;
  73. $item['store']['intra_city_distribution'] = json_decode($item['store']['intra_city_distribution'], true);
  74. // if ($item['store']['intra_city_distribution']['is_alone'] == 1) {
  75. // if (!empty($item['store']['intra_city_distribution']['distribution_scope']) && $item['store']['intra_city_distribution']['distribution_scope'] < $item['store']['distance']) $item['store']['disabled'] = true;
  76. // } else {
  77. // //系统配置
  78. // $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  79. // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $item['store']['distance']) $item['store']['disabled'] = true;
  80. // }
  81. }
  82. }
  83. $field = 'distance';
  84. usort($list, function($a, $b) use($field) {
  85. return $a[$field] <=> $b[$field];
  86. });
  87. }
  88. return $list;
  89. } catch (\Exception $e) {
  90. throw new Exception($e->getMessage());
  91. }
  92. }
  93. //删除用户历史自提门店数据
  94. public function delHistoryStore($params)
  95. {
  96. try {
  97. //查询是否存在该历史数据
  98. $info = HappinessHistoryStore::getOne([
  99. ['mall_id' => $this->mall_id],
  100. ['user_id' => $this->user_id],
  101. ['id' => $params['id']],
  102. ['status' => StatusEnum::ENABLED],
  103. ]);
  104. if (empty($info)) throw new Exception('不存在该历史数据');
  105. $info->status = StatusEnum::DELETE;
  106. if (!$info->save()) throw new Exception('操作失败');
  107. return true;
  108. } catch (\Exception $e) {
  109. throw new Exception($e->getMessage());
  110. }
  111. }
  112. //获取门店详情
  113. public function getStoreDetail($params)
  114. {
  115. try {
  116. $where = [
  117. ['mall_id' => $this->mall_id],
  118. ['id' => $params['store_id']],
  119. ['status' => StatusEnum::ENABLED],
  120. ];
  121. $info = HappinessStore::getOne($where, true);
  122. if (empty($info)) {
  123. return null;
  124. } else {
  125. $info['intra_city_distribution'] = json_decode($info['intra_city_distribution'], true);
  126. //获取门店商品销量
  127. $sale_data = HappinessStoreGoods::lists([
  128. 'where' => [
  129. ['mall_id' => $this->mall_id],
  130. ['store_id' => $params['store_id']],
  131. ],
  132. ]);
  133. $sales_num = 0;
  134. //总销量
  135. if (!empty($sale_data)) {
  136. $sales_num = array_sum(array_column($sale_data, 'sales_num'));
  137. }
  138. $info['sales_num'] = $sales_num;
  139. //计算距离
  140. if (!empty($params['longitude']) && !empty($params['latitude'])) {
  141. $distance = round(
  142. 2 * 6378.137 * ASIN(
  143. SQRT(
  144. POW(
  145. SIN(
  146. PI() * ($params['longitude'] - $info['longitude']) / 360
  147. ),
  148. 2
  149. ) + COS(PI() * $params['latitude'] / 180) * COS($info['latitude'] * PI() / 180) * POW(
  150. SIN(
  151. PI() * ($params['latitude'] - $info['latitude']) / 360
  152. ),
  153. 2
  154. )
  155. )
  156. )
  157. ,1);
  158. $info['distance'] = bcmul($distance, 1, 1);
  159. $group_order_fee = 0;//配送费
  160. if ($info['intra_city_distribution']['is_alone'] == 1) {
  161. //当前门店是否免配送费
  162. if ($info['intra_city_distribution']['delivery_fee'] == 0) {
  163. $group_order_fee += 0;
  164. } else {
  165. $group_order_fee += $info['intra_city_distribution']['distance_charge'];
  166. //如果配送距离超出初始配送距离,则需累计计算每次增加的公里数对应的配送费
  167. if ($info['intra_city_distribution']['distance'] < $info['distance']) {
  168. $distance = $info['distance'] - $info['intra_city_distribution']['distance'] ;
  169. if ($info['intra_city_distribution']['every_distance'] > 0) {
  170. $group_order_fee += ceil($distance/$info['intra_city_distribution']['every_distance']) * $info['intra_city_distribution']['every_distance_charge'];
  171. }
  172. }
  173. }
  174. } else {
  175. //获取幸福小店插件基础配置
  176. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  177. //基础配置是否免配送
  178. if ($setting['delivery_fee'] == 0) {
  179. $group_order_fee += 0;
  180. } else {
  181. $group_order_fee += $info['intra_city_distribution']['start_amount'];
  182. if ($setting['start_limit'] < $info['distance']) {
  183. $distance = $info['distance'] - $setting['start_limit'];
  184. if ($setting['add_limit'] > 0) {
  185. $group_order_fee += ceil($distance/$setting['add_limit']) * $setting['add_amount'];
  186. }
  187. }
  188. }
  189. }
  190. $info['shipping_money'] = $group_order_fee;
  191. }
  192. }
  193. return $info;
  194. } catch (\Exception $e) {
  195. throw new Exception($e->getMessage());
  196. }
  197. }
  198. //获取最近距离的门店
  199. public function getStore($params)
  200. {
  201. try {
  202. if (empty($params['longitude']) || empty($params['latitude'])) return [];
  203. $status = StatusEnum::ENABLED;
  204. $where = " WHERE (s.mall_id={$params['mall_id']}) AND (s.status={$status}) AND (yg.status={$status}) AND (yg.is_on_sale={$status}) AND (yg.stock > 0) AND (s.shop_status={$status}) AND (s.merchant_distribution={$status} or s.self_mention={$status})";
  205. if (!empty($params['goods_ids'])) {
  206. $goods_ids = implode(',', $params['goods_ids']);
  207. $where .= " AND yg.goods_id in ({$goods_ids})";
  208. }
  209. //传入单个规格字段时
  210. if (!empty($params['attr_id'])) $where .= " AND yg.attr_id = {$params['attr_id']}";
  211. //传入多规格字段时
  212. if (!empty($params['attr_ids'])) {
  213. $attr_ids = implode(',', $params['attr_ids']);
  214. $where .= " AND yg.attr_id in ({$attr_ids})";
  215. }
  216. //根据订单快递类型,获取门店
  217. if (!empty($params['shipping_type'])) {
  218. switch ($params['shipping_type']) {
  219. case 2:
  220. $where .= " AND s.self_mention = 1";
  221. break;
  222. case 3:
  223. $where .= " AND s.merchant_distribution = 1";
  224. break;
  225. }
  226. }
  227. //系统配置
  228. $setting = \Yii::$app->services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic');
  229. $sql = "select s.id,s.shop_mobile,s.store_name,s.distribution_scope,s.logo_img,yg.stock,s.address,s.longitude,s.latitude,s.shop_status,s.business_time_start,s.business_time_end,s.merchant_distribution,s.self_mention,s.intra_city_distribution,round(
  230. 2 * 6378.137 * ASIN(
  231. SQRT(
  232. POW(
  233. SIN(
  234. PI() * ({$params['longitude']} - s.longitude) / 360
  235. ),
  236. 2
  237. ) + COS(PI() * {$params['latitude']} / 180) * COS(s.latitude * PI() / 180) * POW(
  238. SIN(
  239. PI() * ({$params['latitude']} - s.latitude) / 360
  240. ),
  241. 2
  242. )
  243. )
  244. )
  245. ,1) AS distance from qimall_addons_happiness_store as s left join qimall_addons_happiness_store_goods_attr as yg on s.id = yg.store_id";
  246. $sql .= $where;
  247. $sql .= ' GROUP BY s.id';
  248. if (!empty($params['shipping_type']) && $params['shipping_type'] == 3) {
  249. $sql .= " HAVING distance <= s.distribution_scope";
  250. } else {
  251. if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit'])) {
  252. $sql .= " HAVING distance <= ". $setting['localtion_limit'];
  253. }
  254. }
  255. $page = 1;
  256. $limit = 20;
  257. $sql .= " ORDER BY distance asc";
  258. $sql .= " limit " . ($page - 1) * $limit . ",{$limit}";
  259. $info = \Yii::$app->db->createCommand($sql)->queryAll();
  260. if (empty($info)) {
  261. return null;
  262. } else {
  263. foreach ($info as $key => $value) {
  264. $today = date('Y-m-d', time());
  265. $business_time_start = $today. ' ' . $value['business_time_start'];
  266. $business_time_end = $today. ' ' . $value['business_time_end'];
  267. if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) continue;
  268. $value['intra_city_distribution'] = json_decode($value['intra_city_distribution'], true);
  269. // if (!isset($params['shipping_type']) || $params['shipping_type'] == 3) {
  270. // if ($value['intra_city_distribution']['is_alone'] == 1) {
  271. // if (!empty($value['intra_city_distribution']['distribution_scope']) && $value['intra_city_distribution']['distribution_scope'] < $value['distance']) continue;
  272. // } else {
  273. // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $value['distance']) continue;
  274. // }
  275. // }
  276. return $value;
  277. }
  278. return null;
  279. }
  280. } catch (\Exception $e) {
  281. throw new Exception($e->getMessage());
  282. }
  283. }
  284. public function getStoreList($params)
  285. {
  286. try {
  287. if (empty($params['longitude']) || empty($params['latitude'])) return [];
  288. $page = $params['page']??1;
  289. $limit = $params['limit']??15;
  290. $status = StatusEnum::ENABLED;
  291. $where = " WHERE (s.mall_id={$this->mall_id}) AND (s.status={$status}) AND (s.shop_status={$status}) AND (s.merchant_distribution={$status} or s.self_mention={$status})";
  292. // if (!empty($params['goods_ids'])) {
  293. // $goods_ids = implode(',', $params['goods_ids']);
  294. // $where .= " AND yg.goods_id in ({$goods_ids})";
  295. // }
  296. //传入单个规格字段时
  297. //if (!empty($params['attr_id'])) $where .= " AND yg.attr_id = {$params['attr_id']}";
  298. //传入多规格字段时
  299. // if (!empty($params['attr_ids'])) {
  300. // $attr_ids = implode(',', $params['attr_ids']);
  301. // $where .= " AND yg.attr_id in ({$attr_ids})";
  302. // }
  303. if (!empty($params['keyword'])) {
  304. $where .= " AND (s.store_name like '%". $params['keyword'] ."%' or s.address like '%". $params['keyword'] ."%')";
  305. }
  306. //根据订单快递类型,获取门店
  307. if (!empty($params['shipping_type'])) {
  308. switch ($params['shipping_type']) {
  309. case 2:
  310. $where .= " AND s.self_mention = 1";
  311. break;
  312. case 3:
  313. $where .= " AND s.merchant_distribution = 1";
  314. break;
  315. }
  316. }
  317. //系统配置
  318. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  319. $sql = "select s.id,s.store_name,s.latitude,s.longitude,s.distribution_scope,s.business_time_start,s.business_time_end,s.logo_img,s.merchant_distribution,s.self_mention,s.address,s.intra_city_distribution,round(
  320. 2 * 6378.137 * ASIN(
  321. SQRT(
  322. POW(
  323. SIN(
  324. PI() * ({$params['longitude']} - s.longitude) / 360
  325. ),
  326. 2
  327. ) + COS(PI() * {$params['latitude']} / 180) * COS(s.latitude * PI() / 180) * POW(
  328. SIN(
  329. PI() * ({$params['latitude']} - s.latitude) / 360
  330. ),
  331. 2
  332. )
  333. )
  334. )
  335. ,1) AS distance from qimall_addons_happiness_store as s";
  336. $sql .= $where;
  337. $sql .= ' GROUP BY s.id';
  338. if (!empty($params['shipping_type']) && $params['shipping_type'] == 3) {
  339. $sql .= " HAVING distance <= s.distribution_scope";
  340. } else {
  341. if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit'])) {
  342. $sql .= " HAVING distance <= ". $setting['localtion_limit'];
  343. }
  344. }
  345. $sql .= " ORDER BY distance asc";
  346. $count_list = \Yii::$app->db->createCommand($sql)->queryAll();
  347. $count = count($count_list);
  348. $sql .= " limit " . ($page - 1) * $limit . ",{$limit}";
  349. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  350. if($list===false)
  351. {
  352. throw new Exception('查询失败');
  353. }
  354. $today = date('Y-m-d', time());
  355. if (!empty($list)) {
  356. foreach ($list as &$item) {
  357. $item['disabled'] = false;
  358. //$item['intra_city_distribution'] = json_decode($item['intra_city_distribution'], true);
  359. $business_time_start = $today. ' ' . $item['business_time_start'];
  360. $business_time_end = $today. ' ' . $item['business_time_end'];
  361. if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) $item['disabled'] = true;
  362. // if ($item['intra_city_distribution']['is_alone'] == 1) {
  363. // if (!empty($item['intra_city_distribution']['distribution_scope']) && $item['intra_city_distribution']['distribution_scope'] < $item['distance']) $item['disabled'] = true;
  364. // } else {
  365. // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $item['distance']) $item['disabled'] = true;
  366. // }
  367. }
  368. }
  369. $list_page['list'] = $list;
  370. $list_page['page'] = $page;
  371. $list_page['page_size'] = $limit;
  372. $list_page['count'] = $count;
  373. $list_page['page_count'] = ceil($count / $limit);
  374. return $list_page;
  375. } catch (\Exception $e) {
  376. throw new Exception($e->getMessage());
  377. }
  378. }
  379. //订单初始化,获取门店
  380. public function selectStore($params)
  381. {
  382. try {
  383. if (empty($params['longitude']) || empty($params['latitude'])) return null;
  384. //如果存在门店id,则直接获取门店数据
  385. if (!empty($params['store_id'])) {
  386. $select = 'id,shop_mobile,store_name,logo_img,address,longitude,latitude,shop_status,business_time_start,business_time_end,merchant_distribution,self_mention,intra_city_distribution';
  387. $where = [
  388. ['mall_id' => $params['mall_id']],
  389. ['status' => StatusEnum::ENABLED],
  390. ['id' => $params['store_id']],
  391. ];
  392. $info = HappinessStore::getOne($where, true, [], 'id desc', $select);
  393. if (!empty($info)) {
  394. $distance = round(
  395. 2 * 6378.137 * ASIN(
  396. SQRT(
  397. POW(
  398. SIN(
  399. PI() * ($params['longitude'] - $info['longitude']) / 360
  400. ),
  401. 2
  402. ) + COS(PI() * $params['latitude'] / 180) * COS($info['latitude'] * PI() / 180) * POW(
  403. SIN(
  404. PI() * ($params['latitude'] - $info['latitude']) / 360
  405. ),
  406. 2
  407. )
  408. )
  409. )
  410. ,1);
  411. $info['distance'] = bcmul($distance, 1, 1);
  412. $info['intra_city_distribution'] = json_decode($info['intra_city_distribution'], true);
  413. }
  414. } else {
  415. //如果不存在门店id,则获取距离最近的门店
  416. $data = $this->getStore($params);
  417. $info = $data;
  418. }
  419. $info['disabled'] = false;
  420. if (!empty($info['intra_city_distribution']) && $params['shipping_type'] == 3) {
  421. //门店独立配置
  422. if ($info['intra_city_distribution']['is_alone'] == 1) {
  423. if (!empty($info['intra_city_distribution']['distribution_scope']) &&$info['intra_city_distribution']['distribution_scope'] < $info['distance']) $info['disabled'] = true;
  424. $info['delivery_amount'] = $info['intra_city_distribution']['initial_delivery_amount'];
  425. } else {
  426. //系统配置
  427. $setting = \Yii::$app->services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic');
  428. if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $info['distance']) $info['disabled'] = true;
  429. $info['setting'] = $setting;
  430. $info['delivery_amount'] = $setting['delivery_amount'];
  431. }
  432. }
  433. return $info;
  434. } catch (\Exception $e) {
  435. throw new Exception($e->getMessage());
  436. }
  437. }
  438. public function getIncomeLogList(array $get, int $pageSize): array
  439. {
  440. $where[] = ['mall_id' => $this->mall_id];
  441. $where[] = ['store_id' => $this->store_id];
  442. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  443. // 是否没有提交筛选条件 (用于决定总数直接从store表拿还是要重新查询统计)
  444. $whereIsEmpty = true;
  445. if (isset($get['keyword']) && $get['keyword'] !== '') {
  446. $whereIsEmpty = false;
  447. $user_list = User::lists([
  448. 'where' => [
  449. ['mall_id' => $this->mall_id],
  450. [
  451. 'or',
  452. ['like', 'username', trim($get['keyword'])],
  453. ['like', 'nickname', trim($get['keyword'])],
  454. ['like', 'mobile', trim($get['keyword'])]
  455. ]
  456. ],
  457. 'select' => 'id'
  458. ]);
  459. $where[] = ['user_id' => array_column($user_list, 'id')];
  460. }
  461. if (!empty($get['user_id'])) {
  462. $whereIsEmpty = false;
  463. $where[] = ['user_id' => $get['user_id']];
  464. }
  465. if (isset($get['order_no']) && $get['order_no'] !== '') {
  466. $whereIsEmpty = false;
  467. $where[] = ['order_no' => $get['order_no']];
  468. }
  469. if (isset($get['is_settlement']) && $get['is_settlement'] !== '') {
  470. $whereIsEmpty = false;
  471. $where[] = ['is_settlement' => $get['is_settlement']];
  472. }
  473. if (!empty($get['date'])) {
  474. $whereIsEmpty = false;
  475. $get['date'][1] .= ' 23:59:59';
  476. array_walk($get['date'], function (&$item) {
  477. $item = strtotime($item);
  478. });
  479. $where[] = ['between', 'created_at', ...$get['date']];
  480. }
  481. $params = [
  482. 'where' => $where,
  483. 'order' => 'id desc',
  484. 'with' => ['user'],
  485. 'limit' => $get['limit'] ?? $pageSize,
  486. ];
  487. $page_data = HappinessStoreCommission::listPage($params);
  488. $total_num = $total_order_money = $total_settled_amount = $unsettled_amount = 0;
  489. if ($page_data) {
  490. $store = HappinessStore::findOne(['id' => $this->store_id]);
  491. // 如果所有搜索条件为空则显示这个,否则需要查询
  492. if ($whereIsEmpty) {
  493. if ($store) {
  494. $commissionTotal = $store;
  495. }
  496. } else {
  497. // 按搜索结果 sum
  498. $commissionTotalField = [
  499. 'COUNT(*) `total_num`',
  500. 'SUM(`pay_money`) `total_order_money`',
  501. 'SUM(IF(`is_settlement` = 1, `amount_received`, 0)) `total_settled_amount`',
  502. 'SUM(IF(`is_settlement` = 0, `amount_received`, 0)) `unsettled_amount`'
  503. ];
  504. $commissionTotal = HappinessStoreCommission::getOne($where, true, [], '', $commissionTotalField);
  505. }
  506. if (isset($commissionTotal)) {
  507. $total_num = $commissionTotal['total_num'];
  508. $total_order_money = $commissionTotal['total_order_money'];
  509. $total_settled_amount = $commissionTotal['total_settled_amount'];
  510. $unsettled_amount = $commissionTotal['unsettled_amount'];
  511. }
  512. if (!empty($page_data['list'])) {
  513. $user_id_arr = array_column($page_data['list'], 'user_id');
  514. $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'index' => 'id']);
  515. $order_no_arr = array_column($page_data['list'], 'order_no');
  516. foreach ($page_data['list'] as &$item) {
  517. if ($item['type'] == 2) $order_no_arr[] = $item['order_no'];
  518. }
  519. foreach ($page_data['list'] as &$item) {
  520. $item['nickname'] = $item['mobile'] = $item['avatar_url'] = '';
  521. if (isset($user_list[$item['user_id']])) {
  522. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  523. $item['mobile'] = $user_list[$item['user_id']]['mobile'];
  524. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  525. }
  526. if ($item['type'] == 2) {
  527. if (isset($store_cashier_order_list[$item['order_no']])) {
  528. $addons_bonus = 0;
  529. $extra = json_decode($store_cashier_order_list[$item['order_no']]['extra'], true);
  530. if (!empty($extra)) {
  531. foreach ($extra as $k => $v) {
  532. if (in_array($k, ['agent_commission', 'distribution_commission', 'boss_commission', 'area_commission'])) $addons_bonus += $v;
  533. }
  534. }
  535. $item['addons_bonus'] = bcadd($addons_bonus, 0, 2);
  536. }
  537. }
  538. if (empty($store['payment_settlement_type'])) {
  539. $item['is_show'] = 0;
  540. } else {
  541. $item['is_show'] = 1;
  542. if (isset($check_list[$item['id']])) {
  543. $item['is_show'] = 0;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. $order_total_arr = [];
  550. $order_total_arr[] = ['type' => 'total_num', 'name' => '总数', 'value' => $total_num ?: 0, 'prompt' => ''];
  551. $order_total_arr[] = ['type' => 'total_order_money', 'name' => '订单总金额', 'value' => $total_order_money ?: 0, 'prompt' => ''];
  552. $order_total_arr[] = ['type' => 'total_settled_amount', 'name' => '已结算总金额', 'value' => $total_settled_amount ?: 0, 'prompt' => ''];
  553. $order_total_arr[] = ['type' => 'unsettled_amount', 'name' => '未结算总金额', 'value' => $unsettled_amount ?: 0, 'prompt' => ''];
  554. $page_data['order_total_arr'] = $order_total_arr;
  555. $page_data['store_name'] = isset($store) ? $store['store_name'] : '';
  556. return $page_data;
  557. }
  558. public function getClerkIdByUserId(int $userId, bool $isStoreUserId = false): int
  559. {
  560. if (!$userId) {
  561. return 0;
  562. }
  563. $where = [
  564. 'mall_id' => $this->mall_id,
  565. 'store_id' => $this->store_id,
  566. 'status' => StatusEnum::ENABLED,
  567. ];
  568. $id = HappinessClerk::find()->where(array_merge(['user_id' => $userId], $where))->select('id')->scalar();
  569. if ($id) {
  570. return $id;
  571. }
  572. if ($isStoreUserId) {
  573. return 0;
  574. }
  575. $storeUserId = HappinessStore::find()->where(['id' => $this->store_id])->select('user_id')->scalar();
  576. if (!$storeUserId) {
  577. return 0;
  578. }
  579. return HappinessClerk::find()->where(array_merge(['user_id' => $storeUserId], $where))->select('id')->scalar() ?: 0;
  580. }
  581. /**
  582. * 发送验证码
  583. *
  584. * @return boolean
  585. */
  586. public function sendCaptcha()
  587. {
  588. $mobile = HappinessStore::getMobileById($this->mall_id, $this->store_id);
  589. $sms = new Sms($this->mall_id, 'domestic');
  590. // 发送验证码
  591. try {
  592. if ($sms->sendCaptcha($mobile, 86, 'update_happiness_pass') === false) {
  593. return $this->error('发送失败');
  594. }
  595. }catch (\Exception $e){
  596. return $this->error($e->getMessage());
  597. }
  598. return true;
  599. }
  600. /**
  601. * 修改密码
  602. *
  603. * @param string $captcha
  604. * @param string $pwd
  605. * @return boolean
  606. */
  607. public function changePwd(string $captcha, string $pwd)
  608. {
  609. $mobile = HappinessStore::getMobileById($this->mall_id, $this->store_id);
  610. $sms = new Sms($this->mall_id, 'domestic');
  611. try {
  612. if (!$sms->checkValidateCode($mobile, $captcha, 'update_happiness_pass')) {
  613. return $this->error('验证码错误');
  614. }
  615. } catch (\Exception $e) {
  616. return $this->error($e->getMessage());
  617. }
  618. $store_admin = HappinessStoreAdmin::getAdminByStoreId($this->mall_id, $this->store_id);
  619. return $store_admin->changePwd($pwd);
  620. }
  621. }