Commission.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace backend\models;
  3. use backend\helpers\DateTimeHelper;
  4. use Yii;
  5. /**
  6. * This is the model class for table "crm_commission".
  7. *
  8. * @property integer $id
  9. * @property integer $login
  10. * @property integer $member_id
  11. * @property double $forex
  12. * @property double $metal
  13. * @property double $cfd
  14. * @property double $gold
  15. * @property double $silver
  16. * @property double $wy
  17. * @property double $stock
  18. * @property double $btc
  19. * @property integer $in_time
  20. */
  21. class Commission extends \yii\db\ActiveRecord
  22. {
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName()
  27. {
  28. return 'crm_commission';
  29. }
  30. /**
  31. * @return \yii\db\Connection the database connection used by this AR class.
  32. */
  33. public static function getDb()
  34. {
  35. return Yii::$app->get('dbXcrm');
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['login', 'member_id', 'in_time'], 'required'],
  44. [['login', 'member_id', 'in_time'], 'integer'],
  45. [['forex', 'metal', 'cfd', 'gold', 'silver', 'wy', 'stock', 'btc'], 'number'],
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => '主键ID',
  55. 'login' => 'MT4登录ID',
  56. 'member_id' => '上级代理商ID',
  57. 'forex' => 'forex每手佣金',
  58. 'metal' => 'metal每手佣金',
  59. 'cfd' => 'cfd',
  60. 'gold' => '黄金',
  61. 'silver' => '白银',
  62. 'wy' => '外佣',
  63. 'stock' => '股指',
  64. 'btc' => '比特币',
  65. 'in_time' => '创建时间',
  66. ];
  67. }
  68. /**
  69. * 根据member_id和logins获取佣金率,返回二位数组,键为login
  70. * @param int $member_id
  71. * @param array $logins
  72. * @return array
  73. */
  74. public function getCommissionByMemberIdAndLogins($member_id, $logins)
  75. {
  76. $commissionWhere = [
  77. 'and',
  78. ['=', 'member_id', $member_id],
  79. ['in', 'login', $logins],
  80. ];
  81. $commissionList = Commission::find()->where($commissionWhere)->asArray()->all();
  82. $commissionList2 = [];
  83. foreach ($commissionList as $k => $v) {
  84. $commissionList2[$v['login']] = $v;
  85. }
  86. return $commissionList2;
  87. }
  88. /**
  89. * 批量设置返佣规则
  90. * @param array $post
  91. * @return array
  92. */
  93. public function batchSet($post)
  94. {
  95. $result = ['code' => 0, 'data' => [], 'message' => ''];
  96. $userId = isset($post['userId']) ? intval($post['userId']) : 0;
  97. $ibMemberId = isset($post['ibMemberId']) ? intval($post['ibMemberId']) : 0;
  98. $setType = isset($post['setType']) ? $post['setType'] : '';
  99. $subIbLogins = isset($post['subordinateIbLogins']) ? intval($post['subordinateIbLogins']) : 0;
  100. $forex = isset($post['forex']) ? floatval($post['forex']) : 0;
  101. $metal = isset($post['metal']) ? floatval($post['metal']) : 0;
  102. $cfd = isset($post['cfd']) ? floatval($post['cfd']) : 0;
  103. $gold = isset($post['gold']) ? floatval($post['gold']) : 0;
  104. $silver = isset($post['silver']) ? floatval($post['silver']) : 0;
  105. $wy = isset($post['wy']) ? floatval($post['wy']) : 0;
  106. $stock = isset($post['stock']) ? floatval($post['stock']) : 0;
  107. $btc = isset($post['btc']) ? floatval($post['btc']) : 0;
  108. if (!$ibMemberId || !in_array($setType, ['direct', 'indirect'])) {
  109. $result['message'] = '参数错误';
  110. return $result;
  111. }
  112. $member = Member::findById($ibMemberId);
  113. if (!$member) {
  114. $result['message'] = '该代理商不存在';
  115. return $result;
  116. }
  117. $customerList = [];
  118. if ($setType === 'direct') {
  119. // 直属返佣
  120. $customerList = Mt4Users::findChildLogins($ibMemberId);
  121. } else {
  122. // 差佣
  123. $subIbMemberList = Member::find()->andWhere("FIND_IN_SET({$subIbLogins}, logins)")
  124. ->andWhere(['type' => Member::MEMBER_TYPE_IB])
  125. ->asArray()
  126. ->all();
  127. if (!$subIbMemberList) {
  128. $result['message'] = '未找到该账户';
  129. return $result;
  130. }
  131. $subIbMemberIds = array_column($subIbMemberList, 'id');
  132. $memberModel = new Member();
  133. $members = $memberModel->findChildren($ibMemberId);
  134. if ($members) {
  135. $memberIds = array_column($members, 'id');
  136. foreach ($subIbMemberIds as $k => $v) {
  137. if (in_array($v, $memberIds)) {
  138. $customerList = array_merge($customerList, Mt4Users::findChildLogins($v));
  139. }
  140. }
  141. }
  142. }
  143. $customerList2 = [];
  144. foreach ($customerList as $k => $v) {
  145. $customerList2[$v['id']] = $v;
  146. }
  147. $customerList = $customerList2;
  148. foreach ($customerList as $k => $v) {
  149. $oldCommission = static::find()->where(['member_id' => $ibMemberId, 'login' => $v['login']])->limit(1)->one();
  150. if ($oldCommission) {
  151. $oldCommission->delete();
  152. }
  153. $commission = new static();
  154. $commission->login = $v['login'];
  155. $commission->member_id = $ibMemberId;
  156. $commission->forex = $forex;
  157. $commission->metal = $metal;
  158. $commission->cfd = $cfd;
  159. $commission->gold = $gold;
  160. $commission->silver = $silver;
  161. $commission->wy = $wy;
  162. $commission->stock = $stock;
  163. $commission->btc = $btc;
  164. $commission->in_time = round(microtime(true) * 1000);
  165. if ($commission->save()) {
  166. $commissionLog = new CommissionLog();
  167. $commissionLog->login = $v['login'];
  168. $commissionLog->member_id = $ibMemberId;
  169. $commissionLog->type = $setType;
  170. $commissionLog->forex = $forex;
  171. $commissionLog->metal = $metal;
  172. $commissionLog->cfd = $cfd;
  173. $commissionLog->gold = $gold;
  174. $commissionLog->silver = $silver;
  175. $commissionLog->wy = $wy;
  176. $commissionLog->stock = $stock;
  177. $commissionLog->btc = $btc;
  178. $commissionLog->modify_user = $userId;
  179. $commissionLog->in_time = DateTimeHelper::microtime_float();
  180. $commissionLog->save();
  181. }
  182. }
  183. $result['code'] = 1;
  184. return $result;
  185. }
  186. /**
  187. * 删除指定MT4账号的返佣规则
  188. * @param int $login
  189. * @return bool|int
  190. */
  191. public static function deleteCommissionsByLogin($login)
  192. {
  193. if ($login == null) {
  194. return false;
  195. }
  196. return static::deleteAll(['login' => $login]);
  197. }
  198. /**
  199. * 查询指定login和代理商id的返佣规则
  200. * @param int $login
  201. * @param int $memberId
  202. * @return array|null|\yii\db\ActiveRecord
  203. */
  204. public static function findByCommission($login, $memberId)
  205. {
  206. return static::find()->where(['login' => $login, 'member_id' => $memberId])->limit(1)->asArray()->one();
  207. }
  208. }