StoreForbiddenDirectoryForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\constants\OptionSetting;
  4. use app\models\Admin;
  5. use app\models\SaasCategory;
  6. use app\models\Store;
  7. use app\models\StoreCloud;
  8. use app\models\StoreForbiddenDirectory;
  9. use yii\base\Model;
  10. class StoreForbiddenDirectoryForm extends Model
  11. {
  12. public $store_name;
  13. public $store_id;
  14. public $id;
  15. public $cloud_cat_id;
  16. public $category_id;
  17. public function rules()
  18. {
  19. return [
  20. [['store_name'], 'string'],
  21. [['id', 'category_id'], 'integer'],
  22. [['cloud_cat_id', 'store_id'], 'safe']
  23. ];
  24. }
  25. //店铺禁售目录
  26. public function storeForbiddenDirectory()
  27. {
  28. $store_name = $this->store_name;
  29. $category_id = $this->category_id;
  30. $query = Store::find()->alias('s')
  31. ->leftJoin(['a' => Admin::tableName()], 'a.type_id = s.id AND a.type = "store"')
  32. ->rightJoin(['sfd' => StoreForbiddenDirectory::tableName()], 'sfd.store_id = s.id');
  33. $query->where(['s.is_delete' => 0, 'sfd.is_delete' => 0]);
  34. if ($store_name) {
  35. $query->andWhere(['like', 's.name', $store_name]);
  36. }
  37. if ($category_id) {
  38. $query->andWhere(['s.category_id' => $category_id]);
  39. }
  40. $query->select('s.id, s.name as store_name, s.logo, a.name as nickname, a.mobile, sfd.cloud_cat_id, s.category_id, sfd.cloud_cat_name');
  41. $list = pagination_make($query);
  42. foreach ($list['list'] as &$item) {
  43. $item['cat_name'] = '-';
  44. if ($item['category_id'] > 0) {
  45. $cat = SaasCategory::findOne($item['category_id']);
  46. if ($cat) {
  47. $item['cat_name'] = $cat->name;
  48. }
  49. }
  50. if ($item['cloud_cat_id']) {
  51. $item['cloud_cat_id'] = explode(',', $item['cloud_cat_id']);
  52. } else {
  53. $item['cloud_cat_id'] = [];
  54. }
  55. }
  56. return [
  57. 'code' => 0,
  58. 'msg' => '获取成功',
  59. 'data' => $list
  60. ];
  61. }
  62. //保存
  63. public function save()
  64. {
  65. try {
  66. $store_id = $this->store_id;
  67. $cloud_cat_id = $this->cloud_cat_id;
  68. //判断是否拥有云仓权限
  69. $merchant_token = get_merchant_token(0, $store_id);
  70. if (empty($merchant_token)) {
  71. throw new \Exception('该店铺未拥有云仓权限');
  72. }
  73. if (empty($cloud_cat_id)) {
  74. throw new \Exception('分类不能为空');
  75. }
  76. $cloud_token = get_platform_token();
  77. $store_user_add_url = "/cloud/user/getGoodsCatList";
  78. $store_user_add_data['access_token'] = $cloud_token;
  79. $store_user_add_data['ids'] = implode(',', $cloud_cat_id);
  80. $cat_list_info = cloud_post((new OptionSetting)->getCloudDomainName().$store_user_add_url,$store_user_add_data);
  81. $cat_list_result = json_decode($cat_list_info,true);
  82. if(empty($cat_list_result) || $cat_list_result['code'] != 0){
  83. return $cat_list_result;
  84. }
  85. $cat_list = $cat_list_result['data']['cat_list'];
  86. $cat_name = $id = [];
  87. if (!empty($cat_list)) {
  88. $cat_name = array_column($cat_list, 'name');
  89. $id = array_column($cat_list, 'id');
  90. }
  91. $store_forbidden_directory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  92. if (empty($store_forbidden_directory)) {
  93. $store_forbidden_directory = new StoreForbiddenDirectory();
  94. $store_forbidden_directory->store_id = $store_id;
  95. }
  96. $store_forbidden_directory->cloud_cat_id = implode(',', $id);
  97. $store_forbidden_directory->cloud_cat_name = implode('/', $cat_name);
  98. if (!$store_forbidden_directory->save()) {
  99. throw new \Exception(json_encode($store_forbidden_directory->errors, JSON_UNESCAPED_UNICODE));
  100. }
  101. return [
  102. 'code' => 0,
  103. 'msg' => '操作成功'
  104. ];
  105. } catch (\Exception $e) {
  106. return [
  107. 'code' => 1,
  108. 'msg' => $e->getMessage()
  109. ];
  110. }
  111. }
  112. //删除
  113. public function del()
  114. {
  115. try {
  116. $store_id = explode(',', $this->store_id);
  117. foreach ($store_id as $item) {
  118. $storeForbiddenDirectory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  119. $storeForbiddenDirectory->is_delete = 1;
  120. $storeForbiddenDirectory->save();
  121. }
  122. return [
  123. 'code' => 0,
  124. 'msg' => '操作成功'
  125. ];
  126. } catch (\Exception $e) {
  127. return [
  128. 'code' => 1,
  129. 'msg' => $e->getMessage()
  130. ];
  131. }
  132. }
  133. }