You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

227 rivejä
6.0 KiB

  1. <?php
  2. namespace common\models;
  3. use fx\util\FxUtil;
  4. use Yii;
  5. use yii\base\Exception;
  6. /**
  7. * This is the model class for table "fx_user_account".
  8. *
  9. * @property integer $id
  10. * @property integer $fx_uid
  11. * @property string $available_commission
  12. * @property string $closed_commission
  13. * @property string $total_commission
  14. * @property string $apply_commission
  15. * @property string $create_time
  16. * @property string $update_time
  17. * @property integer $status
  18. * @property integer $delete_flag
  19. */
  20. class FxUserAccount extends \yii\db\ActiveRecord
  21. {
  22. /**
  23. * @inheritdoc
  24. */
  25. public static function tableName()
  26. {
  27. return 'fx_user_account';
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['fx_uid'], 'required'],
  36. [['fx_uid', 'status', 'delete_flag'], 'integer'],
  37. [['available_commission', 'closed_commission', 'total_commission', 'apply_commission', 'remaining_sum'], 'number'],
  38. [['create_time', 'update_time'], 'safe'],
  39. ];
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'fx_uid' => 'Fx Uid',
  49. 'available_commission' => 'Available Commission',
  50. 'closed_commission' => 'Closed Commission',
  51. 'total_commission' => 'Total Commission',
  52. 'apply_commission' => 'Apply Commission',
  53. 'create_time' => 'Create Time',
  54. 'update_time' => 'Update Time',
  55. 'status' => 'Status',
  56. 'delete_flag' => 'Delete Flag',
  57. ];
  58. }
  59. /**
  60. * Function Description:获取账户信息
  61. * Function Name: getMyCommission
  62. * @return array|null|\yii\db\ActiveRecord
  63. *
  64. * @author 娄梦宁
  65. */
  66. public function getMyCommission($fx_uid = '')
  67. {
  68. if (empty($fx_uid)) {
  69. $fx_uid = FxUtil::$uid;
  70. }
  71. $select = [
  72. 'a.headimgurl',
  73. 'a.phone',
  74. 'a.nickname',
  75. 'b.status as accountStatus',
  76. 'ifnull(b.available_commission,0) available_commission',
  77. 'ifnull(b.total_commission,0) total_commission',
  78. 'ifnull(b.apply_commission,0) apply_commission',
  79. 'FORMAT(ifnull(b.remaining_sum,0),0) remaining_sum_s',
  80. 'b.remaining_sum',
  81. 'wx_openid'
  82. ];
  83. $result = self::find()->select($select)
  84. ->from('fx_user as a')
  85. ->leftJoin(self::tableName() . ' as b', 'a.uid=b.fx_uid')
  86. ->where(['=', 'a.uid', $fx_uid])
  87. ->asArray()
  88. ->one();
  89. return $result;
  90. }
  91. public function ApplyCommission($available_commission, $new_apply_commission)
  92. {
  93. $values = [
  94. 'available_commission' => $available_commission,
  95. 'apply_commission' => $new_apply_commission
  96. ];
  97. $transaction = Yii::$app->db->beginTransaction();
  98. try {
  99. $base_one = self::findOne(['fx_uid' => FxUtil::$uid, 'delete_flag' => 0, 'status' => 1]);
  100. $base_one->attributes = $values;
  101. $res = $base_one->update();
  102. if (!$res) {
  103. throw new Exception('修改出错');
  104. }
  105. $transaction->commit();
  106. return true;
  107. } catch (Exception $e) {
  108. # 回滚事务
  109. $transaction->rollBack();
  110. return false;
  111. }
  112. }
  113. /**
  114. * Function Description:分销商注册手机号后开账户
  115. * Function Name: InsertNewUser
  116. *
  117. * @return bool
  118. *
  119. * @author 娄梦宁
  120. */
  121. public function InsertNewUser()
  122. {
  123. $values = [
  124. 'fx_uid' => FxUtil::$uid,
  125. 'create_time' => date('Y-m-d H:i:s')
  126. ];
  127. $this->attributes = $values;
  128. $res = $this->insert();
  129. if (!$res) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. /*
  135. * 提现后用户账户变化
  136. */
  137. public function changeAccount($apply_id)
  138. {
  139. $fx_info = FxCommissionApply::find()->select(['fx_uid', 'apply_money'])
  140. ->from('fx_commission_apply')
  141. ->where(['id' => $apply_id])
  142. ->asArray()
  143. ->one();
  144. $money = (int)$fx_info['apply_money'];
  145. $fx_uid = $fx_info['fx_uid'];
  146. $sql = "UPDATE fx_user_account
  147. set apply_commission=apply_commission -$money,
  148. closed_commission=closed_commission+$money
  149. where fx_uid=$fx_uid
  150. ";
  151. $res = $this->getDb()->createCommand($sql)->execute();
  152. if (!$res) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**
  158. * Des:修改分销账户状态
  159. * Name: changeStatus
  160. * @param $param
  161. * @return int
  162. * @author 倪宗锋
  163. */
  164. public function changeStatus($param)
  165. {
  166. if ($param['fx_status_id'] == 2) {
  167. $status = 1;
  168. } else {
  169. $status = 2;
  170. }
  171. $count = self::updateAll(['status' => $status], ['=', 'fx_uid', $param['fx_uid']]);
  172. return $count;
  173. }
  174. /**
  175. * Des:扣除用户余额
  176. * Name: changeStatus
  177. * @param $fx_uid
  178. * @param $money
  179. * @return bool
  180. * @author 倪宗锋
  181. */
  182. public function deductAmount($fx_uid, $money)
  183. {
  184. $sql = "UPDATE fx_user_account
  185. set remaining_sum=remaining_sum - $money
  186. where fx_uid=$fx_uid
  187. ";
  188. $res = $this->getDb()->createCommand($sql)->execute();
  189. if (!$res) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Des:用户余额充值
  196. * Name: changeStatus
  197. * @param $fx_uid
  198. * @param $money
  199. * @return bool
  200. * @author 倪宗锋
  201. */
  202. public function rechargeAmount($fx_uid, $money)
  203. {
  204. $sql = "UPDATE fx_user_account
  205. set remaining_sum=remaining_sum + $money
  206. where fx_uid=$fx_uid
  207. ";
  208. $res = $this->getDb()->createCommand($sql)->execute();
  209. if (!$res) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. }