MotorAgent.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace app\admin\controller\users;
  3. use app\api\model\taxi\MotorRecord;
  4. use app\common\controller\AdminController;
  5. use app\common\model\Seller;
  6. use app\common\model\TaxiOrder;
  7. use app\http\IResponse;
  8. use think\App;
  9. use think\Exception;
  10. use think\Request;
  11. use think\Validate;
  12. class MotorAgent extends AdminController
  13. {
  14. protected $model;
  15. public function __construct(App $app = null)
  16. {
  17. parent::__construct($app);
  18. $this->model = new \app\admin\model\users\MotorAgent();
  19. }
  20. /**
  21. * 显示资源列表
  22. *
  23. * @return \think\Response
  24. * @throws \think\exception\DbException
  25. */
  26. public function index()
  27. {
  28. $params = input();
  29. $params['card_id'] = input('card_id/s', '');
  30. $params['nickname'] = input('nickname/s', '');
  31. $where = [];
  32. if ($params['nickname']) {
  33. $where['nickname'] = ['like', '%' . $params['nickname'] . '%'];
  34. }
  35. $list = $this->model->with(['user'])
  36. ->where($where)
  37. ->order(['id' => 'desc'])
  38. ->paginate(input('limit', 10),false);
  39. return IResponse::paginate($list);
  40. }
  41. /**
  42. * @desc 统计数据
  43. * @return mixed
  44. * @author weichuanbao<654745815@qq.com>
  45. * @date 2022/1/4 0004
  46. */
  47. public function stats()
  48. {
  49. return IResponse::success($this->model->stats());
  50. }
  51. /**
  52. * 显示创建资源表单页.
  53. *
  54. * @param $ids
  55. * @return \think\Response
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function create($ids)
  61. {
  62. $row = $this->model::get($ids);
  63. if (!$row) {
  64. return IResponse::failure('记录不存在');
  65. }
  66. $Seller = new Seller();
  67. $TaxiOrder = new TaxiOrder();
  68. // 商户数量
  69. $seller = $Seller->where(['area_id' => $row['area_id']])->count();
  70. // 订单数据
  71. // 摩的订单,技能订单,商品订单,配送订单
  72. $taxi_order = $TaxiOrder->whereIn('area_id', $row['area_id'])->whereNotIn('status', [1])->column('price', 'id');
  73. $total_order = count($taxi_order);
  74. $turnover = array_sum($taxi_order);
  75. return IResponse::success([
  76. 'region' => $row['area'],
  77. 'total_order' => $total_order,
  78. 'turnover' => $turnover,
  79. 'seller' => $seller,
  80. 'taxi_order' => $taxi_order,
  81. ]);
  82. }
  83. /**
  84. * 保存新建的资源
  85. *
  86. * @param \think\Request $request
  87. * @return \think\Response
  88. */
  89. public function save(Request $request)
  90. {
  91. //
  92. }
  93. /**
  94. * 显示指定的资源
  95. *
  96. * @param int $id
  97. * @return \think\Response
  98. */
  99. public function read($id)
  100. {
  101. //
  102. }
  103. /**
  104. * 显示编辑资源表单页.
  105. *
  106. * @param int $id
  107. * @return \think\Response
  108. */
  109. public function edit($id)
  110. {
  111. //
  112. }
  113. /**
  114. * 保存更新的资源
  115. *
  116. * @param int $id
  117. * @return \think\Response
  118. * @throws \think\exception\PDOException
  119. */
  120. public function update($id)
  121. {
  122. $row = $this->model::get($id);
  123. if (!$row) {
  124. return IResponse::failure('记录不存在');
  125. }
  126. $params = input();
  127. $Validate = new Validate([
  128. 'mobile' => 'require'
  129. ], [
  130. 'mobile.require' => '手机号必须',
  131. ]);
  132. if (!$Validate->check($params)) {
  133. return IResponse::failure($Validate->getError());
  134. }
  135. $result = false;
  136. $this->model->startTrans();
  137. try {
  138. $result = $row->allowField(true)->save($params);
  139. $this->model->commit();
  140. }
  141. catch(Exception $e) {
  142. $this->model->rollback();
  143. }
  144. if ($result) {
  145. return IResponse::success('成功');
  146. }
  147. return IResponse::failure('失败');
  148. }
  149. /**
  150. * @desc 审核
  151. * @param $ids
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. * @throws \think\exception\DbException
  155. * @throws \think\exception\PDOException
  156. * @author weichuanbao<654745815@qq.com>
  157. * @date 2021/12/22 0022
  158. */
  159. public function check($ids)
  160. {
  161. $row = $this->model::get($ids);
  162. if (!$row) {
  163. IResponse::failure('记录不存在');
  164. }
  165. if ($row['status'] >= 30) {
  166. IResponse::failure('用户已经通过审核,无需操作');
  167. }
  168. $user = model('\app\common\model\Users')->field('id,is_motor_agent')->find($row['user_id']);
  169. $result = false;
  170. $this->model->startTrans();
  171. try {
  172. $user->is_motor_agent = 2;
  173. $user->save();
  174. $row->status = 30;
  175. $row->save();
  176. // 新建角色
  177. $Role = new \app\agent\model\auth\Role();
  178. // $PermissionRole = new \app\agent\model\auth\PermissionRole();
  179. // 获取角色权限
  180. // $permissionRole = $PermissionRole::get(2);
  181. $role = $Role::create([
  182. 'name' => 'motor_agent_'.$row['user_id'],
  183. 'description' => '系统管理员',
  184. 'user_id' => $row['user_id'],
  185. ], true);
  186. // 创建摩的代理后台账号
  187. $User = new \app\agent\model\auth\User();
  188. $admin = $User::create([
  189. 'username' => $row['account'],
  190. 'email' => $row['mobile'].'@rrj.com',
  191. 'password' => password_hash(input('password/s'), PASSWORD_BCRYPT),
  192. 'user_id' => $row['user_id'],
  193. 'area_id' => $row['area_id'],
  194. ], true);
  195. // 写入用户权限
  196. $AgentCasbin = new \CasbinAdapter\Think\Facades\AgentCasbin();
  197. $result = $AgentCasbin::AddRoleForUser('user_id_' . $admin['id'], $role['name']);
  198. // 赋予角色权限
  199. $this->model->authority($role['id'], $role['name']);
  200. $this->model->commit();
  201. }
  202. catch(Exception $e) {
  203. $this->model->rollback();
  204. IResponse::failure($e->getMessage());
  205. }
  206. if ($result) {
  207. IResponse::success();
  208. }
  209. IResponse::failure('失败');
  210. }
  211. /**
  212. * @desc 删除
  213. * @param $ids
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. * @throws \think\exception\DbException
  217. * @throws \think\exception\PDOException
  218. * @author weichuanbao<654745815@qq.com>
  219. * @date 2021/12/22 0022
  220. */
  221. public function deluser($ids)
  222. {
  223. $row = $this->model::get($ids);
  224. if (!$row) {
  225. IResponse::failure('记录不存在');
  226. }
  227. Db::table('ins_users_motor_agent')->where('$ids',1)->delete();
  228. IResponse::success([],'操作成功');
  229. }
  230. /**
  231. * @desc 发放合伙资产
  232. * @param $ids
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\ModelNotFoundException
  235. * @throws \think\exception\DbException
  236. * @throws \think\exception\PDOException
  237. * @author weichuanbao<654745815@qq.com>
  238. * @date 2022/1/7 0007
  239. */
  240. public function partnership($ids)
  241. {
  242. $row = $this->model->with('user')->find($ids);
  243. if (!$row) {
  244. IResponse::failure('记录不存在');
  245. }
  246. $params = input();
  247. $params['amount'] = input('amount', 0);
  248. $MotorRecord = new MotorRecord();
  249. $result = false;
  250. $this->model->startTrans();
  251. try {
  252. $row->user->partnership = $params['amount'];
  253. $result = $row->user->save();
  254. $MotorRecord->setMoneyLog($row['user'], 'partnership', $params['amount'], 30, '发放合伙资产');
  255. $MotorRecord->saveMoneyLog();
  256. $this->model->commit();
  257. }
  258. catch(Exception $e) {
  259. $this->model->rollback();
  260. }
  261. if ($result) {
  262. IResponse::success([],'成功');
  263. }
  264. IResponse::failure('失败');
  265. }
  266. /**
  267. * 删除指定资源
  268. *
  269. * @param int $id
  270. * @return \think\Response
  271. */
  272. public function delete($id)
  273. {
  274. //
  275. $row = $this->model::get($ids);
  276. if (!$row) {
  277. IResponse::failure('记录不存在');
  278. }
  279. Db::table('ins_users_motor_agent')->where('$ids',1)->delete();
  280. IResponse::success([],'操作成功');
  281. }
  282. }