AgentService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\AgentModel;
  15. use App\Models\AgentRegionModel;
  16. use App\Models\MemberModel;
  17. use App\Services\BaseService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 代理管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Common
  24. */
  25. class AgentService extends BaseService
  26. {
  27. public static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new AgentModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 列表
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15)
  55. {
  56. $where = ['a.mark' => 1];
  57. $list = $this->model->with(['user'])->from('agent as a')
  58. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  59. ->where($where)
  60. ->where(function ($query) use ($params) {
  61. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  62. if ($keyword) {
  63. $query->where('a.realname', 'like', "%{$keyword}%");
  64. }
  65. })
  66. ->where(function ($query) use ($params) {
  67. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  68. if ($mobile) {
  69. $query->where('a.mobile', 'like', "%{$mobile}%");
  70. }
  71. $status = isset($params['status']) ? $params['status'] : 0;
  72. if ($status > 0 && is_array($status)) {
  73. $query->whereIn('a.status', $status);
  74. } else if ($status) {
  75. $query->where('a.status', $status);
  76. }
  77. })
  78. ->select(['a.*', 'c.nickname', 'c.mobile as user_mobile', 'c.balance as balance'])
  79. ->orderBy('a.create_time', 'desc')
  80. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  81. $list = $list ? $list->toArray() : [];
  82. if ($list) {
  83. foreach ($list['data'] as &$item) {
  84. $item['selected'] = false;
  85. $item['nickname'] = trim($item['nickname']);
  86. $item['apply_user'] = trim($item['nickname']) . '-' . $item['user_mobile'];
  87. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  88. $item['idcard_back_img'] = isset($item['idcard_back_img']) && $item['idcard_back_img'] ? get_image_url($item['idcard_back_img']) : '';
  89. $item['idcard_front_img'] = isset($item['idcard_front_img']) && $item['idcard_front_img'] ? get_image_url($item['idcard_front_img']) : '';
  90. $item['idcard_hand_img'] = isset($item['idcard_hand_img']) && $item['idcard_hand_img'] ? get_image_url($item['idcard_hand_img']) : '';
  91. $item['city'] = [];
  92. if (isset($item['area']) && $item['area']) {
  93. $regions = AgentRegionModel::whereIn('name', [$item['province'], $item['area']])->orderBy('level', 'asc')->pluck('id');
  94. $parentId = AgentRegionModel::where(['name' => $item['area']])->value('pid');
  95. if ($regions) {
  96. $provinceId = isset($regions[0]) ? $regions[0] : 0;
  97. $cityId = isset($regions[1]) ? $regions[1] : 0;
  98. $hasChildren = AgentRegionModel::where(['pid' => $cityId])->value('id');
  99. if (!$hasChildren) {
  100. $item['city'] = $parentId == $provinceId ? [$provinceId, $cityId] : [$provinceId, $parentId, $cityId];
  101. } else {
  102. $item['city'] = $parentId == $provinceId ? [$provinceId, $cityId, $cityId] : [$provinceId, $parentId, $cityId];
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return [
  109. 'pageSize' => $pageSize,
  110. 'total' => isset($list['total']) ? $list['total'] : 0,
  111. 'list' => isset($list['data']) ? $list['data'] : []
  112. ];
  113. }
  114. /**
  115. * 按日期统计注册司机数
  116. * @param string $beginAt 开始时间
  117. * @param string $endAt 结束时间
  118. * @param int[] $status 状态:数组或数值
  119. * @return mixed
  120. */
  121. public function getRegisterCount($beginAt = '', $endAt = '', $status = [2, 4])
  122. {
  123. $where = ['mark' => 1];
  124. return $this->model->where($where)->where(function ($query) use ($beginAt, $endAt, $status) {
  125. if ($beginAt && $endAt) {
  126. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  127. } else if ($beginAt) {
  128. $query->where('create_time', '>=', strtotime($beginAt));
  129. }
  130. if ($status && is_array($status)) {
  131. $query->whereIn('status', $status);
  132. } else if ($status) {
  133. $query->where('status', $status);
  134. }
  135. })->count('id');
  136. }
  137. /**
  138. * 用户选项
  139. * @return array
  140. */
  141. public function options()
  142. {
  143. // 获取参数
  144. $param = request()->all();
  145. // 用户ID
  146. $keyword = getter($param, "keyword");
  147. $parentId = getter($param, "parent_id");
  148. $userId = getter($param, "id");
  149. $datas = $this->model->where(function ($query) use ($parentId) {
  150. if ($parentId) {
  151. $query->where(['id' => $parentId, 'mark' => 1]);
  152. } else {
  153. $query->where(['status' => 1, 'mark' => 1]);
  154. }
  155. })
  156. ->where(function ($query) use ($userId) {
  157. if ($userId) {
  158. $query->whereNotIn('id', [$userId]);
  159. }
  160. })
  161. ->where(function ($query) use ($keyword) {
  162. if ($keyword) {
  163. $query->where('realname', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
  164. }
  165. })
  166. ->select(['id', 'area', 'mobile', 'realname', 'status'])
  167. ->get();
  168. return $datas ? $datas->toArray() : [];
  169. }
  170. /**
  171. * 添加会编辑会员
  172. * @return array
  173. * @since 2020/11/11
  174. * @author laravel开发员
  175. */
  176. public function edit()
  177. {
  178. // 请求参数
  179. $data = request()->all();
  180. $id = isset($data['id']) ? $data['id'] : 0;
  181. $cityData = isset($data['city']) ? $data['city'] : [];
  182. if ($cityData) {
  183. if (count($cityData) > 2) {
  184. unset($cityData[1]);
  185. }
  186. $regions = AgentRegionModel::whereIn('id', $cityData)->orderBy('level', 'asc')->pluck('name');
  187. $data['province'] = isset($regions[0]) ? $regions[0] : '';
  188. $data['area'] = isset($regions[1]) ? $regions[1] : '';
  189. $checkId = $this->model->where(['area' => $data['area'], 'status' => 2, 'mark' => 1])->value('id');
  190. if ($data['area'] && $checkId && ($checkId != $id) && $data['status'] == 2) {
  191. return message("该区域【{$data['area']}】代理已存在", false);
  192. }
  193. }
  194. $info = $this->model->where(['id' => $id])->first();
  195. // 设置日志标题
  196. ActionLogModel::setTitle("审核或修改代理信息");
  197. ActionLogModel::record();
  198. unset($data['city']);
  199. unset($data['user']);
  200. DB::beginTransaction();
  201. $result = parent::edit($data); // TODO: Change the autogenerated stub
  202. $success = isset($result['success']) ? $result['success'] : '';
  203. $status = isset($info['status']) ? $info['status'] : 0;
  204. $deposit = isset($info['deposit']) ? $info['deposit'] : 0;
  205. $userId = isset($data['user_id'])? $data['user_id'] : 0;
  206. if ($success) {
  207. // 审核通过
  208. if ($status != 2 && $data['status'] == 2) {
  209. if(!\App\Services\Api\FinanceService::make()->tzScoreAward($deposit, $userId, $data['id'], 2)){
  210. DB::rollBack();
  211. return message('审核失败', false);
  212. }
  213. }
  214. }
  215. DB::commit();
  216. return $result;
  217. }
  218. }