Store.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\HuifuPay\common\service\HuifuBillShareService;
  4. use addons\ReservationService\common\models\ReservationServiceUser;
  5. use addons\Store\common\enums\StoreEnum;
  6. use common\enums\AddonsEnum;
  7. use common\enums\OrderStatusEnum;
  8. use common\enums\PayTypeEnum;
  9. use common\enums\StatusEnum;
  10. use common\enums\UserWalletEnum;
  11. use common\models\base\BaseActiveRecord;
  12. use common\models\common\CapitalLog;
  13. use common\models\mall\MallAddons;
  14. use services\common\UserWalletService;
  15. use Yii;
  16. use yii\db\Exception;
  17. use common\models\common\CommissionLog;
  18. use common\models\mall\Mall;
  19. use yii\console\ExitCode;
  20. /**
  21. * This is the model class for table "qimall_addons_store".
  22. *
  23. * @property int $id
  24. * @property int $mall_id
  25. * @property int $user_id 绑定会员id
  26. * @property string $shop_owner 店长姓名
  27. * @property string $shop_mobile 店长手机号
  28. * @property string $store_name 门店名称
  29. * @property int $c_id 门店分类
  30. * @property string $store_desc 门店简介
  31. * @property string $logo_img 门店logo
  32. * @property string $store_img 门店图片
  33. * @property string $license_img 营业执照
  34. * @property int|null $province_id 省
  35. * @property int|null $city_id 市
  36. * @property int|null $district_id 区
  37. * @property int|null $street_id 区
  38. * @property int|null $community_id 区
  39. * @property string $address 详细地址
  40. * @property int $expire_time 失效时间,0代表无限期
  41. * @property float $total_money 累计收入
  42. * @property float $share_amount 累计收入
  43. * @property float $balance 当前余额
  44. * @property float $total_expenses 累计支出
  45. * @property int|null $user_nums 会员总数
  46. * @property string $longitude 经度
  47. * @property string $latitude 维度
  48. * @property int $check_status 审核状态:0:待审核;1:已通过;2:未通过
  49. * @property string $check_remark 审核备注
  50. * @property string $business_time_start 营业时间开始
  51. * @property string $business_time_end 营业时间结束
  52. * @property int $store_style 门店样式 1:样式1;2:样式2
  53. * @property string $store_background 门店背景
  54. * @property string $commission_authority 分佣/营销权限
  55. * @property string $share_title 分享标题
  56. * @property string $share_desc 分享描述
  57. * @property string $share_pic 分享图片
  58. * @property string $freight_type 配送设置
  59. * @property string $intra_city_distribution 同城配送
  60. * @property float $total_order_money 订单总金额
  61. * @property int $total_num 总数
  62. * @property float $total_settled_amount 已结算总金额
  63. * @property float $unsettled_amount 未结算金额
  64. * @property int $sales_volume 销量
  65. * @property float $praise 评分
  66. * @property int $status 状态[0禁用 1启用 -1删除]
  67. * @property int $shop_status 门店营业状态,1:正在营业;0:休息中
  68. * @property int $scan_code_bind 门店收款码-扫码绑定开关:0:否;1:是
  69. * @property int $created_at 创建时间
  70. * @property int $updated_at 修改时间
  71. * @property int $is_open_credit 是否开启赊账功能
  72. * @property int $payment_settlement_type 门店货款结算方式,是否开启上传凭证,0:否;1:是
  73. * @property string $device_id 芸众喇叭设备id
  74. * @property int $show_style
  75. * @property int $show_sync_goods
  76. * @property string $bannar_pic
  77. * @property string $introduce
  78. * @property int $is_show_wechat_video_number 是否显示微信视频号:0否;1是
  79. * @property int $is_show_coupon 是否显示优惠券:0否;1是
  80. * @property int $default_store_cate_style 门店分类样式
  81. * @property string $wechat_video_number_id 微信视频号id
  82. * @property int store_category_switch 门店分类开关
  83. * @property int is_sync 是否需要同步
  84. * @property int sync_status 同步状态
  85. * @property int is_examine 审核方式:-1:后台添加,0:h5申请,不用审核,>0:h5申请,需要审核,记录审核人ID
  86. * @property int $limit_distance 匹配距离限制
  87. */
  88. class Store extends BaseActiveRecord
  89. {
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public static function tableName()
  94. {
  95. return '{{%addons_store}}';
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function rules()
  101. {
  102. return [
  103. [['mall_id', 'user_id', 'c_id', 'province_id', 'city_id', 'district_id', 'expire_time', 'user_nums', 'check_status', 'store_style',
  104. 'total_num','sales_volume', 'status', 'shop_status','scan_code_bind', 'created_at', 'updated_at','is_open_credit',
  105. 'payment_settlement_type','show_style','is_freeze', 'show_sync_goods','street_id','community_id','is_show_wechat_video_number', 'store_category_switch',
  106. 'is_show_coupon','default_store_cate_style', 'is_sync', 'sync_status', 'is_examine'], 'integer'],
  107. [['total_money', 'balance', 'total_expenses', 'total_order_money', 'total_settled_amount', 'unsettled_amount','praise', 'share_amount', 'limit_distance'], 'number'],
  108. [['shop_owner', 'store_name', 'store_desc', 'logo_img','store_img', 'license_img', 'address', 'longitude', 'latitude', 'check_remark', 'business_time_start', 'business_time_end', 'store_background', 'commission_authority', 'share_title', 'share_desc', 'share_pic', 'freight_type'], 'string', 'max' => 255],
  109. [['intra_city_distribution'],'string', 'max' => 500],
  110. [['shop_mobile'], 'string', 'max' => 11],
  111. [['device_id'], 'string', 'max' => 255],
  112. [['bannar_pic','introduce', 'pickup'], 'string'],
  113. [['wechat_video_number_id'],'safe']
  114. ];
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function attributeLabels()
  120. {
  121. return [
  122. 'id' => 'ID',
  123. 'mall_id' => 'Mall ID',
  124. 'user_id' => '绑定会员id',
  125. 'shop_owner' => '店长姓名',
  126. 'shop_mobile' => '店长手机号',
  127. 'store_name' => '门店名称',
  128. 'c_id' => '门店分类',
  129. 'store_desc' => '门店简介',
  130. 'logo_img' => '门店logo',
  131. 'license_img' => '营业执照',
  132. 'province_id' => '省',
  133. 'city_id' => '市',
  134. 'district_id' => '区',
  135. 'street_id' => '街道',
  136. 'community_id' => '社区',
  137. 'address' => '详细地址',
  138. 'expire_time' => '失效时间,0代表无限期',
  139. 'total_money' => '累计收入',
  140. 'balance' => '当前余额',
  141. 'total_expenses' => '累计支出',
  142. 'user_nums' => '会员总数',
  143. 'longitude' => '经度',
  144. 'latitude' => '维度',
  145. 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过',
  146. 'check_remark' => '审核备注',
  147. 'business_time_start' => '营业时间开始',
  148. 'business_time_end' => '营业时间结束',
  149. 'store_style' => '门店样式 1:样式1;2:样式2',
  150. 'store_background' => '门店背景',
  151. 'commission_authority' => '分佣/营销权限',
  152. 'share_title' => '分享标题',
  153. 'share_desc' => '分享描述',
  154. 'share_pic' => '分享图片',
  155. 'freight_type' => '配送设置',
  156. 'intra_city_distribution' => '同城配送',
  157. 'total_order_money' => '订单总金额',
  158. 'total_num' => '总数',
  159. 'total_settled_amount' => '已结算总金额',
  160. 'unsettled_amount' => '未结算金额',
  161. 'status' => '状态[0禁用 1启用 -1删除]',
  162. 'shop_status' => '门店营业状态',
  163. 'scan_code_bind' => '门店收款码-扫码绑定开关:0:否;1:是',
  164. 'created_at' => '创建时间',
  165. 'updated_at' => '修改时间',
  166. 'payment_settlement_type'=>'门店货款结算方式,是否开启上传凭证,0:否;1:是',
  167. 'device_id' => '芸众喇叭设备id',
  168. 'show_style' => '门店装修风格',
  169. 'bannar_pic' => '门店轮播图',
  170. 'introduce' => '门店介绍',
  171. 'is_freeze' => '是否冻结',
  172. 'show_sync_goods' => '是否显示同步的商品[0否,1是]',
  173. 'pickup' => '',
  174. 'is_show_wechat_video_number' => '是否显示微信视频号:0否;1是',
  175. 'is_show_coupon' => '是否显示优惠券:0否;1是',
  176. 'wechat_video_number_id' => '微信视频号id',
  177. 'default_store_cate_style' => '门店分类样式',
  178. 'store_category_switch' => '门店分类开关',
  179. 'is_sync' => '是否需要同步:0:否,1:是',
  180. 'sync_status' => '同步状态:0:待同步;1:同步中;2:同步完成',
  181. 'is_examine' => '审核方式:-1:后台添加,0:h5申请,不用审核,>0:h5申请,需要审核,记录审核人ID',
  182. 'limit_distance' => '匹配距离限制',
  183. ];
  184. }
  185. public static function setData(array $params, bool $is_custom_pwd = false)
  186. {
  187. $t = Yii::$app->db->beginTransaction();
  188. try {
  189. if (!empty($params['id'])) {
  190. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  191. if (empty($model)) throw new \Exception('门店不存在或已删除');
  192. } else {
  193. $model = new self();
  194. $model->loadDefaultValues();
  195. $settings = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'enter');
  196. if (1 == $settings['time_status']) {
  197. $seconds = $settings['expired_days'] * 86400 + time();
  198. $model->expire_time = $seconds;
  199. } else {
  200. $model->expire_time = 0;
  201. }
  202. $decorationSetting = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'decoration');
  203. $model->show_style = $decorationSetting['default_store_style'] ?? 1;
  204. $model->default_store_cate_style = $decorationSetting['default_store_cate_style'] ?? 1;
  205. $model->store_category_switch = $decorationSetting['store_category_switch'] ?? 0;
  206. }
  207. if(!empty($params['pickup'])) $params['pickup'] = json_encode($params['pickup']);
  208. if(!empty($params['freight_type'])) $params['freight_type'] = json_encode($params['freight_type']);
  209. if (empty($model->intra_city_distribution)) $params['intra_city_distribution'] = [];
  210. if (isset($params['intra_city_distribution'])) $params['intra_city_distribution'] = json_encode($params['intra_city_distribution']);
  211. if(!empty($params['introduce'])) $params['introduce'] = json_encode($params['introduce']);
  212. if(!empty($params['bannar_pic'])) $params['bannar_pic'] = json_encode($params['bannar_pic']);
  213. if (!$model->load($params, '') || !$model->save()) {
  214. throw new \Exception($model->getErrorMessage());
  215. }
  216. $store_addmin = StoreAdmin::findOne(['mall_id' => $params['mall_id'], 'store_id' => $model->id, 'status' => StatusEnum::ENABLED]);
  217. // 同步一下账号
  218. if($store_addmin){
  219. if($model->shop_mobile != $store_addmin->username){
  220. $pwd_str = substr($model->shop_mobile, -6);
  221. if ($is_custom_pwd) $pwd_str = $params['password'];
  222. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  223. $store_addmin->username = $model->shop_mobile;
  224. $store_addmin->password = $pwd_hash_str;
  225. if(!$store_addmin->save()) throw new \Exception($store_addmin->getErrorMessage());
  226. }
  227. }
  228. if (!empty($params['id']) && MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE)) {
  229. ReservationServiceUser::updateAll(['store_lng' => $model->longitude, 'store_lat' => $model->latitude], ['store_id' => $model->id]);
  230. }
  231. $t->commit();
  232. return $model;
  233. } catch (\Exception $e) {
  234. $t->rollBack();
  235. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  236. return false;
  237. }
  238. }
  239. /**
  240. * @param array $params
  241. * @param \Closure|null $callback
  242. * @param bool $is_custom_pwd //是否自定义密码
  243. * @return bool {*}
  244. * @throws Exception
  245. * @throws \yii\base\Exception
  246. * @msg: 添加一个门店,同时添加门店后台的管理员。默认账号是门店绑定手机号,默认登录密码是手机号后六位
  247. */
  248. public static function addStore(array $params, \Closure $callback = null, bool $is_custom_pwd = false)
  249. {
  250. $transaction = \Yii::$app->db->beginTransaction();
  251. $res = self::setData($params, $is_custom_pwd);
  252. if (false === $res) {
  253. $transaction->rollBack();
  254. return false;
  255. }
  256. $pwd_str = substr($params['shop_mobile'], -6);
  257. if ($is_custom_pwd) $pwd_str = $params['password'];
  258. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  259. $admin_info = [
  260. 'username' => $params['shop_mobile'],
  261. 'account' => $params['shop_owner'],
  262. 'password' => $pwd_hash_str,
  263. 'mall_id' => $params['mall_id'],
  264. 'admin_type' => 2,
  265. 'status' => StatusEnum::ENABLED,
  266. 'store_id' => $res->id,
  267. ];
  268. $admin_model = StoreAdmin::find()
  269. ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'username' => $params['shop_mobile']])
  270. ->one();
  271. if (!empty($admin_model) && $admin_model['store_id'] != $res->id) {
  272. $transaction->rollBack();
  273. self::$static_error = '门店管理员账号已经存在';
  274. return false;
  275. }
  276. $admin_model = new StoreAdmin();
  277. $admin_model->loadDefaultValues();
  278. $admin_model->attributes = $admin_info;
  279. $admin_model->updated_at = time();
  280. $admin_model->created_at = time();
  281. $res_ad = $admin_model->save();
  282. if (false === $res_ad) {
  283. $transaction->rollBack();
  284. self::$static_error = $admin_model->getErrorMessage();
  285. return false;
  286. }
  287. if ($params['is_admin']) {
  288. self::updateStoreServiceRatio($params['mall_id'], $res->id);
  289. }
  290. $cate = StoreCate::findOne(['id'=>$res['c_id']]);
  291. if(!empty($cate['is_collect_service_fee'])&&!empty($res['check_status'])&&$res['check_status']==1){
  292. $exist_setting = StoreSettings::find()
  293. ->where([
  294. 'store_id' => $res['id'],
  295. ])->one();
  296. $cashier_info = [
  297. 'store_service_fee' => $cate['is_collect_service_fee'],
  298. 'store_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'],
  299. 'cachier_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'],
  300. 'store_settle_type' => 1,
  301. 'store_settle_scene' =>1,
  302. 'store_id' => $res['id'],
  303. ];
  304. StoreSettings::setData($res->mall_id,$cashier_info);
  305. }
  306. // 设置让利
  307. if (!empty($callback)) {
  308. $ret = $callback->call(new self(), $res->id);
  309. if (is_string($ret)) {
  310. $transaction->rollBack();
  311. self::$static_error = $ret;
  312. return false;
  313. }
  314. }
  315. $transaction->commit();
  316. return $res;
  317. }
  318. public static function updateStoreServiceRatio($mall_id, $store_id)
  319. {
  320. $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
  321. if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) {
  322. StoreSettings::setData($mall_id, [
  323. 'store_id'=> $store_id,
  324. 'store_service_fee'=> 1,
  325. 'store_service_ratio'=> $basic['max_platform_ratio'],
  326. 'cachier_service_ratio'=> $basic['max_platform_ratio'],
  327. ]);
  328. }
  329. }
  330. /**
  331. * @name:
  332. * @msg: 装修页面获取门店列表
  333. * @param {int} $mall_id
  334. * @param {array} $params
  335. * @return {*}
  336. */
  337. public static function getStoreListInDiy(int $mall_id, array $params = [])
  338. {
  339. $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT];
  340. if (!empty($params['store_id'])) {
  341. $wheres[] = ['id' => $params['store_id']];
  342. }
  343. if (!empty($params['store_name'])) {
  344. $wheres[] = ['like', 'store_name', $params['store_name']];
  345. }
  346. if (!empty($params['storeowner'])) {
  347. $wheres[] = ['like', 'shop_owner', $params['storeowner']];
  348. }
  349. $params = [
  350. 'select' => '*,store_name as title,logo_img as img',
  351. 'where' => $wheres,
  352. 'order' => 'created_at desc',
  353. ];
  354. $store_list = self::listPage($params);
  355. if (false === $store_list) {
  356. return false;
  357. }
  358. return $store_list;
  359. }
  360. //获取门店更多
  361. public static function MoreStore(array $data)
  362. {
  363. $storeList = $data['store'];
  364. $mall_id = $data['mall_id'];
  365. $params = [
  366. 'select' => '*,store_name as title,logo_img as img',
  367. 'where' => "id in ($storeList) and status = 1 and check_status = 1 and mall_id = $mall_id and shop_status = 1",
  368. 'order' => 'created_at desc',
  369. ];
  370. $store_list = self::listpage($params);
  371. if (false === $store_list) {
  372. return false;
  373. }
  374. $decoration = \Yii::$app->services->setting->addons->get($mall_id, StoreEnum::ADDONS_NAME, 'decoration');
  375. foreach ($store_list['list'] as &$item){
  376. // 根据当前定位计算门店距离
  377. $item['distance'] = 0;
  378. if (!empty($data['longitude']) && !empty($data['latitude'])) {
  379. $distance = round(
  380. 2 * 6378.137 * ASIN(
  381. SQRT(
  382. POW(
  383. SIN(
  384. PI() * ($data['longitude'] - $item['longitude']) / 360
  385. ),
  386. 2
  387. ) + COS(PI() * $data['latitude'] / 180) * COS($item['latitude'] * PI() / 180) * POW(
  388. SIN(
  389. PI() * ($data['latitude'] - $item['latitude']) / 360
  390. ),
  391. 2
  392. )
  393. )
  394. )
  395. ,1);
  396. $item['distance'] = bcmul($distance, 1, 1);
  397. }
  398. if(!empty($decoration['custom_store_style'])){
  399. $item['store_style'] = $item['show_style']??1;
  400. }else{
  401. $item['store_style'] = $decoration['default_store_style']??1;
  402. }
  403. }
  404. return $store_list;
  405. }
  406. /**
  407. * 结算货款
  408. *
  409. * @param int $order_id
  410. * @param int $store_id
  411. * @param string $order_no
  412. * @param integer $is_settlement 是否已经结算订(用于虚拟订单直接结算, 虚拟订单生成的佣金记录是已结算的,所以需要传1查询)
  413. * @return void
  414. */
  415. public static function settlementStoreCommission($order_id, $store_id, $order_no, $is_settlement = 0)
  416. {
  417. $store = Store::findOne(['id' => $store_id]);
  418. $store_commission_model = StoreCommission::findOne(['order_no' => $order_no, 'is_settlement' => $is_settlement, 'status'=>StatusEnum::ENABLED]);
  419. if ($store_commission_model) {
  420. // 配置信息
  421. $storeSetting = StoreSettings::findOne(['store_id' => $store->id]);
  422. $order = StoreOrder::findOne(['id' => $order_id]);
  423. if ($storeSetting->store_service_fee == 2 && MallAddons::isInstall($store->mall_id,AddonsEnum::ADDONS_NAME_HUIFU_PAY) && $order->payment_type == PayTypeEnum::HUIFUPAY) {
  424. $store->share_amount += $store_commission_model->amount_received; // 已分帐金额
  425. } else {
  426. $store->balance += $store_commission_model->amount_received; // 余额
  427. $store->total_settled_amount += $store_commission_model->amount_received; // 已结算总金额
  428. }
  429. $store->unsettled_amount -= $store_commission_model->amount_received; // 未结算
  430. if ($store->unsettled_amount < 0) $store->unsettled_amount = 0;
  431. $res = $store->save();
  432. if ($res == false) throw new Exception('o2o订单完成结算货款,更新店铺结算金额失败,order_no:' . $order_no);
  433. $store_commission_model->is_settlement = 1;
  434. $res = $store_commission_model->save();
  435. if ($res == false) throw new Exception('o2o订单完成结算货款,更新提成记录失败,order_no:' . $order_no);
  436. StoreStatistics::updateIncomeMoney($store, $store_commission_model->amount_received);
  437. $key = StoreEnum::STORE_NOT_SETTLE_AMOUNTS.':'.$store['mall_id'];
  438. $res = Yii::$app->redis->get($key);
  439. $amount_received_temp = $store_commission_model->amount_received;
  440. if(!empty($res)) $amount_received_temp = bcsub($res,$store_commission_model->amount_received,2);
  441. if($amount_received_temp<0) $amount_received_temp = 0;
  442. Yii::$app->redis->set($key,$amount_received_temp);//待结算
  443. $amount_received_temp = $store_commission_model->amount_received;
  444. $key = StoreEnum::STORE_SETTLE_AMOUNTS . ':' . $store['mall_id'];
  445. $res = \Yii::$app->redis->get($key);
  446. if(!empty($res)) $amount_received_temp = bcadd($res,$store_commission_model->amount_received,2);
  447. Yii::$app->redis->set($key,$amount_received_temp);//已结算
  448. //结算积分
  449. if(!empty($store_commission_model['score_deduction_bear']) && $store_commission_model['score_deduction_bear']==1){
  450. self::settlementScore($order_no,$store['user_id'],$store_commission_model['id']);
  451. }
  452. }
  453. }
  454. /**
  455. * 结算积分
  456. * @param $order_no
  457. * @param $store_user_id
  458. * @param $source_table_id
  459. * @return void
  460. */
  461. public static function settlementScore($order_no,$store_user_id,$source_table_id)
  462. {
  463. $store_order = StoreOrder::findOne(['order_no'=>$order_no]);
  464. $store_order_detail_list = StoreOrderDetail::find()->where(['order_id'=>$store_order['id'],'is_feedback'=>0])->all();
  465. $order_detail_ids = array_column($store_order_detail_list,'id');
  466. $source_table = StoreOrderDetail::tableName();
  467. CapitalLog::$capitalType = 'score';
  468. $score_frozen_log_list = CapitalLog::lists(
  469. [
  470. 'where'=>[
  471. ['user_id'=>$store_order['user_id']],
  472. ['change_type'=>'sub'],
  473. ['source_table'=>$source_table],
  474. ['source_table_id'=>$order_detail_ids],
  475. ['from_type' => UserWalletEnum::CONSUME],
  476. ['capital_type'=>'consume_store_order'],
  477. ]
  478. ]
  479. );
  480. if(!empty($score_frozen_log_list)){
  481. $received_score = array_sum(array_column($score_frozen_log_list,'change_num'));
  482. if($received_score>0){
  483. $desc = '门店订单:'.$order_no.':积分结算' . $received_score;
  484. $data = [
  485. 'message_id' => 0,
  486. 'mall_id' => $store_order['mall_id'],
  487. 'user_id' => $store_user_id,
  488. 'wallet_type' => 'score',
  489. 'is_frozen' => false,
  490. 'unfrozen' => false,
  491. 'money' => $received_score,
  492. 'desc' => $desc,
  493. 'source_table' => StoreCommission::tableName(),
  494. 'source_table_id' => $source_table_id,
  495. 'from_type' => AddonsEnum::ADDONS_NAME_STORE,
  496. 'capital_type' => 'consume_store_order',
  497. 'order_no' => $order_no
  498. ];
  499. UserWalletService::handle(0, $data);
  500. }
  501. }
  502. }
  503. /**
  504. * 通过ID获取手机号码
  505. *
  506. * @param integer $id
  507. * @return string|null
  508. */
  509. public static function getMobileById(int $mall_id, int $id)
  510. {
  511. return static::find()
  512. ->where(['mall_id' => $mall_id, 'id' => $id])
  513. ->select('shop_mobile')
  514. ->scalar();
  515. }
  516. public static function delStore($mall_id,$store_id){
  517. $store = Store::findOne(['id'=>$store_id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  518. if(empty($store)) throw new \Exception('门店不存在');
  519. if($store['balance']>0) throw new \Exception('店铺余额不为0,请先提现后再删除');
  520. $store_order = StoreOrder::getOne([
  521. ['store_id'=>$store_id],
  522. ['order_status'=>[OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING]],
  523. ['is_feedback'=>[0,2]]
  524. ]);
  525. if(!empty($store_order)) throw new \Exception('请等订单完成后再删除');
  526. $t = Yii::$app->db->beginTransaction();
  527. try {
  528. $store->status = StatusEnum::DELETE;
  529. $res = $store->save();
  530. if(empty($res)) throw new \Exception('删除失败');
  531. StoreAdmin::updateAll(['status'=>StatusEnum::DELETE],['store_id'=>$store_id,'mall_id'=>$mall_id]);
  532. StoreMerchantReduceProfit::updateAll(['status' => StatusEnum::DELETE],
  533. ['source_id' => $store_id, 'mall_id' => $mall_id, 'source' => StoreEnum::ADDONS_NAME, 'status' => StatusEnum::ENABLED]);
  534. StoreAssociatedUser::updateAll(['status'=>StatusEnum::DELETE],['store_id'=>$store_id, 'mall_id'=>$mall_id]);
  535. $t->commit();
  536. } catch (\Exception $e) {
  537. $t->rollBack();
  538. throw new \Exception($e->getMessage());
  539. }
  540. return true;
  541. }
  542. public static function fixData(){
  543. $mall_list = Mall::getEnableMallList(true);
  544. $mall_ids = array_column($mall_list, 'id');
  545. if (empty($mall_ids)) {
  546. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  547. return ExitCode::UNSPECIFIED_ERROR;
  548. }
  549. foreach ($mall_ids as $mall_id) {
  550. $key = StoreEnum::STORE_NOT_SETTLE_AMOUNTS . ':' . $mall_id;
  551. $not_settle_amounts = StoreCommission::find()->where(['mall_id' =>$mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 0])->sum('amount_received');
  552. if(empty($not_settle_amounts)) $not_settle_amounts = 0;
  553. \Yii::$app->redis->set($key,$not_settle_amounts);
  554. $key = StoreEnum::STORE_SETTLE_AMOUNTS . ':' . $mall_id;
  555. $settle_amounts = StoreCommission::find()->where(['mall_id' =>$mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 1])->sum('amount_received');
  556. if(empty($settle_amounts)) $settle_amounts = 0;
  557. \Yii::$app->redis->set($key,$settle_amounts);
  558. var_dump($mall_id);
  559. }
  560. }
  561. /**
  562. * 门店自提码设置
  563. *
  564. * @param integer $store_id
  565. * @return array
  566. */
  567. public static function getStorePickup(int $store_id)
  568. {
  569. $pickup = static::find()
  570. ->where(['id' => $store_id])
  571. ->select('pickup')
  572. ->scalar();
  573. return empty($pickup) ? [] : json_decode($pickup, true);
  574. }
  575. public function getCate()
  576. {
  577. return $this->hasOne(StoreCate::class, ['id' => 'c_id']);
  578. }
  579. /**
  580. * @param integer $mall_id
  581. * @return array
  582. */
  583. public static function getStoreList(int $mall_id)
  584. {
  585. return static::find()
  586. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  587. ->select('id, store_name')
  588. ->asArray()
  589. ->all();
  590. }
  591. }