| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace addons\Store\common\services;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreAdmin;
- use addons\Store\common\models\StoreCashierOrder;
- use addons\Store\common\models\StoreCoupon;
- use addons\Store\common\models\StoreCouponUserRelate;
- use addons\Store\common\models\StoreClerk;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreSettings;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\common\Region;
- use common\models\user\Town;
- use addons\Store\common\models\StoreCommunity;
- use addons\Store\common\models\StoreMiniCode;
- /**
- * 门店service
- * @package addons\Store\common\services
- */
- class StoreService extends BaseService
- {
- /**
- * 发送验证码
- *
- * @return boolean
- */
- public function sendCaptcha()
- {
- $mobile = Store::getMobileById($this->mall_id, $this->store_id);
- $sms = new Sms($this->mall_id, 'domestic');
- // 发送验证码
- try {
- if ($sms->sendCaptcha($mobile, 86, 'update_store_pass') === false) {
- return $this->error('发送失败');
- }
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return true;
- }
- /**
- * 修改密码
- *
- * @param string $captcha
- * @param string $pwd
- * @return boolean
- */
- public function changePwd(string $captcha, string $pwd)
- {
- $mobile = Store::getMobileById($this->mall_id, $this->store_id);
- $sms = new Sms($this->mall_id, 'domestic');
- try {
- if (!$sms->checkValidateCode($mobile, $captcha, 'update_store_pass')) {
- return $this->error('验证码错误');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- $store_admin = StoreAdmin::getAdminByStoreId($this->mall_id, $this->store_id);
- return $store_admin->changePwd($pwd);
- }
- /**
- * 获取全部门店列表
- *
- * @return array
- */
- public function getStoreList()
- {
- return Store::getStoreList($this->mall_id);
- }
- public function updateSalesVolume($store_id=null){
- $sql = "SELECT * FROM qimall_addons_store where status in(0,1)";
- if(!empty($store_id)){
- $sql.=" and id = ".$store_id;
- }
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- if ($list) {
- foreach ($list as $item) {
- $sales_num = StoreGoods::find()->where(['mall_id'=>$item['mall_id'],'store_id'=>$item['id'], 'status' => StatusEnum::ENABLED])->sum('sales_num+virtual_sales');
- $sales_volume2 = StoreCashierOrder::find()->where(['mall_id'=>$item['mall_id'],'store_id'=>$item['id'], 'status' => StatusEnum::ENABLED])
- ->andWhere(['>','pay_time',0])
- ->count();
- $sales_volume = 0;
- if(!empty($sales_num)){
- $sales_volume+=$sales_num;
- }
- if(!empty($sales_volume2)){
- $sales_volume+=$sales_volume2;
- }
- \Yii::$app->db->createCommand()->update('qimall_addons_store', [
- 'sales_volume' => $sales_volume], ['id' => $item['id']])->execute();
- }
- }
- }
- /**
- * 检测营业时间
- * @Author:lun
- * @Date:2024-07-02 19:07
- * @Copyright:copyright(c)2024 广东七件事集团
- * @param $store
- * @param null $scene
- * @return string|void
- * @throws \Exception
- */
- public static function checkBusinessTime($store, $scene = null)
- {
- if (empty($store['shop_status'])) {// 店铺未开启营业
- $message = '店铺暂未开始营业哦!';
- if ($scene == 'front') return $message;
- throw new \Exception($message);
- }
- $business_time_start = $store['business_time_start'] ?? '00:00';
- $business_time_end = $store['business_time_end'] ?? '00:00';
- if ($business_time_start == '00:00' && $business_time_end == '00:00') {
- $message = '营业时间未设置!';
- if ($scene == 'front') return $message;
- throw new \Exception('营业时间未设置');
- }
- // 将开始时间设置为当天。结束时间默认比开始时间大所以设置为第二天
- $business_time_start = strtotime($business_time_start);
- $business_time_end = strtotime($business_time_end);
- $currentTime = time();
- if ($business_time_end < $business_time_start) { // 当结束时间比开始时间小时,则将结束营业时间设置为第二天
- $business_time_end = strtotime('+1 day', $business_time_end);
- // 同时判断当前时间是否小于开始时间,如果小于则开始时间也添加到上一天
- if ($currentTime < $business_time_start) {
- $currentTime = strtotime('+1 day', $currentTime);
- }
- }
- $isLnTimeRange = (
- ($currentTime >= $business_time_start && $currentTime < $business_time_end)
- );
- if (!$isLnTimeRange) {
- if ($scene == 'front') return '店铺休息中';//前端接口使用
- throw new \Exception('当前时间不在营业时间内!');
- }
- }
- /**
- * 获取门店客服配置信息
- * @return array|\yii\db\ActiveRecord
- */
- public static function getCustomer(int $mall_id, int $store_id)
- {
- $setting = StoreSettings::find()
- ->select(['is_customer', 'customer_types'])->where(['mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'status' => StatusEnum::ENABLED]
- )->asArray()->one();
- if (!empty($setting['customer_types'])){
- $setting['customer_types'] = json_decode($setting['customer_types'], true);
- } else {
- $setting['customer_types'] = [];
- }
- return $setting;
- }
- public function getMoreStore($params)
- {
- if(empty($params) || empty($params['store']) || empty($params['mall_id'])){
- return [];
- }
- //模型查询
- $list = Store::MoreStore($params);
- return $list;
- }
- public function getDraftRegionInfo(array $params)
- {
- $province_id = $params['province_id']??0;
- $city_id = $params['city_id']??0;
- $district_id = $params['district_id']??0;
- $street_id = $params['street_id']??0;
- $provinceInfo = Region::findOne(['id'=>$province_id,'level'=>1]);
- $cityInfo = Region::findOne(['id'=>$city_id,'level'=>2]);
- $districtInfo = Region::findOne(['id'=>$district_id,'level'=>3]);
- $streetInfo = Town::findOne(['id'=>$street_id]);
- $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]);
- $params['province_name'] = $provinceInfo->name??'';
- $params['city_name'] = $cityInfo->name??'';
- $params['district_name'] = $districtInfo->name??'';
- $params['street_name'] = $streetInfo->name??'';
- $params['community_name'] = $communityInfo->community_name??'';
- return $params;
- }
- /**
- * 根据后台登录的user_id找到核销员id,如果不是核销员就找门店的店主的核销员id
- *
- * @param int $userId
- * @param bool $isStoreUserId 表示传过来的是不是门店的user_id
- *
- * @return int
- */
- public function getClerkIdByUserId(int $userId, bool $isStoreUserId = false): int
- {
- if (!$userId) {
- return 0;
- }
- $where = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED,
- ];
- $id = StoreClerk::find()->where(array_merge(['user_id' => $userId], $where))->select('id')->scalar();
- if ($id) {
- return $id;
- }
- if ($isStoreUserId) {
- return 0;
- }
- $storeUserId = Store::find()->where(['id' => $this->store_id])->select('user_id')->scalar();
- if (!$storeUserId) {
- return 0;
- }
- return StoreClerk::find()->where(array_merge(['user_id' => $storeUserId], $where))->select('id')->scalar() ?: 0;
- }
- public function updateIsShowCoupon($params)
- {
- $store = Store::findOne(['id'=>$this->store_id]);
- $store->is_show_coupon = $params['is_show_coupon'];
- return $store->save();
- }
- /**
- * 清除小程序码
- *
- * @return boolean
- */
- public function cleanMiniCode()
- {
- if (StoreMiniCode::cleanMiniCodeByMallId($this->mall_id)) {
- return true;
- } else {
- return $this->error(StoreMiniCode::getStaticError());
- }
- }
- }
|