AreaDividendForm.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\CashExt;
  4. use app\models\District;
  5. use app\models\Option;
  6. use app\models\SaasUser;
  7. use app\models\ShareHolder;
  8. use app\models\ShareHolderAreaAgentApply;
  9. use app\models\ShareHolderLevel;
  10. use app\models\User;
  11. use app\utils\Share\BonusPool;
  12. use yii\base\Model;
  13. class AreaDividendForm extends Model
  14. {
  15. public $id;
  16. public $store_id;
  17. public $is_open_area; //是否开启区域分红
  18. public $area_agent_banner_status; //是否开启申请页面banner图轮播
  19. public $area_agent_banner_images; //申请页面banner图
  20. public $area_name; //自定义区域分红名称
  21. public $status; //审核状态
  22. public $name;
  23. public $start_time;
  24. public $end_time;
  25. public $agent_rate;
  26. public $agent_rate_status;
  27. public $shareHolderCashProfit;
  28. public $pay_wechat;
  29. public $pay_alipay;
  30. public $bank;
  31. public $remaining_sum;
  32. public $lg;
  33. public $cash_max_day;
  34. public $cash_max_single_day;
  35. public $min_money;
  36. public $agent_type;
  37. public $province_id;
  38. public $city_id;
  39. public $district_id;
  40. public $town_id;
  41. public $cash_price_type;
  42. public $cash_price_amount;
  43. public $cash_price_integral;
  44. public $cash_price_balance;
  45. public $export;
  46. public function rules()
  47. {
  48. return [
  49. [['store_id', 'is_open_area', 'area_agent_banner_status', 'status', 'agent_rate_status', 'agent_type', 'province_id', 'city_id', 'district_id','export'], 'integer'],
  50. [['area_name', 'name', 'start_time', 'end_time', 'cash_price_type'], 'string'],
  51. [['agent_rate', 'shareHolderCashProfit', 'pay_wechat', 'pay_alipay', 'bank', 'lg', 'cash_max_day', 'cash_max_single_day', 'min_money',
  52. 'remaining_sum', 'cash_price_amount', 'cash_price_integral', 'cash_price_balance'], 'number'],
  53. [['area_agent_banner_images', 'id', 'town_id'], 'safe']
  54. ];
  55. }
  56. //获取配置信息
  57. public function getAreaAgentConfig() {
  58. // 区域分红状态
  59. $is_open_area = Option::get('is_open_area', $this->store_id, 'bonus_pool')['value'];
  60. //是否开启区域多代理
  61. $is_multi_agent = Option::get('is_multi_agent', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  62. $multi_agent_type1 = Option::get('multi_agent_type1', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  63. $multi_agent_type2 = Option::get('multi_agent_type2', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  64. $multi_agent_type3 = Option::get('multi_agent_type3', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  65. $multi_agent_type4 = Option::get('multi_agent_type4', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  66. // 申请设置
  67. // 轮播广告状态
  68. $area_agent_banner_status = Option::get('area_agent_banner_status', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  69. // 图片列表
  70. $area_agent_banner_images = Option::get('area_agent_banner_images', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  71. $area_agent_banner_images = json_decode($area_agent_banner_images, true);
  72. //客户端名称自定义
  73. $area_name = Option::get('area_name', $this->store_id, 'bonus_pool', '')['value'];
  74. $shareHolderCashProfit = Option::get('shareHolderCashProfit', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  75. $pay_wechat = Option::get('pay_wechat', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  76. $pay_alipay = Option::get('pay_alipay', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  77. $bank = Option::get('bank', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  78. $remaining_sum = Option::get('remaining_sum', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  79. $lg = Option::get('lg', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  80. $cash_max_day = Option::get('cash_max_day', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  81. $cash_max_single_day = Option::get('cash_max_single_day', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  82. $min_money = Option::get('min_money', $this->store_id, 'bonus_pool_area_agent', '')['value'];
  83. $cash_price_type = Option::get('cash_price_type', $this->store_id, 'bonus_pool_area_agent', '1')['value'];
  84. $cash_price_amount = Option::get('cash_price_amount', $this->store_id, 'bonus_pool_area_agent', 100)['value'];
  85. $cash_price_integral = Option::get('cash_price_integral', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  86. $cash_price_balance = Option::get('cash_price_balance', $this->store_id, 'bonus_pool_area_agent', 0)['value'];
  87. return [
  88. 'code' => 0,
  89. 'msg' => '获取成功',
  90. 'data' => [
  91. 'is_open_area' => $is_open_area,
  92. 'is_multi_agent' => $is_multi_agent,
  93. 'multi_agent_type1' => $multi_agent_type1,
  94. 'multi_agent_type2' => $multi_agent_type2,
  95. 'multi_agent_type3' => $multi_agent_type3,
  96. 'multi_agent_type4' => $multi_agent_type4,
  97. 'area_agent_banner_status' => (int)$area_agent_banner_status,
  98. 'area_agent_banner_images' => $area_agent_banner_images ?: [],
  99. 'area_name' => $area_name,
  100. 'shareHolderCashProfit' => $shareHolderCashProfit,
  101. 'pay_wechat' => $pay_wechat,
  102. 'pay_alipay' => $pay_alipay,
  103. 'bank' => $bank,
  104. 'remaining_sum' => $remaining_sum,
  105. 'cash_max_day' => $cash_max_day,
  106. 'cash_max_single_day' => $cash_max_single_day,
  107. 'min_money' => $min_money,
  108. 'lg' => $lg,
  109. 'cash_price_type' => $cash_price_type,
  110. 'cash_price_amount' => $cash_price_amount,
  111. 'cash_price_integral' => $cash_price_integral,
  112. 'cash_price_balance' => $cash_price_balance,
  113. ]
  114. ];
  115. }
  116. //配置信息
  117. public function setAreaAgentConfig($params = []) {
  118. try {
  119. $is_open_area = $this->is_open_area;
  120. $is_multi_agent = $params['is_multi_agent'] ?? 0;
  121. $multi_agent_type1 = $params['multi_agent_type1'] ?? 0;
  122. $multi_agent_type2 = $params['multi_agent_type2'] ?? 0;
  123. $multi_agent_type3 = $params['multi_agent_type3'] ?? 0;
  124. $multi_agent_type4 = $params['multi_agent_type4'] ?? 0;
  125. $area_agent_banner_status = $this->area_agent_banner_status;
  126. $area_name = $this->area_name;
  127. $area_agent_banner_images = json_encode($this->area_agent_banner_images);
  128. $shareHolderCashProfit = $this->shareHolderCashProfit;
  129. $pay_wechat = $this->pay_wechat;
  130. $pay_alipay = $this->pay_alipay;
  131. $bank = $this->bank;
  132. $remaining_sum = $this->remaining_sum;
  133. $cash_max_day = $this->cash_max_day;
  134. $cash_max_single_day = $this->cash_max_single_day;
  135. $min_money = $this->min_money;
  136. $store_id = $this->store_id;
  137. $lg = $this->lg;
  138. $cash_price_type = $this->cash_price_type ?? '1';
  139. $cash_price_amount = $this->cash_price_amount ?? 100;
  140. $cash_price_integral = $this->cash_price_integral ?? 0;
  141. $cash_price_balance = $this->cash_price_balance ?? 0;
  142. // $total_profit = 0;
  143. // $cash_price_type_ = explode(',', $cash_price_type);
  144. // if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type_)) {
  145. // $total_profit = bcadd($total_profit, $cash_price_amount, 2);
  146. // }
  147. // if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type_)) {
  148. // $total_profit = bcadd($total_profit, $cash_price_integral, 2);
  149. // }
  150. // if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type_)) {
  151. // $total_profit = bcadd($total_profit, $cash_price_balance, 2);
  152. // }
  153. // if ($total_profit != 100) {
  154. // return [
  155. // 'code' => 1,
  156. // 'msg' => '佣金/积分/余额比例设置错误'
  157. // ];
  158. // }
  159. $key = [
  160. // 'is_open_area',
  161. 'is_multi_agent',
  162. 'multi_agent_type1',
  163. 'multi_agent_type2',
  164. 'multi_agent_type3',
  165. 'multi_agent_type4',
  166. 'area_agent_banner_status',
  167. 'area_agent_banner_images',
  168. // 'area_name',
  169. 'shareHolderCashProfit',
  170. "pay_wechat",
  171. "pay_alipay",
  172. "bank",
  173. 'remaining_sum',
  174. "lg",
  175. "cash_max_day",
  176. "cash_max_single_day",
  177. "min_money",
  178. 'cash_price_type',
  179. 'cash_price_amount',
  180. 'cash_price_integral',
  181. 'cash_price_balance',
  182. ];
  183. Option::set($key, [
  184. // $is_open_area,
  185. $is_multi_agent,
  186. $multi_agent_type1,
  187. $multi_agent_type2,
  188. $multi_agent_type3,
  189. $multi_agent_type4,
  190. $area_agent_banner_status,
  191. $area_agent_banner_images,
  192. // $area_name,
  193. $shareHolderCashProfit,
  194. $pay_wechat,
  195. $pay_alipay,
  196. $bank,
  197. $remaining_sum,
  198. $lg,
  199. $cash_max_day,
  200. $cash_max_single_day,
  201. $min_money,
  202. $cash_price_type,
  203. $cash_price_amount,
  204. $cash_price_integral,
  205. $cash_price_balance
  206. ], $store_id, 'bonus_pool_area_agent');
  207. Option::set(['is_open_area', 'area_name'], [$is_open_area, $area_name], $store_id, 'bonus_pool');
  208. return [
  209. 'code' => 0,
  210. 'msg' => '保存成功'
  211. ];
  212. } catch (\Exception $e) {
  213. return [
  214. 'code' => 1,
  215. 'msg' => $e->getMessage()
  216. ];
  217. }
  218. }
  219. //处理区域代理申请
  220. public function handleAreaAgentApply() {
  221. try {
  222. $ids = $this->id;
  223. $ids = explode(',', $ids);
  224. $status = $this->status;
  225. $store_id = $this->store_id;
  226. foreach ($ids as $id) {
  227. $agent_apply = ShareHolderAreaAgentApply::findOne(['id' => $id, 'is_delete' => 0]);
  228. if (!$agent_apply) {
  229. throw new \Exception('数据不存在');
  230. }
  231. if (in_array($status, [1, 2]) && intval($agent_apply->status) !== 0) {
  232. throw new \Exception('不可重复操作');
  233. }
  234. if (!in_array($status, [1, 2, 3])) {
  235. throw new \Exception('状态错误');
  236. }
  237. if (in_array($status, [1, 2])) {
  238. $agent_apply->status = $status;
  239. $agent_apply->apply_time = time();
  240. } else {
  241. $agent_apply->is_delete = 1;
  242. }
  243. if (!$agent_apply->save()) {
  244. throw new \Exception($agent_apply->errors, JSON_UNESCAPED_UNICODE);
  245. }
  246. $name = '区域代理';
  247. $mobile = '';
  248. $user = User::findOne($agent_apply->user_id);
  249. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  250. if ($saas_user) {
  251. $name = $saas_user->name;
  252. $mobile = $saas_user->mobile;
  253. }
  254. // $params = json_decode($agent_apply->params, true);
  255. if (intval($status) === 1) {
  256. $shareHolderModel = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $agent_apply->user_id, 'is_delete' => 0]);
  257. if (!$shareHolderModel) {
  258. $currentLevel = ShareHolderLevel::findOne(['store_id' => $store_id, 'is_default' => 1, 'is_delete' => 0]);
  259. $shareHolderModel = new ShareHolder();
  260. $shareHolderModel->level_id = $currentLevel->id ?: 0;
  261. $shareHolderModel->store_id = $store_id;
  262. $shareHolderModel->user_id = $agent_apply->user_id;
  263. $shareHolderModel->name = $name;
  264. $shareHolderModel->address = '';
  265. $shareHolderModel->mobile = $mobile;
  266. $shareHolderModel->status = 1;
  267. $shareHolderModel->audit_time = time();
  268. $shareHolderModel->province_id = $shareHolderModel->province = ($agent_apply->province_id ?: 0);
  269. $shareHolderModel->city_id = $shareHolderModel->city = ($agent_apply->city_id ?: 0);
  270. $shareHolderModel->district_id = $shareHolderModel->district = ($agent_apply->district_id ?: 0);
  271. $shareHolderModel->town_id = $shareHolderModel->town_id = ($agent_apply->town_id ?: 0);
  272. $shareHolderModel->province_name = District::findOne($shareHolderModel->province_id)->name ?: '';
  273. $shareHolderModel->city_name = District::findOne($shareHolderModel->city_id)->name ?: '';
  274. $shareHolderModel->district_name = District::findOne($shareHolderModel->district_id)->name ?: '';
  275. $shareHolderModel->town_name = District::findOne($shareHolderModel->town_id)->name ?: '';
  276. if (!$shareHolderModel->save()) {
  277. throw new \Exception($shareHolderModel->errors, JSON_UNESCAPED_UNICODE);
  278. }
  279. // BonusPool::checkChildHolderUpdateLevel($agent_apply->user_id, $currentLevel->id);
  280. // BonusPool::checkLevel($store_id, 0, $agent_apply->user_id);
  281. BonusPool::ShareHolderLevelJob($store_id, 0, $agent_apply->user_id);
  282. if ((int)$currentLevel->member_level > (int)$user->level) {
  283. $user->level = $currentLevel->member_level;
  284. }
  285. $user->is_holder = 1;
  286. if (!$user->save()) {
  287. throw new \Exception($user->errors, JSON_UNESCAPED_UNICODE);
  288. }
  289. }
  290. $shareHolderModel->province_id = $shareHolderModel->province = $agent_apply->province_id;
  291. $shareHolderModel->city_id = $shareHolderModel->city = $agent_apply->city_id;
  292. $shareHolderModel->district_id = $shareHolderModel->district = $agent_apply->district_id;
  293. $shareHolderModel->town_id = $shareHolderModel->town_id = $agent_apply->town_id;
  294. $shareHolderModel->province_name = District::findOne($agent_apply->province_id)->name ?: '';
  295. $shareHolderModel->city_name = District::findOne($agent_apply->city_id)->name ?: '';
  296. $shareHolderModel->district_name = District::findOne($agent_apply->district_id)->name ?: '';
  297. $shareHolderModel->town_name = District::findOne($agent_apply->town_id)->name ?: '';
  298. $shareHolderModel->agent_type = $agent_apply->agent_type;
  299. $shareHolderModel->agent_time = time();
  300. if (!$shareHolderModel->save()) {
  301. throw new \Exception($shareHolderModel->errors, JSON_UNESCAPED_UNICODE);
  302. }
  303. $agent_apply->share_holder_id = $shareHolderModel->id;
  304. if (!$agent_apply->save()) {
  305. throw new \Exception($agent_apply->errors, JSON_UNESCAPED_UNICODE);
  306. }
  307. }
  308. }
  309. return [
  310. 'code' => 0,
  311. 'msg' => '操作成功'
  312. ];
  313. } catch (\Exception $e) {
  314. return [
  315. 'code' => 1,
  316. 'msg' => $e->getMessage()
  317. ];
  318. }
  319. }
  320. //区域代理申请列表
  321. public function getAreaAgentApplyList() {
  322. $store_id = $this->store_id;
  323. $name = $this->name;
  324. $status = $this->status;
  325. $start_time = $this->start_time;
  326. $end_time = $this->end_time;
  327. $query = ShareHolderAreaAgentApply::find()->alias('s')->where(['s.is_delete' => 0, 's.store_id' => $store_id])
  328. ->leftJoin(['u' => User::tableName()], 's.user_id = u.id')
  329. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile');
  330. if ($status !== null && in_array($status, [0, 1])) {
  331. $query->andWhere(['s.status' => $status]);
  332. }
  333. if ($name) {
  334. $query->andWhere(['LIKE', 'su.name', $name]);
  335. }
  336. if ($start_time) {
  337. $start_time = strtotime($start_time);
  338. $query->andWhere(['>=', 's.created_at', $start_time]);
  339. }
  340. if ($end_time) {
  341. $end_time = strtotime($end_time);
  342. $query->andWhere(['<=', 's.created_at', $end_time]);
  343. }
  344. $query->select('s.id, su.name, su.avatar, su.mobile, s.params, s.created_at, s.status');
  345. $pagination = pagination_make($query);
  346. foreach ($pagination['list'] as &$item) {
  347. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  348. $item['params'] = json_decode($item['params'], true);
  349. $item['status'] = (int)$item['status'];
  350. $item['agent_type'] = (int)$item['params']['agent_type'];
  351. $item['province'] = '';
  352. $item['city'] = '';
  353. $item['district'] = '';
  354. switch ($item['agent_type']) {
  355. case 1:
  356. $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name;
  357. break;
  358. case 2:
  359. $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name;
  360. $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name;
  361. break;
  362. case 3:
  363. $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name;
  364. $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name;
  365. $item['district'] = District::findOne(['id' => $item['params']['district_id']])->name;
  366. break;
  367. case 4:
  368. $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name;
  369. $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name;
  370. $item['district'] = District::findOne(['id' => $item['params']['district_id']])->name;
  371. $item['town'] = District::findOne(['id' => $item['params']['town_id']])->name;
  372. break;
  373. }
  374. }
  375. return [
  376. 'code' => 0,
  377. 'msg' => 'success',
  378. 'data' => [
  379. 'data' => $pagination['list'],
  380. 'pageNo' => $pagination['pageNo'],
  381. 'totalCount' => $pagination['totalCount'],
  382. ]
  383. ];
  384. }
  385. // 获取代理管理
  386. public function getAreaAgentList($params = []) {
  387. $store_id = $this->store_id;
  388. $name = $this->name;
  389. $status = $this->status;
  390. $start_time = $this->start_time;
  391. $end_time = $this->end_time;
  392. $query = ShareHolder::find()->alias('sh')->where(['sh.store_id' => $store_id, 'sh.is_delete' => 0, 'sh.status' => 1, 'u.is_delete' => 0])
  393. ->andWhere(['AND', ['IS NOT', 'u.id', NULL], ['>', 'agent_type', 0]])
  394. ->leftJoin(['u' => User::tableName()], 'u.id=sh.user_id')
  395. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding');
  396. if ($name) {
  397. $query->andWhere(['like', 'su.name', $name]);
  398. }
  399. if ($status !== null && in_array($status, [0, 1])) {
  400. $query->andWhere(['agent_rate_status' => $status]);
  401. }
  402. if ($params['agent_type'] > 0) {
  403. $query->andWhere(['agent_type' => $params['agent_type']]);
  404. }
  405. if ($params['province_id'] > 0) {
  406. $query->andWhere(['province_id' => $params['province_id']]);
  407. }
  408. if ($params['city_id'] > 0) {
  409. $query->andWhere(['city_id' => $params['city_id']]);
  410. }
  411. if ($params['district_id'] > 0) {
  412. $query->andWhere(['district_id' => $params['district_id']]);
  413. }
  414. if ($params['town_id'] > 0) {
  415. $query->andWhere(['town_id' => $params['town_id']]);
  416. }
  417. if ($start_time) {
  418. $start_time = strtotime($start_time);
  419. $query->andWhere(['>=', 'sh.created_at', $start_time]);
  420. }
  421. if ($end_time) {
  422. $end_time = strtotime($end_time);
  423. $query->andWhere(['<=', 'sh.created_at', $end_time]);
  424. }
  425. $query->select('sh.id, su.name, su.avatar, su.mobile, sh.agent_type, sh.agent_rate, sh.agent_rate_status, sh.created_at, sh.agent_time, sh.province_id ,sh.city_id ,sh.district_id ,sh.town_id');
  426. $pagination = pagination_make($query);
  427. if($this->export = input_params('export', 0)){
  428. $export_list = $query->asArray()->all();
  429. if ($export_list){
  430. foreach ($export_list as &$value){
  431. $district = District::find();
  432. switch ($value['agent_type']) {
  433. case 1:
  434. $address_arr = $district->andWhere(['id' => $value['province_id']])->select('name')->column();
  435. break;
  436. case 2:
  437. $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id']]])->select('name')->column();
  438. break;
  439. case 3:
  440. $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id'], $value['district_id']]])->select('name')->column();
  441. break;
  442. case 4:
  443. $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id'], $value['district_id'], $value['town_id']]])->select('name')->column();
  444. break;
  445. }
  446. $value['address'] = '';
  447. if (!empty($address_arr)) {
  448. $value['address'] = implode('', $address_arr);
  449. }
  450. }
  451. }
  452. return $this->export($export_list);
  453. }
  454. foreach ($pagination['list'] as &$item) {
  455. $item['created_at'] = date('Y-m-d H:i:s', $item['agent_time'] ? : $item['created_at']);
  456. $item['agent_time'] = date('Y-m-d H:i:s', $item['agent_time']);
  457. $item['agent_rate_status'] = (int)$item['agent_rate_status'];
  458. $item['agent_type'] = (int)$item['agent_type'];
  459. $district = District::find();
  460. switch ($item['agent_type']) {
  461. case 1:
  462. $address_arr = $district->andWhere(['id' => $item['province_id']])->select('name')->column();
  463. break;
  464. case 2:
  465. $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id']]])->select('name')->column();
  466. break;
  467. case 3:
  468. $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column();
  469. break;
  470. case 4:
  471. $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id'], $item['district_id'], $item['town_id']]])->select('name')->column();
  472. break;
  473. }
  474. $item['address'] = '';
  475. if (!empty($address_arr)) {
  476. $item['address'] = implode('', $address_arr);
  477. }
  478. $item['province'] = District::findOne($item['province_id'])->name ?: '';
  479. $item['city'] = District::findOne($item['city_id'])->name ?: '';
  480. $item['district'] = District::findOne($item['district_id'])->name ?: '';
  481. $item['town'] = District::findOne($item['town_id'])->name ?: '';
  482. $item['province_id'] = $item['province_id'] ?: '';
  483. $item['city_id'] = $item['city_id'] ?: '';
  484. $item['district_id'] = $item['district_id'] ?: '';
  485. $item['town_id'] = $item['town_id'] ?: '';
  486. }
  487. return [
  488. 'code' => 0,
  489. 'msg' => 'success',
  490. 'data' => [
  491. 'data' => $pagination['list'],
  492. 'pageNo' => $pagination['pageNo'],
  493. 'totalCount' => $pagination['totalCount'],
  494. ]
  495. ];
  496. }
  497. private function export($list)
  498. {
  499. $rows = [[
  500. 'ID',
  501. '用户名称',
  502. '手机号',
  503. '代理级别',
  504. '地址',
  505. '佣金比例',
  506. '通过时间',
  507. '是否开启',
  508. ]];
  509. foreach($list as $item){
  510. $r = [
  511. $item['id'],
  512. $item['name'],
  513. $item['mobile'],
  514. $item['agent_type']==1?'省':($item['agent_type']==2?'市':($item['agent_type']==3?'区':'镇')),
  515. $item['address'],
  516. $item['agent_rate'],
  517. $item['agent_time']? date('Y-m-d H:i:s', $item['agent_time']):'',
  518. $item['agent_rate_status'] == 1 ? '开启' : '关闭',
  519. ];
  520. $rows[] = $r;
  521. }
  522. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  523. ->addRows($rows)->toBrowser();
  524. }
  525. public function setAgentRate() {
  526. try {
  527. $ids = $this->id;
  528. $store_id = $this->store_id;
  529. $ids = explode(',', $ids);
  530. $agent_rate = $this->agent_rate;
  531. $agent_rate_status = $this->agent_rate_status;
  532. $agent_type = $this->agent_type;
  533. $province_id = $this->province_id;
  534. $city_id = $this->city_id;
  535. $district_id = $this->district_id;
  536. $town_id = $this->town_id ?: 0;
  537. $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value'];
  538. switch ($agent_type) {
  539. case 1:
  540. if (empty($province_id)) {
  541. throw new \Exception('缺失省级参数');
  542. }
  543. break;
  544. case 2:
  545. if (empty($province_id) || empty($city_id)) {
  546. throw new \Exception('缺失省市级参数');
  547. }
  548. break;
  549. case 3:
  550. if (empty($province_id) || empty($city_id) || empty($district_id)) {
  551. throw new \Exception('缺失省市区级参数');
  552. }
  553. break;
  554. case 4:
  555. if (empty($province_id) || empty($city_id) || empty($district_id) || empty($town_id)) {
  556. throw new \Exception('缺失省市区镇级参数');
  557. }
  558. break;
  559. }
  560. foreach ($ids as $id) {
  561. $shareHolder = ShareHolder::findOne($id);
  562. if (!$shareHolder) {
  563. throw new \Exception('数据不存在');
  564. }
  565. if (isset($agent_rate)) {
  566. $shareHolder->agent_rate = $agent_rate;
  567. }
  568. if ($agent_rate_status !== null && in_array($agent_rate_status, [0, 1])) {
  569. $shareHolder->agent_rate_status = $agent_rate_status;
  570. }
  571. if (intval($agent_rate_status) === 2) {
  572. // $shareHolder->is_delete = 1;
  573. $shareHolder->agent_type = 0;
  574. $shareHolder->province_id = $shareHolder->province = 0;
  575. $shareHolder->city_id = $shareHolder->city = 0;
  576. $shareHolder->district_id = $shareHolder->district = 0;
  577. }
  578. if ($agent_type) {
  579. if($shareHolder->agent_type == 0){
  580. $shareHolder->agent_time = time();
  581. }
  582. $shareHolder->agent_type = $agent_type;
  583. $shareHolder->province_id = $shareHolder->province = $province_id ?: 0;
  584. $shareHolder->city_id = $shareHolder->city = $city_id ?: 0;
  585. $shareHolder->district_id = $shareHolder->district = $district_id ?: 0;
  586. $shareHolder->town_id = $town_id ?: 0;
  587. $query = ShareHolder::find()->where(['agent_type' => $agent_type, 'is_delete' => 0, 'store_id' => $store_id]);//check_share_holder
  588. switch ($agent_type) {
  589. case 1:
  590. $query->andWhere(['province_id' => $province_id]);
  591. break;
  592. case 2:
  593. $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id]);
  594. break;
  595. case 3:
  596. $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id]);
  597. break;
  598. case 4:
  599. $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id, 'town_id' => $town_id]);
  600. break;
  601. }
  602. $check_share_holder = $query->andWhere(['<>', 'id', $id])->one();
  603. if ($check_share_holder && !$is_multi_agent) {
  604. throw new \Exception('存在相同区域的代理');
  605. }
  606. }
  607. if (!$shareHolder->save()) {
  608. throw new \Exception($shareHolder->errors, JSON_UNESCAPED_UNICODE);
  609. }
  610. $area_agent_apply = ShareHolderAreaAgentApply::findOne(['user_id' => $shareHolder->user_id, 'is_delete' => 0]);
  611. if ($area_agent_apply && intval($agent_rate_status) === 2) {
  612. $area_agent_apply->is_delete = 1;
  613. if (!$area_agent_apply->save()) {
  614. throw new \Exception($area_agent_apply->errors, JSON_UNESCAPED_UNICODE);
  615. }
  616. }
  617. }
  618. return [
  619. 'code' => 0,
  620. 'msg' => '操作成功'
  621. ];
  622. } catch (\Exception $e) {
  623. return [
  624. 'code' => 1,
  625. 'msg' => $e->getMessage()
  626. ];
  627. }
  628. }
  629. }