| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\models\StoreCommunity;
- use addons\Store\common\models\StoreCommunity as StoreCommunityModel;
- use common\models\common\Region;
- use common\models\user\Town;
- /**
- * 门店service
- * @package addons\Store\common\services
- */
- class StoreCommunityService extends BaseService
- {
- public function getList(array $params = [])
- {
- $where[] = ['=', 'mall_id', $params['mall_id']];
- $where[] = ['=', 'status', 0];
- if (!empty($params['province_name'])) {
- $provinceInfo = Region::find()
- ->where(['level' => 1])
- ->andFilterWhere(['like', 'name', $params['province_name']])
- ->all();
- $provinceInfoList = array_column($provinceInfo,'id');
- if (!empty($provinceInfoList)) {
- $where[] = ['in', 'province_id', $provinceInfoList];
- }
- }
- if (!empty($params['city_name'])) {
- $cityInfo = Region::find()
- ->where(['level' => 2])
- ->andFilterWhere(['like', 'name', $params['city_name']])
- ->all();
- $cityInfoList = array_column($cityInfo,'id');
- if (!empty($cityInfoList)) {
- $where[] = ['in', 'city_id', $cityInfoList];
- }
- }
- if (!empty($params['district_name'])) {
- $districtInfo = Region::find()
- ->where(['level' => 3])
- ->andFilterWhere(['like', 'name', $params['district_name']])
- ->all();
- $districtInfoList = array_column($districtInfo,'id');
- if (!empty($districtInfoList)) {
- $where[] = ['in', 'district_id', $districtInfoList];
- }
- }
- if (!empty($params['street_name'])) {
- $streetInfo = Town::find()
- ->andFilterWhere(['like', 'name', $params['street_name']])
- ->all();
- $streetInfoList = array_column($streetInfo,'id');
- if (!empty($streetInfoList)) {
- $where[] = ['in', 'street_id', $streetInfoList];
- }
- }
- if (!empty($params['community_name'])) {
- $where[] = ['like', 'community_name', $params['community_name']];
- }
- if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
- if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
- $wheres['where'] = $where;
- $wheres['limit'] = 10;
- $wheres['order'] = 'id DESC';
- $pageData = StoreCommunity::listPage($wheres, false);
- if (!empty($pageData)) {
- $provinceIds = array_column($pageData['list'], 'province_id');
- $cityIds = array_column($pageData['list'], 'city_id');
- $districtIds = array_column($pageData['list'], 'district_id');
- $regionIds = array_unique(array_merge($provinceIds, $cityIds, $districtIds));
- $regionList = Region::find()->where(['id'=>$regionIds])->select('id,name')->asArray()->indexBy('id')->all();
- $streetIds = array_unique(array_column($pageData['list'], 'street_id'));
- $streetList = Town::find()->where(['id'=>$streetIds])->select('id,name')->asArray()->indexBy('id')->all();
- foreach ($pageData['list'] as &$item) {
- $item['province_name'] = $regionList[$item['province_id']]['name']??'';
- $item['city_name'] = $regionList[$item['city_id']]['name']??'';
- $item['district_name'] = $regionList[$item['district_id']]['name']??'';
- $item['street_name'] = $streetList[$item['street_id']]['name']??'';
- }
- }
- return $pageData;
- }
- public function addCommunity(array $params):bool
- {
- if(empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id']) || empty($params['community_list'])) {
- return $this->error('参数错误');
- }
- $communityList = array_column($params['community_list'], 'name');
- if(in_array('', $communityList) || in_array(null, $communityList)){
- return $this->error('抱歉,您有社区内容未填写');
- }
- $model = new StoreCommunityModel();
- $now = time();
- //校验名称是否有重复
- $communityExist = StoreCommunityModel::lists(['where' => [['mall_id'=>$params['mall_id'],'province_id'=>$params['province_id'],'city_id'=>$params['city_id'],'district_id'=>$params['district_id'],'street_id'=>$params['street_id']]], 'select' => 'community_name'], true);
- $communityExist = array_column($communityExist, 'community_name');
- if(array_intersect($communityList, $communityExist)){
- return $this->error('抱歉,提交的社区里面已经有存在的');
- }
- $uniqueArray = array_unique($communityList);
- if(count($uniqueArray) != count($communityList)){
- return $this->error('抱歉,您提交的社区里面不能重复');
- }
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- foreach ($communityList as $v) {
- try {
- $data = [
- 'mall_id'=>$params['mall_id'],
- 'province_id'=>$params['province_id'],
- 'city_id'=>$params['city_id'],
- 'district_id'=>$params['district_id'],
- 'street_id'=>$params['street_id'],
- 'community_name'=>$v,
- 'created_at' => $now,
- 'updated_at' => $now
- ];
- $model->setData($data);
- }catch (\Exception $e){
- var_dump($e->getMessage());
- var_dump($e->getLine());
- }
- }
- $transaction->commit();
- return true;
- } catch (\Exception $e) {
- $transaction->rollBack();
- \Yii::error('Error: ' . $e->getMessage(), 'my_app');
- return $this->error('保存失败: ' . $e->getMessage());
- }
- return false;
- }
- }
|