SystemStore.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\model\system;
  3. use app\models\store\StoreOrder;
  4. use app\models\system\SystemStoreStaff;
  5. use app\models\user\User;
  6. use crmeb\traits\ModelTrait;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\PHPExcelService;
  9. /**
  10. * 门店自提 model
  11. * Class SystemStore
  12. * @package app\admin\model\system
  13. */
  14. class SystemStore extends BaseModel
  15. {
  16. use ModelTrait;
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'system_store';
  27. public static function getLatlngAttr($value, $data)
  28. {
  29. return $data['latitude'] . ',' . $data['longitude'];
  30. }
  31. public static function verificWhere()
  32. {
  33. return self::where('is_show', 1)->where('is_del', 0);
  34. }
  35. /**
  36. * 获取门店信息
  37. * @param int $id
  38. * @return array|\think\Model|null
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public static function getStoreDispose($id = 0)
  44. {
  45. if ($id)
  46. $storeInfo = self::verificWhere()->where('id', $id)->find();
  47. else
  48. // $storeInfo = self::verificWhere()->find();
  49. $storeInfo = [];
  50. if ($storeInfo) {
  51. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  52. $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  53. $storeInfo['day_time'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  54. $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  55. } else {
  56. $storeInfo['latlng'] = [];
  57. $storeInfo['valid_time'] = [];
  58. $storeInfo['valid_time'] = [];
  59. $storeInfo['day_time'] = [];
  60. $storeInfo['address'] = [];
  61. $storeInfo['id'] = 0;
  62. }
  63. return $storeInfo;
  64. }
  65. /**
  66. * 获取门店列表
  67. * @param $where
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public static function getStoreList($where)
  74. {
  75. $model = new self();
  76. $model = $model->alias('a')
  77. ->field('a.*, b.cate_name')
  78. ->leftJoin('store_cate b', 'a.cate_id = b.id');
  79. if (isset($where['name']) && $where['name'] != '') {
  80. $model = $model->where('a.id|a.name|a.introduction', 'like', '%' . $where['name'] . '%');
  81. }
  82. if (isset($where['type']) && $where['type'] != '' && ($data = self::setData($where['type']))) {
  83. $model = $model->where($data);
  84. }
  85. $count = $model->count();
  86. $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
  87. if ($where['excel'] == 1) {
  88. $export = [];
  89. foreach ($data as $index => $item) {
  90. $export[] = [
  91. $item['name'],
  92. $item['phone'],
  93. $item['address'] .= ' ' . $item['detailed_address'],
  94. $item['introduction'],
  95. $item['day_time'],
  96. $item['valid_time']
  97. ];
  98. }
  99. PHPExcelService::setExcelHeader(['门店名称', '门店电话', '门店地址', '门店简介', '营业时间', '核销日期'])
  100. ->setExcelTile('门店导出', '门店信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  101. ->setExcelContent($export)
  102. ->ExcelSave();
  103. }
  104. foreach ($data as &$item) {
  105. $uid = SystemStoreStaff::where('store_id', $item['id'])->column('uid');
  106. $userUid = $uid;
  107. if ($uid){
  108. $user = User::select();
  109. foreach ($uid as $v)
  110. {
  111. $list = get_downline($user,$v);
  112. if ($list){
  113. foreach ($list as $value){
  114. $userUid[$value] = $value;
  115. }
  116. }
  117. }
  118. }
  119. $item['count'] = StoreOrder::where('uid', 'in',$userUid)->where('paid', 1)->count();
  120. }
  121. return compact('count', 'data');
  122. }
  123. /**
  124. * 获取连表查询条件
  125. * @param $type
  126. * @return array
  127. */
  128. public static function setData($type)
  129. {
  130. switch ((int)$type) {
  131. case 1:
  132. $data = ['a.is_show' => 1, 'a.is_del' => 0];
  133. break;
  134. case 2:
  135. $data = ['a.is_show' => 0, 'a.is_del' => 0];
  136. break;
  137. case 3:
  138. $data = ['a.is_del' => 1];
  139. break;
  140. };
  141. return isset($data) ? $data : [];
  142. }
  143. public static function dropList()
  144. {
  145. $model = new self();
  146. $model = $model->where('is_del', 0);
  147. $list = $model->select()
  148. ->toArray();
  149. return $list;
  150. }
  151. }