Division.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\agent;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\agent\DivisionAgentApplyServices;
  14. use app\services\agent\DivisionServices;
  15. use app\services\other\AgreementServices;
  16. use app\services\user\UserServices;
  17. use crmeb\exceptions\AdminException;
  18. use think\facade\App;
  19. /**
  20. * 事业部控制器
  21. * Class Division
  22. * @package app\adminapi\controller\v1\agent
  23. */
  24. class Division extends AuthController
  25. {
  26. /**
  27. * @var DivisionServices
  28. */
  29. protected $services;
  30. /**
  31. * Division constructor.
  32. * @param App $app
  33. * @param DivisionServices $services
  34. */
  35. public function __construct(App $app, DivisionServices $services)
  36. {
  37. parent::__construct($app);
  38. $this->services = $services;
  39. }
  40. /**
  41. * 事业部列表
  42. * @return mixed
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function divisionList()
  48. {
  49. // 获取请求参数
  50. $where = $this->request->getMore([
  51. ['division_type', 0],
  52. ['keyword', '']
  53. ]);
  54. if ($where['division_type'] == 2) {
  55. $where['division_id'] = $this->adminInfo['division_id'];
  56. }
  57. // 调用服务层获取事业部列表
  58. $data = $this->services->getDivisionList($where);
  59. return app('json')->success($data);
  60. }
  61. /**
  62. * 下级列表
  63. * @return mixed
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function divisionDownList()
  69. {
  70. // 获取参数:事业部类型、用户ID
  71. [$type, $uid] = $this->request->getMore([
  72. ['division_type', 0],
  73. ['uid', 0],
  74. ], true);
  75. // 调用服务层获取下级列表
  76. $data = $this->services->divisionDownList($type, $uid);
  77. return app('json')->success($data);
  78. }
  79. /**
  80. * 添加编辑事业部
  81. * @param $uid
  82. * @return mixed
  83. * @throws \FormBuilder\Exception\FormBuilderException
  84. */
  85. public function divisionCreate($uid)
  86. {
  87. // 调用服务层获取事业部表单
  88. return app('json')->success($this->services->getDivisionForm((int)$uid));
  89. }
  90. /**
  91. * 保存事业部
  92. * @return mixed
  93. */
  94. public function divisionSave()
  95. {
  96. // 获取并验证请求数据
  97. $data = $this->request->postMore([
  98. ['uid', 0],
  99. ['aid', 0],
  100. ['division_percent', 0],
  101. ['division_end_time', ''],
  102. ['division_status', 1],
  103. ['account', ''],
  104. ['pwd', ''],
  105. ['conf_pwd', ''],
  106. ['division_name', ''],
  107. ['roles', []],
  108. ['image', []]
  109. ]);
  110. // 保存事业部数据
  111. $this->services->divisionSave($data);
  112. return app('json')->success('保存成功');
  113. }
  114. /**
  115. * 添加编辑代理商
  116. * @param $uid
  117. * @return mixed
  118. * @throws \FormBuilder\Exception\FormBuilderException
  119. */
  120. public function divisionAgentCreate($uid)
  121. {
  122. // 调用服务层获取代理商表单
  123. return app('json')->success($this->services->getDivisionAgentForm((int)$uid));
  124. }
  125. /**
  126. * 保存代理商
  127. * @param UserServices $userServices
  128. * @return mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function divisionAgentSave(UserServices $userServices)
  134. {
  135. // 获取并验证请求数据
  136. $data = $this->request->postMore([
  137. ['division_id', 0],
  138. ['uid', 0],
  139. ['division_percent', 0],
  140. ['division_end_time', ''],
  141. ['division_status', 1],
  142. ['division_name', ''],
  143. ['edit', 0],
  144. ['image', []],
  145. ]);
  146. if ((int)$data['uid'] == 0) $data['uid'] = $data['image']['uid'];
  147. // 验证用户信息
  148. $userInfo = $userServices->getUserInfo($data['uid'], 'is_division,is_agent,is_staff');
  149. if (!$userInfo) throw new AdminException('参数错误');
  150. if ($data['edit'] == 0) {
  151. if ($userInfo['is_division']) throw new AdminException('此用户是事业部,请勿添加为代理商');
  152. if ($userInfo['is_agent']) throw new AdminException('此用户是代理商,无法重复添加');
  153. if ($userInfo['is_staff']) throw new AdminException('此用户是下级员工,无法添加为代理商');
  154. // 验证事业部信息
  155. $divisionUserInfo = $userServices->count(['uid' => (int)$data['division_id'], 'is_division' => 1, 'division_id' => $data['division_id']]);
  156. if (!$divisionUserInfo) throw new AdminException('参数错误');
  157. }
  158. // 保存代理商数据
  159. $this->services->divisionAgentSave($data);
  160. return app('json')->success('保存成功');
  161. }
  162. /**
  163. * 设置状态
  164. * @param $status
  165. * @param $uid
  166. * @return mixed
  167. */
  168. public function setDivisionStatus($status, $uid)
  169. {
  170. // 调用服务层设置状态
  171. $this->services->setDivisionStatus($status, $uid);
  172. return app('json')->success('设置成功');
  173. }
  174. /**
  175. * 删除成功
  176. * @param $type
  177. * @param $uid
  178. * @return mixed
  179. */
  180. public function delDivision($type, $uid)
  181. {
  182. // 调用服务层删除事业部/代理商
  183. $this->services->delDivision($type, $uid);
  184. return app('json')->success('删除成功');
  185. }
  186. /**
  187. * 后台申请列表
  188. * @return mixed
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function AdminApplyList()
  194. {
  195. // 获取请求参数
  196. $where = $this->request->getMore([
  197. ['uid', 0],
  198. ['division_id', 0],
  199. ['division_invite', ''],
  200. ['status', ''],
  201. ['keyword', ''],
  202. ['time', ''],
  203. ]);
  204. $where['division_id'] = $this->adminInfo['division_id'];
  205. /** @var DivisionAgentApplyServices $applyServices */
  206. $applyServices = app()->make(DivisionAgentApplyServices::class);
  207. // 获取申请列表
  208. $data = $applyServices->AdminApplyList($where);
  209. return app('json')->success($data);
  210. }
  211. /**
  212. * 审核表单
  213. * @param $id
  214. * @param $type
  215. * @return mixed
  216. * @throws \FormBuilder\Exception\FormBuilderException
  217. */
  218. public function examineApply($id, $type)
  219. {
  220. /** @var DivisionAgentApplyServices $applyServices */
  221. $applyServices = app()->make(DivisionAgentApplyServices::class);
  222. // 获取审核表单
  223. $data = $applyServices->examineApply($id, $type);
  224. return app('json')->success($data);
  225. }
  226. /**
  227. * 代理商审核
  228. * @return mixed
  229. * @throws \think\db\exception\DataNotFoundException
  230. * @throws \think\db\exception\DbException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. */
  233. public function applyAgentSave()
  234. {
  235. // 获取审核参数
  236. $data = $this->request->getMore([
  237. ['type', 0],
  238. ['id', 0],
  239. ['division_percent', ''],
  240. ['division_end_time', ''],
  241. ['division_status', ''],
  242. ['refusal_reason', 0]
  243. ]);
  244. /** @var DivisionAgentApplyServices $applyServices */
  245. $applyServices = app()->make(DivisionAgentApplyServices::class);
  246. // 保存审核结果
  247. $data = $applyServices->applyAgentSave($data);
  248. return app('json')->success('设置成功');
  249. }
  250. /**
  251. * 删除代理商审核
  252. * @param $id
  253. * @return mixed
  254. */
  255. public function delApply($id)
  256. {
  257. /** @var DivisionAgentApplyServices $applyServices */
  258. $applyServices = app()->make(DivisionAgentApplyServices::class);
  259. // 删除申请记录
  260. $applyServices->delApply($id);
  261. return app('json')->success('删除成功');
  262. }
  263. /**
  264. * 添加员工表单
  265. * @param $uid
  266. * @return \think\Response
  267. * @throws \FormBuilder\Exception\FormBuilderException
  268. * @author 吴汐
  269. * @email 442384644@qq.com
  270. * @date 2024/1/22
  271. */
  272. public function divisionStaffCreate($uid)
  273. {
  274. // 调用服务层获取员工表单
  275. return app('json')->success($this->services->getDivisionStaffForm((int)$uid));
  276. }
  277. /**
  278. * 保存员工
  279. * @return \think\Response
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. * @author 吴汐
  284. * @email 442384644@qq.com
  285. * @date 2024/1/22
  286. */
  287. public function divisionStaffSave()
  288. {
  289. // 获取并验证请求数据
  290. $data = $this->request->getMore([
  291. ['uid', 0],
  292. ['division_percent', 0],
  293. ['agent_id', 0],
  294. ['image', []],
  295. ]);
  296. // 保存员工数据
  297. $this->services->divisionStaffSave($data);
  298. return app('json')->success('保存成功');
  299. }
  300. /**
  301. * 分销统计
  302. * @return \think\Response
  303. * @author wuhaotian
  304. * @email 442384644@qq.com
  305. * @date 2025/4/8
  306. */
  307. public function divisionStatistics()
  308. {
  309. // 获取请求参数:类型、时间、分页、排序
  310. [$type, $time, $page, $limit, $sort, $order] = $this->request->getMore([
  311. ['type', 0],
  312. ['time', ''],
  313. ['page', 1],
  314. ['limit', 15],
  315. ['sort', 'order_sum'],
  316. ['order', 'desc'],
  317. ], true);
  318. $time = $time != '' ? explode('-', $time) : [];
  319. // 获取统计数据
  320. $data = $this->services->divisionStatistics($type, $time, $page, $limit, $sort, $order);
  321. return app('json')->success($data);
  322. }
  323. }