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.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace common\models;
  3. use common\util\Util;
  4. use Yii;
  5. use yii\db\Expression;
  6. /**
  7. * This is the model class for table "fx_commission_info".
  8. *
  9. * @property integer $id
  10. * @property integer $fx_uid
  11. * @property string $total_money
  12. * @property integer $order_id
  13. * @property integer $status
  14. * @property integer $delete_flag
  15. * @property string $create_time
  16. * @property string $update_time
  17. * @property string $update_user
  18. */
  19. class FxCommissionInfo extends \yii\db\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return 'fx_commission_info';
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['fx_uid', 'total_money', 'order_id'], 'required'],
  35. [['fx_uid', 'order_id', 'status', 'delete_flag'], 'integer'],
  36. [['total_money'], 'number'],
  37. [['create_time', 'update_time'], 'safe'],
  38. [['update_user'], 'string', 'max' => 50],
  39. ];
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'fx_uid' => 'Fx Uid',
  49. 'total_money' => 'Total Money',
  50. 'order_id' => 'Order ID',
  51. 'status' => 'Status',
  52. 'delete_flag' => 'Delete Flag',
  53. 'create_time' => 'Create Time',
  54. 'update_time' => 'Update Time',
  55. 'update_user' => 'Update User',
  56. ];
  57. }
  58. public function finishCommission()
  59. {
  60. $cnt = 0;
  61. $count = 0;
  62. //1.找出今天-完成日期大于3天的订单号,且佣金待结算
  63. $LogOrder = self::find()->select(['GROUP_CONCAT(a.order_id) as order_arr'])
  64. ->from('order_main as a')
  65. ->leftJoin('fx_commission_info as b', 'a.order_id=b.order_id')
  66. ->where(['and', ['=', 'order_status', 3], ['>', 'DATEDIFF(current_date,end_date)', 2], ['=', 'a.delete_flag', 0], ['=', 'b.`status`', 1]])
  67. ->asArray()
  68. ->all();
  69. $uptOrder = explode(',', $LogOrder[0]['order_arr']);
  70. if (empty($uptOrder['0']) == false) {
  71. //2.佣金表待结算变已结算
  72. $count = self::updateAll(['status' => 2], ['and', ['=', 'status', 1], ['in', 'order_id', $uptOrder]]);
  73. //3.不同用户的账户表分别更新
  74. //查出需要更新的分销账户及佣金金额
  75. $fx_user = self::find()->select(['fx_uid', 'SUM(total_money) as money'])
  76. ->from(self::tableName())
  77. ->where(['and', ['in', 'order_id', $uptOrder], ['=', 'delete_flag', 0]])
  78. ->groupBy('fx_uid')
  79. ->asArray()
  80. ->all();
  81. if (count($fx_user) != 0) {
  82. $fx_user_account = new FxUserAccount();
  83. foreach ($fx_user as $val) {
  84. $available_commission = $val['money'];
  85. $fx_uid = $val['fx_uid'];
  86. $array = ['available_commission' => new Expression("available_commission +$available_commission"), 'total_commission' => new Expression("total_commission +$available_commission")];
  87. $cnt += $fx_user_account::updateAll($array, ['and', ['=', 'fx_uid', $fx_uid]]);
  88. }
  89. }
  90. foreach ($uptOrder as $val) {
  91. Util::interfaceZzcx(['type' => 3, 'order_id' => $val], '/zzcx/interfaces/order/send-wx-msg', 1);
  92. }
  93. Util::addOrderLog(0, 0, implode('-', $uptOrder), '佣金结算', 7);
  94. }
  95. return ['order_list' => $LogOrder[0]['order_arr'], 'count' => $count, 'cnt' => $cnt];
  96. }
  97. /**
  98. * Function Description:佣金明细
  99. * Function Name: getInfoAdmin
  100. * @param $order_id
  101. *
  102. * @return array|\yii\db\ActiveRecord[]
  103. *
  104. * @author 娄梦宁
  105. */
  106. public function getInfoAdmin($order_id)
  107. {
  108. $result = self::find()
  109. ->from('order_info a')
  110. ->innerJoin('order_main c', 'a.order_id = c.order_id')
  111. ->leftJoin('prod_main b', 'a.prod_id=b.prod_id')
  112. ->select(['b.prod_name', 'a.unit_price prod_single_price', 'a.count prod_count', 'a.commission' // update fuhc add b.commission on 2017-05-26
  113. // '(select TRUNCATE(total_money/c.prod_cnt,0)*a.count from fx_commission_info where order_id =a.order_id ) as commission'
  114. ])
  115. ->where(['and', ['=', 'a.order_id', $order_id], ['!=', 'a.count', 0]])
  116. ->asArray()
  117. ->all();
  118. return $result;
  119. }
  120. /**
  121. * Des:根据订单ID获取佣金详细
  122. * Name: getInfoByOrderId
  123. * @param $oder_id
  124. * @return array
  125. * @author 倪宗锋
  126. */
  127. public function getInfoByOrderId($oder_id)
  128. {
  129. $orderInfo = self::find()
  130. ->from(static::tableName())
  131. ->where(['=', 'order_id', $oder_id])
  132. ->asArray()
  133. ->one();
  134. return $orderInfo;
  135. }
  136. /**
  137. * Des:修改单状态
  138. * Name: editStatus
  139. * @param $oder_id
  140. * @param $status
  141. * @return int
  142. * @author 倪宗锋
  143. */
  144. public function editStatus($oder_id, $status)
  145. {
  146. $flag = self::updateAll(['status' => $status], ['=', 'order_id', $oder_id]);
  147. return $flag;
  148. }
  149. }