StoreCommunityService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\Store\common\models\StoreCommunity;
  4. use addons\Store\common\models\StoreCommunity as StoreCommunityModel;
  5. use common\models\common\Region;
  6. use common\models\user\Town;
  7. /**
  8. * 门店service
  9. * @package addons\Store\common\services
  10. */
  11. class StoreCommunityService extends BaseService
  12. {
  13. public function getList(array $params = [])
  14. {
  15. $where[] = ['=', 'mall_id', $params['mall_id']];
  16. $where[] = ['=', 'status', 0];
  17. if (!empty($params['province_name'])) {
  18. $provinceInfo = Region::find()
  19. ->where(['level' => 1])
  20. ->andFilterWhere(['like', 'name', $params['province_name']])
  21. ->all();
  22. $provinceInfoList = array_column($provinceInfo,'id');
  23. if (!empty($provinceInfoList)) {
  24. $where[] = ['in', 'province_id', $provinceInfoList];
  25. }
  26. }
  27. if (!empty($params['city_name'])) {
  28. $cityInfo = Region::find()
  29. ->where(['level' => 2])
  30. ->andFilterWhere(['like', 'name', $params['city_name']])
  31. ->all();
  32. $cityInfoList = array_column($cityInfo,'id');
  33. if (!empty($cityInfoList)) {
  34. $where[] = ['in', 'city_id', $cityInfoList];
  35. }
  36. }
  37. if (!empty($params['district_name'])) {
  38. $districtInfo = Region::find()
  39. ->where(['level' => 3])
  40. ->andFilterWhere(['like', 'name', $params['district_name']])
  41. ->all();
  42. $districtInfoList = array_column($districtInfo,'id');
  43. if (!empty($districtInfoList)) {
  44. $where[] = ['in', 'district_id', $districtInfoList];
  45. }
  46. }
  47. if (!empty($params['street_name'])) {
  48. $streetInfo = Town::find()
  49. ->andFilterWhere(['like', 'name', $params['street_name']])
  50. ->all();
  51. $streetInfoList = array_column($streetInfo,'id');
  52. if (!empty($streetInfoList)) {
  53. $where[] = ['in', 'street_id', $streetInfoList];
  54. }
  55. }
  56. if (!empty($params['community_name'])) {
  57. $where[] = ['like', 'community_name', $params['community_name']];
  58. }
  59. if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
  60. if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
  61. $wheres['where'] = $where;
  62. $wheres['limit'] = 10;
  63. $wheres['order'] = 'id DESC';
  64. $pageData = StoreCommunity::listPage($wheres, false);
  65. if (!empty($pageData)) {
  66. $provinceIds = array_column($pageData['list'], 'province_id');
  67. $cityIds = array_column($pageData['list'], 'city_id');
  68. $districtIds = array_column($pageData['list'], 'district_id');
  69. $regionIds = array_unique(array_merge($provinceIds, $cityIds, $districtIds));
  70. $regionList = Region::find()->where(['id'=>$regionIds])->select('id,name')->asArray()->indexBy('id')->all();
  71. $streetIds = array_unique(array_column($pageData['list'], 'street_id'));
  72. $streetList = Town::find()->where(['id'=>$streetIds])->select('id,name')->asArray()->indexBy('id')->all();
  73. foreach ($pageData['list'] as &$item) {
  74. $item['province_name'] = $regionList[$item['province_id']]['name']??'';
  75. $item['city_name'] = $regionList[$item['city_id']]['name']??'';
  76. $item['district_name'] = $regionList[$item['district_id']]['name']??'';
  77. $item['street_name'] = $streetList[$item['street_id']]['name']??'';
  78. }
  79. }
  80. return $pageData;
  81. }
  82. public function addCommunity(array $params):bool
  83. {
  84. if(empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id']) || empty($params['community_list'])) {
  85. return $this->error('参数错误');
  86. }
  87. $communityList = array_column($params['community_list'], 'name');
  88. if(in_array('', $communityList) || in_array(null, $communityList)){
  89. return $this->error('抱歉,您有社区内容未填写');
  90. }
  91. $model = new StoreCommunityModel();
  92. $now = time();
  93. //校验名称是否有重复
  94. $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);
  95. $communityExist = array_column($communityExist, 'community_name');
  96. if(array_intersect($communityList, $communityExist)){
  97. return $this->error('抱歉,提交的社区里面已经有存在的');
  98. }
  99. $uniqueArray = array_unique($communityList);
  100. if(count($uniqueArray) != count($communityList)){
  101. return $this->error('抱歉,您提交的社区里面不能重复');
  102. }
  103. $transaction = \Yii::$app->db->beginTransaction();
  104. try {
  105. foreach ($communityList as $v) {
  106. try {
  107. $data = [
  108. 'mall_id'=>$params['mall_id'],
  109. 'province_id'=>$params['province_id'],
  110. 'city_id'=>$params['city_id'],
  111. 'district_id'=>$params['district_id'],
  112. 'street_id'=>$params['street_id'],
  113. 'community_name'=>$v,
  114. 'created_at' => $now,
  115. 'updated_at' => $now
  116. ];
  117. $model->setData($data);
  118. }catch (\Exception $e){
  119. var_dump($e->getMessage());
  120. var_dump($e->getLine());
  121. }
  122. }
  123. $transaction->commit();
  124. return true;
  125. } catch (\Exception $e) {
  126. $transaction->rollBack();
  127. \Yii::error('Error: ' . $e->getMessage(), 'my_app');
  128. return $this->error('保存失败: ' . $e->getMessage());
  129. }
  130. return false;
  131. }
  132. }