SystemStore.php 5.3 KB

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