$this->mall_id], ['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED] ]; $list = HappinessHistoryStore::lists([ 'where' => $where, 'order' => 'id desc', 'with' => ['store'], ]); if (!empty($list)) { $today = date('Y-m-d', time()); foreach ($list as $key => &$item) { if (empty($item['store'])) { unset($list[$key]); continue; } if ($item['store']['shop_status'] == 0) { unset($list[$key]); continue; } if (!empty($params['longitude']) && !empty($params['longitude']) && !empty($item['store'])) { $distance = round( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ($params['longitude'] - $item['store']['longitude']) / 360 ), 2 ) + COS(PI() * $params['latitude'] / 180) * COS($item['store']['latitude'] * PI() / 180) * POW( SIN( PI() * ($params['latitude'] - $item['store']['latitude']) / 360 ), 2 ) ) ) ,1); $item['store']['distance'] = bcmul($distance, 1, 1); $item['distance'] = bcmul($distance, 1, 1); $business_time_start = $today. ' ' . $item['business_time_start']; $business_time_end = $today. ' ' . $item['business_time_end']; if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) $item['disabled'] = true; $item['store']['disabled'] = false; $item['store']['intra_city_distribution'] = json_decode($item['store']['intra_city_distribution'], true); // if ($item['store']['intra_city_distribution']['is_alone'] == 1) { // if (!empty($item['store']['intra_city_distribution']['distribution_scope']) && $item['store']['intra_city_distribution']['distribution_scope'] < $item['store']['distance']) $item['store']['disabled'] = true; // } else { // //系统配置 // $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic'); // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $item['store']['distance']) $item['store']['disabled'] = true; // } } } $field = 'distance'; usort($list, function($a, $b) use($field) { return $a[$field] <=> $b[$field]; }); } return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //删除用户历史自提门店数据 public function delHistoryStore($params) { try { //查询是否存在该历史数据 $info = HappinessHistoryStore::getOne([ ['mall_id' => $this->mall_id], ['user_id' => $this->user_id], ['id' => $params['id']], ['status' => StatusEnum::ENABLED], ]); if (empty($info)) throw new Exception('不存在该历史数据'); $info->status = StatusEnum::DELETE; if (!$info->save()) throw new Exception('操作失败'); return true; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //获取门店详情 public function getStoreDetail($params) { try { $where = [ ['mall_id' => $this->mall_id], ['id' => $params['store_id']], ['status' => StatusEnum::ENABLED], ]; $info = HappinessStore::getOne($where, true); if (empty($info)) { return null; } else { $info['intra_city_distribution'] = json_decode($info['intra_city_distribution'], true); //获取门店商品销量 $sale_data = HappinessStoreGoods::lists([ 'where' => [ ['mall_id' => $this->mall_id], ['store_id' => $params['store_id']], ], ]); $sales_num = 0; //总销量 if (!empty($sale_data)) { $sales_num = array_sum(array_column($sale_data, 'sales_num')); } $info['sales_num'] = $sales_num; //计算距离 if (!empty($params['longitude']) && !empty($params['latitude'])) { $distance = round( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ($params['longitude'] - $info['longitude']) / 360 ), 2 ) + COS(PI() * $params['latitude'] / 180) * COS($info['latitude'] * PI() / 180) * POW( SIN( PI() * ($params['latitude'] - $info['latitude']) / 360 ), 2 ) ) ) ,1); $info['distance'] = bcmul($distance, 1, 1); $group_order_fee = 0;//配送费 if ($info['intra_city_distribution']['is_alone'] == 1) { //当前门店是否免配送费 if ($info['intra_city_distribution']['delivery_fee'] == 0) { $group_order_fee += 0; } else { $group_order_fee += $info['intra_city_distribution']['distance_charge']; //如果配送距离超出初始配送距离,则需累计计算每次增加的公里数对应的配送费 if ($info['intra_city_distribution']['distance'] < $info['distance']) { $distance = $info['distance'] - $info['intra_city_distribution']['distance'] ; if ($info['intra_city_distribution']['every_distance'] > 0) { $group_order_fee += ceil($distance/$info['intra_city_distribution']['every_distance']) * $info['intra_city_distribution']['every_distance_charge']; } } } } else { //获取幸福小店插件基础配置 $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic'); //基础配置是否免配送 if ($setting['delivery_fee'] == 0) { $group_order_fee += 0; } else { $group_order_fee += $info['intra_city_distribution']['start_amount']; if ($setting['start_limit'] < $info['distance']) { $distance = $info['distance'] - $setting['start_limit']; if ($setting['add_limit'] > 0) { $group_order_fee += ceil($distance/$setting['add_limit']) * $setting['add_amount']; } } } } $info['shipping_money'] = $group_order_fee; } } return $info; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //获取最近距离的门店 public function getStore($params) { try { if (empty($params['longitude']) || empty($params['latitude'])) return []; $status = StatusEnum::ENABLED; $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})"; if (!empty($params['goods_ids'])) { $goods_ids = implode(',', $params['goods_ids']); $where .= " AND yg.goods_id in ({$goods_ids})"; } //传入单个规格字段时 if (!empty($params['attr_id'])) $where .= " AND yg.attr_id = {$params['attr_id']}"; //传入多规格字段时 if (!empty($params['attr_ids'])) { $attr_ids = implode(',', $params['attr_ids']); $where .= " AND yg.attr_id in ({$attr_ids})"; } //根据订单快递类型,获取门店 if (!empty($params['shipping_type'])) { switch ($params['shipping_type']) { case 2: $where .= " AND s.self_mention = 1"; break; case 3: $where .= " AND s.merchant_distribution = 1"; break; } } //系统配置 $setting = \Yii::$app->services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic'); $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( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ({$params['longitude']} - s.longitude) / 360 ), 2 ) + COS(PI() * {$params['latitude']} / 180) * COS(s.latitude * PI() / 180) * POW( SIN( PI() * ({$params['latitude']} - s.latitude) / 360 ), 2 ) ) ) ,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"; $sql .= $where; $sql .= ' GROUP BY s.id'; if (!empty($params['shipping_type']) && $params['shipping_type'] == 3) { $sql .= " HAVING distance <= s.distribution_scope"; } else { if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit'])) { $sql .= " HAVING distance <= ". $setting['localtion_limit']; } } $page = 1; $limit = 20; $sql .= " ORDER BY distance asc"; $sql .= " limit " . ($page - 1) * $limit . ",{$limit}"; $info = \Yii::$app->db->createCommand($sql)->queryAll(); if (empty($info)) { return null; } else { foreach ($info as $key => $value) { $today = date('Y-m-d', time()); $business_time_start = $today. ' ' . $value['business_time_start']; $business_time_end = $today. ' ' . $value['business_time_end']; if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) continue; $value['intra_city_distribution'] = json_decode($value['intra_city_distribution'], true); // if (!isset($params['shipping_type']) || $params['shipping_type'] == 3) { // if ($value['intra_city_distribution']['is_alone'] == 1) { // if (!empty($value['intra_city_distribution']['distribution_scope']) && $value['intra_city_distribution']['distribution_scope'] < $value['distance']) continue; // } else { // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $value['distance']) continue; // } // } return $value; } return null; } } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function getStoreList($params) { try { if (empty($params['longitude']) || empty($params['latitude'])) return []; $page = $params['page']??1; $limit = $params['limit']??15; $status = StatusEnum::ENABLED; $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})"; // if (!empty($params['goods_ids'])) { // $goods_ids = implode(',', $params['goods_ids']); // $where .= " AND yg.goods_id in ({$goods_ids})"; // } //传入单个规格字段时 //if (!empty($params['attr_id'])) $where .= " AND yg.attr_id = {$params['attr_id']}"; //传入多规格字段时 // if (!empty($params['attr_ids'])) { // $attr_ids = implode(',', $params['attr_ids']); // $where .= " AND yg.attr_id in ({$attr_ids})"; // } if (!empty($params['keyword'])) { $where .= " AND (s.store_name like '%". $params['keyword'] ."%' or s.address like '%". $params['keyword'] ."%')"; } //根据订单快递类型,获取门店 if (!empty($params['shipping_type'])) { switch ($params['shipping_type']) { case 2: $where .= " AND s.self_mention = 1"; break; case 3: $where .= " AND s.merchant_distribution = 1"; break; } } //系统配置 $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic'); $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( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ({$params['longitude']} - s.longitude) / 360 ), 2 ) + COS(PI() * {$params['latitude']} / 180) * COS(s.latitude * PI() / 180) * POW( SIN( PI() * ({$params['latitude']} - s.latitude) / 360 ), 2 ) ) ) ,1) AS distance from qimall_addons_happiness_store as s"; $sql .= $where; $sql .= ' GROUP BY s.id'; if (!empty($params['shipping_type']) && $params['shipping_type'] == 3) { $sql .= " HAVING distance <= s.distribution_scope"; } else { if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit'])) { $sql .= " HAVING distance <= ". $setting['localtion_limit']; } } $sql .= " ORDER BY distance asc"; $count_list = \Yii::$app->db->createCommand($sql)->queryAll(); $count = count($count_list); $sql .= " limit " . ($page - 1) * $limit . ",{$limit}"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); if($list===false) { throw new Exception('查询失败'); } $today = date('Y-m-d', time()); if (!empty($list)) { foreach ($list as &$item) { $item['disabled'] = false; //$item['intra_city_distribution'] = json_decode($item['intra_city_distribution'], true); $business_time_start = $today. ' ' . $item['business_time_start']; $business_time_end = $today. ' ' . $item['business_time_end']; if (strtotime($business_time_start) > time() || strtotime($business_time_end) < time()) $item['disabled'] = true; // if ($item['intra_city_distribution']['is_alone'] == 1) { // if (!empty($item['intra_city_distribution']['distribution_scope']) && $item['intra_city_distribution']['distribution_scope'] < $item['distance']) $item['disabled'] = true; // } else { // if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $item['distance']) $item['disabled'] = true; // } } } $list_page['list'] = $list; $list_page['page'] = $page; $list_page['page_size'] = $limit; $list_page['count'] = $count; $list_page['page_count'] = ceil($count / $limit); return $list_page; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //订单初始化,获取门店 public function selectStore($params) { try { if (empty($params['longitude']) || empty($params['latitude'])) return null; //如果存在门店id,则直接获取门店数据 if (!empty($params['store_id'])) { $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'; $where = [ ['mall_id' => $params['mall_id']], ['status' => StatusEnum::ENABLED], ['id' => $params['store_id']], ]; $info = HappinessStore::getOne($where, true, [], 'id desc', $select); if (!empty($info)) { $distance = round( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ($params['longitude'] - $info['longitude']) / 360 ), 2 ) + COS(PI() * $params['latitude'] / 180) * COS($info['latitude'] * PI() / 180) * POW( SIN( PI() * ($params['latitude'] - $info['latitude']) / 360 ), 2 ) ) ) ,1); $info['distance'] = bcmul($distance, 1, 1); $info['intra_city_distribution'] = json_decode($info['intra_city_distribution'], true); } } else { //如果不存在门店id,则获取距离最近的门店 $data = $this->getStore($params); $info = $data; } $info['disabled'] = false; if (!empty($info['intra_city_distribution']) && $params['shipping_type'] == 3) { //门店独立配置 if ($info['intra_city_distribution']['is_alone'] == 1) { if (!empty($info['intra_city_distribution']['distribution_scope']) &&$info['intra_city_distribution']['distribution_scope'] < $info['distance']) $info['disabled'] = true; $info['delivery_amount'] = $info['intra_city_distribution']['initial_delivery_amount']; } else { //系统配置 $setting = \Yii::$app->services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic'); if ($setting['is_location_limit'] == 1 && !empty($setting['localtion_limit']) && $setting['localtion_limit'] < $info['distance']) $info['disabled'] = true; $info['setting'] = $setting; $info['delivery_amount'] = $setting['delivery_amount']; } } return $info; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function getIncomeLogList(array $get, int $pageSize): array { $where[] = ['mall_id' => $this->mall_id]; $where[] = ['store_id' => $this->store_id]; $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]; // 是否没有提交筛选条件 (用于决定总数直接从store表拿还是要重新查询统计) $whereIsEmpty = true; if (isset($get['keyword']) && $get['keyword'] !== '') { $whereIsEmpty = false; $user_list = User::lists([ 'where' => [ ['mall_id' => $this->mall_id], [ 'or', ['like', 'username', trim($get['keyword'])], ['like', 'nickname', trim($get['keyword'])], ['like', 'mobile', trim($get['keyword'])] ] ], 'select' => 'id' ]); $where[] = ['user_id' => array_column($user_list, 'id')]; } if (!empty($get['user_id'])) { $whereIsEmpty = false; $where[] = ['user_id' => $get['user_id']]; } if (isset($get['order_no']) && $get['order_no'] !== '') { $whereIsEmpty = false; $where[] = ['order_no' => $get['order_no']]; } if (isset($get['is_settlement']) && $get['is_settlement'] !== '') { $whereIsEmpty = false; $where[] = ['is_settlement' => $get['is_settlement']]; } if (!empty($get['date'])) { $whereIsEmpty = false; $get['date'][1] .= ' 23:59:59'; array_walk($get['date'], function (&$item) { $item = strtotime($item); }); $where[] = ['between', 'created_at', ...$get['date']]; } $params = [ 'where' => $where, 'order' => 'id desc', 'with' => ['user'], 'limit' => $get['limit'] ?? $pageSize, ]; $page_data = HappinessStoreCommission::listPage($params); $total_num = $total_order_money = $total_settled_amount = $unsettled_amount = 0; if ($page_data) { $store = HappinessStore::findOne(['id' => $this->store_id]); // 如果所有搜索条件为空则显示这个,否则需要查询 if ($whereIsEmpty) { if ($store) { $commissionTotal = $store; } } else { // 按搜索结果 sum $commissionTotalField = [ 'COUNT(*) `total_num`', 'SUM(`pay_money`) `total_order_money`', 'SUM(IF(`is_settlement` = 1, `amount_received`, 0)) `total_settled_amount`', 'SUM(IF(`is_settlement` = 0, `amount_received`, 0)) `unsettled_amount`' ]; $commissionTotal = HappinessStoreCommission::getOne($where, true, [], '', $commissionTotalField); } if (isset($commissionTotal)) { $total_num = $commissionTotal['total_num']; $total_order_money = $commissionTotal['total_order_money']; $total_settled_amount = $commissionTotal['total_settled_amount']; $unsettled_amount = $commissionTotal['unsettled_amount']; } if (!empty($page_data['list'])) { $user_id_arr = array_column($page_data['list'], 'user_id'); $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'index' => 'id']); $order_no_arr = array_column($page_data['list'], 'order_no'); foreach ($page_data['list'] as &$item) { if ($item['type'] == 2) $order_no_arr[] = $item['order_no']; } foreach ($page_data['list'] as &$item) { $item['nickname'] = $item['mobile'] = $item['avatar_url'] = ''; if (isset($user_list[$item['user_id']])) { $item['nickname'] = $user_list[$item['user_id']]['nickname']; $item['mobile'] = $user_list[$item['user_id']]['mobile']; $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url']; } if ($item['type'] == 2) { if (isset($store_cashier_order_list[$item['order_no']])) { $addons_bonus = 0; $extra = json_decode($store_cashier_order_list[$item['order_no']]['extra'], true); if (!empty($extra)) { foreach ($extra as $k => $v) { if (in_array($k, ['agent_commission', 'distribution_commission', 'boss_commission', 'area_commission'])) $addons_bonus += $v; } } $item['addons_bonus'] = bcadd($addons_bonus, 0, 2); } } if (empty($store['payment_settlement_type'])) { $item['is_show'] = 0; } else { $item['is_show'] = 1; if (isset($check_list[$item['id']])) { $item['is_show'] = 0; } } } } } $order_total_arr = []; $order_total_arr[] = ['type' => 'total_num', 'name' => '总数', 'value' => $total_num ?: 0, 'prompt' => '']; $order_total_arr[] = ['type' => 'total_order_money', 'name' => '订单总金额', 'value' => $total_order_money ?: 0, 'prompt' => '']; $order_total_arr[] = ['type' => 'total_settled_amount', 'name' => '已结算总金额', 'value' => $total_settled_amount ?: 0, 'prompt' => '']; $order_total_arr[] = ['type' => 'unsettled_amount', 'name' => '未结算总金额', 'value' => $unsettled_amount ?: 0, 'prompt' => '']; $page_data['order_total_arr'] = $order_total_arr; $page_data['store_name'] = isset($store) ? $store['store_name'] : ''; return $page_data; } 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 = HappinessClerk::find()->where(array_merge(['user_id' => $userId], $where))->select('id')->scalar(); if ($id) { return $id; } if ($isStoreUserId) { return 0; } $storeUserId = HappinessStore::find()->where(['id' => $this->store_id])->select('user_id')->scalar(); if (!$storeUserId) { return 0; } return HappinessClerk::find()->where(array_merge(['user_id' => $storeUserId], $where))->select('id')->scalar() ?: 0; } /** * 发送验证码 * * @return boolean */ public function sendCaptcha() { $mobile = HappinessStore::getMobileById($this->mall_id, $this->store_id); $sms = new Sms($this->mall_id, 'domestic'); // 发送验证码 try { if ($sms->sendCaptcha($mobile, 86, 'update_happiness_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 = HappinessStore::getMobileById($this->mall_id, $this->store_id); $sms = new Sms($this->mall_id, 'domestic'); try { if (!$sms->checkValidateCode($mobile, $captcha, 'update_happiness_pass')) { return $this->error('验证码错误'); } } catch (\Exception $e) { return $this->error($e->getMessage()); } $store_admin = HappinessStoreAdmin::getAdminByStoreId($this->mall_id, $this->store_id); return $store_admin->changePwd($pwd); } }