StoreService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Store\common\models\Store;
  5. use addons\Store\common\models\StoreAdmin;
  6. use addons\Store\common\models\StoreCashierOrder;
  7. use addons\Store\common\models\StoreCoupon;
  8. use addons\Store\common\models\StoreCouponUserRelate;
  9. use addons\Store\common\models\StoreClerk;
  10. use addons\Store\common\models\StoreGoods;
  11. use addons\Store\common\models\StoreSettings;
  12. use common\enums\StatusEnum;
  13. use common\helpers\sms\Sms;
  14. use common\models\common\Region;
  15. use common\models\user\Town;
  16. use addons\Store\common\models\StoreCommunity;
  17. use addons\Store\common\models\StoreMiniCode;
  18. /**
  19. * 门店service
  20. * @package addons\Store\common\services
  21. */
  22. class StoreService extends BaseService
  23. {
  24. /**
  25. * 发送验证码
  26. *
  27. * @return boolean
  28. */
  29. public function sendCaptcha()
  30. {
  31. $mobile = Store::getMobileById($this->mall_id, $this->store_id);
  32. $sms = new Sms($this->mall_id, 'domestic');
  33. // 发送验证码
  34. try {
  35. if ($sms->sendCaptcha($mobile, 86, 'update_store_pass') === false) {
  36. return $this->error('发送失败');
  37. }
  38. }catch (\Exception $e){
  39. return $this->error($e->getMessage());
  40. }
  41. return true;
  42. }
  43. /**
  44. * 修改密码
  45. *
  46. * @param string $captcha
  47. * @param string $pwd
  48. * @return boolean
  49. */
  50. public function changePwd(string $captcha, string $pwd)
  51. {
  52. $mobile = Store::getMobileById($this->mall_id, $this->store_id);
  53. $sms = new Sms($this->mall_id, 'domestic');
  54. try {
  55. if (!$sms->checkValidateCode($mobile, $captcha, 'update_store_pass')) {
  56. return $this->error('验证码错误');
  57. }
  58. } catch (\Exception $e) {
  59. return $this->error($e->getMessage());
  60. }
  61. $store_admin = StoreAdmin::getAdminByStoreId($this->mall_id, $this->store_id);
  62. return $store_admin->changePwd($pwd);
  63. }
  64. /**
  65. * 获取全部门店列表
  66. *
  67. * @return array
  68. */
  69. public function getStoreList()
  70. {
  71. return Store::getStoreList($this->mall_id);
  72. }
  73. public function updateSalesVolume($store_id=null){
  74. $sql = "SELECT * FROM qimall_addons_store where status in(0,1)";
  75. if(!empty($store_id)){
  76. $sql.=" and id = ".$store_id;
  77. }
  78. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  79. if ($list) {
  80. foreach ($list as $item) {
  81. $sales_num = StoreGoods::find()->where(['mall_id'=>$item['mall_id'],'store_id'=>$item['id'], 'status' => StatusEnum::ENABLED])->sum('sales_num+virtual_sales');
  82. $sales_volume2 = StoreCashierOrder::find()->where(['mall_id'=>$item['mall_id'],'store_id'=>$item['id'], 'status' => StatusEnum::ENABLED])
  83. ->andWhere(['>','pay_time',0])
  84. ->count();
  85. $sales_volume = 0;
  86. if(!empty($sales_num)){
  87. $sales_volume+=$sales_num;
  88. }
  89. if(!empty($sales_volume2)){
  90. $sales_volume+=$sales_volume2;
  91. }
  92. \Yii::$app->db->createCommand()->update('qimall_addons_store', [
  93. 'sales_volume' => $sales_volume], ['id' => $item['id']])->execute();
  94. }
  95. }
  96. }
  97. /**
  98. * 检测营业时间
  99. * @Author:lun
  100. * @Date:2024-07-02 19:07
  101. * @Copyright:copyright(c)2024 广东七件事集团
  102. * @param $store
  103. * @param null $scene
  104. * @return string|void
  105. * @throws \Exception
  106. */
  107. public static function checkBusinessTime($store, $scene = null)
  108. {
  109. if (empty($store['shop_status'])) {// 店铺未开启营业
  110. $message = '店铺暂未开始营业哦!';
  111. if ($scene == 'front') return $message;
  112. throw new \Exception($message);
  113. }
  114. $business_time_start = $store['business_time_start'] ?? '00:00';
  115. $business_time_end = $store['business_time_end'] ?? '00:00';
  116. if ($business_time_start == '00:00' && $business_time_end == '00:00') {
  117. $message = '营业时间未设置!';
  118. if ($scene == 'front') return $message;
  119. throw new \Exception('营业时间未设置');
  120. }
  121. // 将开始时间设置为当天。结束时间默认比开始时间大所以设置为第二天
  122. $business_time_start = strtotime($business_time_start);
  123. $business_time_end = strtotime($business_time_end);
  124. $currentTime = time();
  125. if ($business_time_end < $business_time_start) { // 当结束时间比开始时间小时,则将结束营业时间设置为第二天
  126. $business_time_end = strtotime('+1 day', $business_time_end);
  127. // 同时判断当前时间是否小于开始时间,如果小于则开始时间也添加到上一天
  128. if ($currentTime < $business_time_start) {
  129. $currentTime = strtotime('+1 day', $currentTime);
  130. }
  131. }
  132. $isLnTimeRange = (
  133. ($currentTime >= $business_time_start && $currentTime < $business_time_end)
  134. );
  135. if (!$isLnTimeRange) {
  136. if ($scene == 'front') return '店铺休息中';//前端接口使用
  137. throw new \Exception('当前时间不在营业时间内!');
  138. }
  139. }
  140. /**
  141. * 获取门店客服配置信息
  142. * @return array|\yii\db\ActiveRecord
  143. */
  144. public static function getCustomer(int $mall_id, int $store_id)
  145. {
  146. $setting = StoreSettings::find()
  147. ->select(['is_customer', 'customer_types'])->where(['mall_id' => $mall_id,
  148. 'store_id' => $store_id,
  149. 'status' => StatusEnum::ENABLED]
  150. )->asArray()->one();
  151. if (!empty($setting['customer_types'])){
  152. $setting['customer_types'] = json_decode($setting['customer_types'], true);
  153. } else {
  154. $setting['customer_types'] = [];
  155. }
  156. return $setting;
  157. }
  158. public function getMoreStore($params)
  159. {
  160. if(empty($params) || empty($params['store']) || empty($params['mall_id'])){
  161. return [];
  162. }
  163. //模型查询
  164. $list = Store::MoreStore($params);
  165. return $list;
  166. }
  167. public function getDraftRegionInfo(array $params)
  168. {
  169. $province_id = $params['province_id']??0;
  170. $city_id = $params['city_id']??0;
  171. $district_id = $params['district_id']??0;
  172. $street_id = $params['street_id']??0;
  173. $provinceInfo = Region::findOne(['id'=>$province_id,'level'=>1]);
  174. $cityInfo = Region::findOne(['id'=>$city_id,'level'=>2]);
  175. $districtInfo = Region::findOne(['id'=>$district_id,'level'=>3]);
  176. $streetInfo = Town::findOne(['id'=>$street_id]);
  177. $communityInfo = StoreCommunity::findOne(['mall_id'=>$params['mall_id'],'province_id'=>$params['province_id'],'city_id'=>$params['city_id'],'street_id'=>$params['street_id'],'status'=>0]);
  178. $params['province_name'] = $provinceInfo->name??'';
  179. $params['city_name'] = $cityInfo->name??'';
  180. $params['district_name'] = $districtInfo->name??'';
  181. $params['street_name'] = $streetInfo->name??'';
  182. $params['community_name'] = $communityInfo->community_name??'';
  183. return $params;
  184. }
  185. /**
  186. * 根据后台登录的user_id找到核销员id,如果不是核销员就找门店的店主的核销员id
  187. *
  188. * @param int $userId
  189. * @param bool $isStoreUserId 表示传过来的是不是门店的user_id
  190. *
  191. * @return int
  192. */
  193. public function getClerkIdByUserId(int $userId, bool $isStoreUserId = false): int
  194. {
  195. if (!$userId) {
  196. return 0;
  197. }
  198. $where = [
  199. 'mall_id' => $this->mall_id,
  200. 'store_id' => $this->store_id,
  201. 'status' => StatusEnum::ENABLED,
  202. ];
  203. $id = StoreClerk::find()->where(array_merge(['user_id' => $userId], $where))->select('id')->scalar();
  204. if ($id) {
  205. return $id;
  206. }
  207. if ($isStoreUserId) {
  208. return 0;
  209. }
  210. $storeUserId = Store::find()->where(['id' => $this->store_id])->select('user_id')->scalar();
  211. if (!$storeUserId) {
  212. return 0;
  213. }
  214. return StoreClerk::find()->where(array_merge(['user_id' => $storeUserId], $where))->select('id')->scalar() ?: 0;
  215. }
  216. public function updateIsShowCoupon($params)
  217. {
  218. $store = Store::findOne(['id'=>$this->store_id]);
  219. $store->is_show_coupon = $params['is_show_coupon'];
  220. return $store->save();
  221. }
  222. /**
  223. * 清除小程序码
  224. *
  225. * @return boolean
  226. */
  227. public function cleanMiniCode()
  228. {
  229. if (StoreMiniCode::cleanMiniCodeByMallId($this->mall_id)) {
  230. return true;
  231. } else {
  232. return $this->error(StoreMiniCode::getStaticError());
  233. }
  234. }
  235. }