Store.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\store;
  13. use app\model\shop\Shop;
  14. use app\model\system\Group;
  15. use think\facade\Cache;
  16. use app\model\BaseModel;
  17. use app\model\system\Cron;
  18. /**
  19. * 门店管理
  20. */
  21. class Store extends BaseModel
  22. {
  23. /**
  24. * 添加门店
  25. * @param unknown $data
  26. */
  27. public function addStore($data,$user_data = [],$is_store = 0)
  28. {
  29. $site_id = isset($data['site_id']) ? $data['site_id'] : '';
  30. if ($site_id === '') {
  31. return $this->error('', 'REQUEST_SITE_ID');
  32. }
  33. $data['create_time'] = time();
  34. $shop_model = new Shop();
  35. $shop_info_result = $shop_model->getShopInfo([["site_id", "=", $site_id]], "site_name");
  36. $shop_info = $shop_info_result["data"];
  37. $data["site_name"] = $shop_info["site_name"];
  38. model('store')->startTrans();
  39. try{
  40. if($is_store == 1){
  41. $data['username'] = $is_store == 1 ? $user_data['username'] : '';
  42. $data['store_id'] = model("site")->add([ 'site_type' => 'store' ]);
  43. $store_id = model('store')->add($data);
  44. Cache::tag("store")->clear();
  45. //添加系统用户组
  46. $group = new Group();
  47. $group_data = [
  48. 'site_id' => $store_id,
  49. 'app_module' => 'store',
  50. 'group_name' => '管理员组',
  51. 'is_system' => 1,
  52. 'create_time' => time()
  53. ];
  54. $group_id = $group->addGroup($group_data)['data'];
  55. //用户检测
  56. if (empty($user_data['username'])) {
  57. model("store")->rollback();
  58. return $this->error('', 'USER_NOT_EXIST');
  59. }
  60. $user_count = model("user")->getCount([ [ 'username', '=', $user_data['username'] ],['app_module','=','store'] ]);
  61. if ($user_count > 0) {
  62. model("store")->rollback();
  63. return $this->error('', 'USERNAME_EXISTED');
  64. }
  65. //添加用户
  66. $data_user = [
  67. 'app_module' => 'store',
  68. 'app_group' => 0,
  69. 'is_admin' => 1,
  70. 'group_id' => $group_id,
  71. 'group_name' => '管理员组',
  72. 'site_id' => $store_id
  73. ];
  74. $user_info = array_merge($data_user, $user_data);
  75. model("user")->add($user_info);
  76. }else{
  77. $store_id = model('store')->add($data);
  78. Cache::tag("store")->clear();
  79. }
  80. event("AddStore", ['store_id' => $store_id]);
  81. model('store')->commit();
  82. return $this->success($store_id);
  83. }catch (\Exception $e){
  84. model('store')->rollback();
  85. return $this->error('',$e->getMessage());
  86. }
  87. }
  88. /**
  89. * 修改门店
  90. * @param unknown $data
  91. * @return multitype:string
  92. */
  93. public function editStore($data, $condition)
  94. {
  95. $check_condition = array_column($condition, 2, 0);
  96. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  97. if ($site_id === '') {
  98. return $this->error('', 'REQUEST_SITE_ID');
  99. }
  100. $data["modify_time"] = time();
  101. $res = model('store')->update($data, $condition);
  102. Cache::tag("store")->clear();
  103. return $this->success($res);
  104. }
  105. /**
  106. * 删除门店
  107. * @param unknown $condition
  108. */
  109. public function deleteStore($condition)
  110. {
  111. $check_condition = array_column($condition, 2, 0);
  112. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  113. if ($site_id === '') {
  114. return $this->error('', 'REQUEST_SITE_ID');
  115. }
  116. $res = model('store')->delete($condition);
  117. Cache::tag("store")->clear();
  118. return $this->success($res);
  119. }
  120. /**
  121. * @param $condition
  122. * @param $is_frozen
  123. */
  124. public function frozenStore($condition, $is_frozen) {
  125. $check_condition = array_column($condition, 2, 0);
  126. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  127. if ($site_id === '') {
  128. return $this->error('', 'REQUEST_SITE_ID');
  129. }
  130. $res = model('store')->update(['is_frozen' => $is_frozen == 1 ? 0 : 1], $condition);
  131. Cache::tag("store")->clear();
  132. return $this->success($res);
  133. }
  134. /**
  135. * 获取门店信息
  136. * @param array $condition
  137. * @param string $field
  138. */
  139. public function getStoreInfo($condition, $field = '*')
  140. {
  141. $data = json_encode([ $condition, $field]);
  142. $cache = Cache::get("store_". $data);
  143. if (!empty($cache)) {
  144. return $this->success($cache);
  145. }
  146. $res = model('store')->getInfo( $condition, $field);
  147. Cache::tag("store")->set("store_". $data, $res);
  148. return $this->success($res);
  149. }
  150. /**
  151. * 获取门店列表
  152. * @param array $condition
  153. * @param string $field
  154. * @param string $order
  155. * @param string $limit
  156. */
  157. public function getStoreList($condition = [], $field = '*', $order = '', $limit = null)
  158. {
  159. $data = json_encode([ $condition, $field, $order, $limit ]);
  160. $cache = Cache::get("store_getStoreList_" . $data);
  161. if (!empty($cache)) {
  162. return $this->success($cache);
  163. }
  164. $list = model('store')->getList($condition, $field, $order, '', '', '', $limit);
  165. Cache::tag("store")->set("store_getStoreList_" . $data, $list);
  166. return $this->success($list);
  167. }
  168. /**
  169. * 获取门店分页列表
  170. * @param array $condition
  171. * @param number $page
  172. * @param string $page_size
  173. * @param string $order
  174. * @param string $field
  175. */
  176. public function getStorePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  177. {
  178. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  179. $cache = Cache::get("store_getStorePageList_" . $data);
  180. if (!empty($cache)) {
  181. return $this->success($cache);
  182. }
  183. $list = model('store')->pageList($condition, $field, $order, $page, $page_size);
  184. Cache::tag("store")->set("store_getStorePageList_" . $data, $list);
  185. return $this->success($list);
  186. }
  187. }