MdFoodsQrcodeForm.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\FoodTableNumber;
  9. use app\models\Md;
  10. use app\models\MdFoodsQrcode;
  11. use app\models\Option;
  12. use app\models\Store;
  13. use yii\base\Model;
  14. class MdFoodsQrcodeForm extends Model
  15. {
  16. public $id;
  17. public $parent_id;
  18. public $store_id;
  19. public $num;
  20. public $table_num;
  21. public $md_id;
  22. public $status;
  23. public function rules()
  24. {
  25. return [
  26. [['id', 'store_id', 'num', 'md_id', 'parent_id', 'status'], 'integer'],
  27. [['table_num'], 'string']
  28. ];
  29. }
  30. public function list() {
  31. $query = MdFoodsQrcode::find()->where(['is_delete' => 0, 'parent_id' => 0])->select('id, parent_id, url_path, store_id, table_num, created_at, md_id, is_disabled');
  32. if ($this->status !== null && in_array($this->status, [0, 1])) {
  33. if (intval($this->status) === 0) {
  34. $query->andWhere(['store_id' => 0]);
  35. } else {
  36. $query->andWhere(['>', 'store_id', 0]);
  37. }
  38. }
  39. $pagination = pagination_make($query);
  40. foreach ($pagination['list'] as &$item) {
  41. $item = $this->getField($item);
  42. }
  43. // $pagination['list'] = $this->getField($pagination['list']);
  44. return [
  45. 'code' => 0,
  46. 'msg' => 'success',
  47. 'data' => [
  48. 'data' => $pagination['list'],
  49. 'pageNo' => $pagination['pageNo'],
  50. 'totalCount' => $pagination['totalCount'],
  51. ]
  52. ];
  53. }
  54. public function createQrcode() {
  55. try {
  56. $num = $this->num ?: 1;
  57. $arr = range(1, $num);
  58. // $parent_id = $this->parent_id ?: 0;
  59. foreach ($arr as $item) {
  60. $max_id = MdFoodsQrcode::find()->max('id');;
  61. $max_id = $max_id + 1;
  62. $form = new MdFoodsQrcode();
  63. $form->id = $max_id;
  64. $form->url_path = \Yii::$app->request->hostInfo . '/web/food/store/' . $max_id . '/?id='. $max_id;
  65. // $form->parent_id = $parent_id;
  66. $form->mini_path = "/alipay-order/orderMeal/orderMeal";
  67. $form->save();
  68. }
  69. return [
  70. 'code' => 0,
  71. 'msg' => '生成成功'
  72. ];
  73. } catch (\Exception $e) {
  74. return [
  75. 'code' => 1,
  76. 'msg' => $e->getMessage()
  77. ];
  78. }
  79. }
  80. //修改子码桌号 / 门店
  81. public function editQrcode() {
  82. try {
  83. $id = $this->id;
  84. $table_num = $this->table_num;
  85. $md_id = $this->md_id;
  86. $qrcode = MdFoodsQrcode::findOne(['id' => $id, 'is_delete' => 0]);
  87. if ($qrcode) {
  88. if ($table_num) {
  89. $query = FoodTableNumber::find()->where([
  90. 'is_delete' => 0,
  91. 'store_id' => $qrcode->store_id,
  92. 'id' => $table_num
  93. ]);
  94. if ($qrcode->md_id) {
  95. $query->andWhere(['md_id' => $qrcode->md_id]);
  96. } else {
  97. $query->andWhere(['md_id' => [-1, 0]]);
  98. }
  99. $table_info = $query->one();
  100. if ($table_info) {
  101. $qrcode->table_num = $table_num;
  102. } else {
  103. throw new \Exception('未找到对应的桌号信息');
  104. }
  105. }
  106. if ($md_id) {
  107. if ($qrcode->table_num) {
  108. throw new \Exception('当前已绑定桌号,不可设置门店');
  109. }
  110. // if ($qrcode->is_disabled) {
  111. // throw new \Exception('当前状态已到最底层,不可设置门店');
  112. // }
  113. $md_info = Md::find()->where([
  114. 'is_delete' => 0,
  115. 'store_id' => $qrcode->store_id,
  116. 'id' => $md_id
  117. ])->one();
  118. if ($md_info) {
  119. $qrcode->md_id = $md_id;
  120. } else {
  121. throw new \Exception('未找到对应的门店信息');
  122. }
  123. }
  124. $store_id = $qrcode->store_id;
  125. if ($store_id) {
  126. $store = Store::findOne($store_id);
  127. if ($store) {
  128. if (intval($store->business_model) !== 1) { //非独立运营
  129. $str = $qrcode->url_path;
  130. $scene = substr($str, 0,(strpos($str, '?scene=')));
  131. $scene .= "?scene=store_id:" . $store_id;
  132. if ($qrcode->md_id) {
  133. $scene .= ",md_id:" . $qrcode->md_id;
  134. }
  135. if (isset($table_info)) {
  136. $scene .= ",food_table_num:" . $table_info->num . ",f_num:" . $table_info->num;
  137. }
  138. $qrcode->url_path = $scene;
  139. }
  140. }
  141. }
  142. if (!$qrcode->save()) {
  143. throw new \Exception(json_encode($qrcode->errors, JSON_UNESCAPED_UNICODE));
  144. }
  145. return [
  146. 'code' => 0,
  147. 'msg' => '操作成功'
  148. ];
  149. }
  150. throw new \Exception('查找数据失败');
  151. } catch (\Exception $e) {
  152. return [
  153. 'code' => 1,
  154. 'msg' => $e->getMessage()
  155. ];
  156. }
  157. }
  158. public function delQrcode() {
  159. try {
  160. $id = $this->id;
  161. $qrcode = MdFoodsQrcode::findOne($id);
  162. $qrcode->is_delete = 1;
  163. if (!$qrcode->save()) {
  164. throw new \Exception(json_encode($qrcode->errors, JSON_UNESCAPED_UNICODE));
  165. }
  166. return [
  167. 'code' => 0,
  168. 'msg' => '操作成功'
  169. ];
  170. } catch (\Exception $e) {
  171. return [
  172. 'code' => 1,
  173. 'msg' => $e->getMessage()
  174. ];
  175. }
  176. }
  177. //
  178. public function getField($data){
  179. $item = $data;
  180. $item['is_disabled'] = (int)$item['is_disabled'];
  181. $store = Store::findOne($item['store_id']);
  182. $item['store'] = [
  183. 'name' => $store->name ?: '',
  184. 'logo' => $store->logo ?: (Option::get('logo', $item['store_id'], 'store')['value'] ?: Option::get('web_log', $item['store_id'], 'web')['value'])
  185. ];
  186. $item['table_num_list'] = [];
  187. $item['md'] = [
  188. 'name' => '',
  189. 'logo' => ''
  190. ];
  191. if ($store) {
  192. $query = FoodTableNumber::find()->where([
  193. 'is_delete' => 0,
  194. 'store_id' => $item['store_id'],
  195. ]);
  196. $qrcode_query = MdFoodsQrcode::find()->where(['is_delete' => 0, 'store_id' => $item['store_id']]);
  197. $item['table_num_text'] = FoodTableNumber::findOne($item['table_num'])->num ?: '';
  198. $md_query = clone $qrcode_query;
  199. $filter_md_id = $md_query->select('md_id')->column();
  200. $md_list = Md::find()->where(['store_id' => $item['store_id'], 'is_delete' => 0])
  201. ->andWhere(['NOT IN', 'id', $filter_md_id])->select('id, name')->asArray()->all();
  202. $item['md_list'] = $md_list;
  203. if ($item['md_id']) {
  204. $query->andWhere(['md_id' => $item['md_id']]);
  205. $qrcode_query->andWhere(['md_id' => $item['md_id']]);
  206. $md = Md::findOne($item['md_id']);
  207. $item['md'] = [
  208. 'name' => $md->name ?: '',
  209. 'logo' => $md->cover_url ?: ''
  210. ];
  211. } else {
  212. $query->andWhere(['md_id' => [-1, 0]]);
  213. $qrcode_query->andWhere(['md_id' => [-1, 0]]);
  214. }
  215. $filter_id = $qrcode_query->select('table_num')->column();
  216. $query->andWhere(['NOT IN', 'id', $filter_id]);
  217. $table_num_list = $query->andWhere(['<>', 'id', $item['id']])->select('id, num, remark')->asArray()->all();
  218. $item['table_num_list'] = $table_num_list;
  219. }
  220. $item['status'] = 0;
  221. if ($item['store_id'] > 0) {
  222. $item['status'] = 1;
  223. }
  224. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  225. // if ($item['children']) {
  226. // foreach ($item['children'] as &$i) {
  227. // $i = $this->getField($i);
  228. // }
  229. //
  230. // }
  231. return $item;
  232. }
  233. }