IntegralForm.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\modules\client\models\v1\integralAppreciation;
  3. use app\models\IntegralAppreciationUser;
  4. use app\models\IntegralAppreciationUserIntegralLog;
  5. use app\models\Option;
  6. use app\models\User;
  7. use yii\base\Model;
  8. class IntegralForm extends Model
  9. {
  10. public $user;
  11. public $store_id;
  12. public $integral;
  13. public $type;
  14. public $give_user_id;
  15. public $time;
  16. public function rules()
  17. {
  18. return [
  19. [['type', 'store_id'], 'integer'],
  20. [['time'], 'string'],
  21. [['integral'], 'number'],
  22. [['give_user_id'], 'safe']
  23. ];
  24. }
  25. public function getList() {
  26. $user = $this->user;
  27. $type = $this->type;
  28. $time = $this->time;
  29. $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  30. $query = IntegralAppreciationUserIntegralLog::find()
  31. ->where(['integral_user_id' => $integral_user->id]);
  32. if (isset($type) && in_array($type, [IntegralAppreciationUserIntegralLog::TYPE_INCOME, IntegralAppreciationUserIntegralLog::TYPE_EXPEND])) {
  33. $query->andWhere(['type' => $type]);
  34. }
  35. if ($time > 0) {
  36. $start_time = strtotime($time);
  37. if (strpos($time, '-') !== false) {
  38. $end_time = strtotime('+1 month', strtotime($time));
  39. } else {
  40. $start_time = strtotime($time . '-01-01');
  41. $end_time = strtotime('+1 year', strtotime($time . '-01-01'));
  42. }
  43. $query->andWhere(['AND', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]]);
  44. }
  45. $query->orderBy('id DESC')->select('id, type, amount, source_type, created_at');
  46. $pagination = pagination_make($query);
  47. $first_log = IntegralAppreciationUserIntegralLog::find()->where(['integral_user_id' => $integral_user->id])
  48. ->orderBy('id ASC')->select('created_at')->asArray()->one();
  49. $last_log = IntegralAppreciationUserIntegralLog::find()->where(['integral_user_id' => $integral_user->id])
  50. ->orderBy('id DESC')->select('created_at')->asArray()->one();
  51. foreach ($pagination['list'] as &$item) {
  52. $item['created_at'] = date('Y/m/d H:i:s', $item['created_at']);
  53. $item['type'] = intval($item['type']);
  54. $item['source_type'] = intval($item['source_type']);
  55. $item['source_type_text'] = IntegralAppreciationUserIntegralLog::$source_type_desc[$item['source_type']];
  56. }
  57. $pagination['date'] = self::handleDate($first_log['created_at'] ?: 0, $last_log['created_at'] ?: 0);
  58. $pagination['integral'] = $integral_user->integral;
  59. return [
  60. 'code' => 0,
  61. 'msg' => 'success',
  62. 'data' => $pagination
  63. ];
  64. }
  65. //将两个时间之间的年还有月返回成数组
  66. public static function handleDate($start_time, $end_time) {
  67. $date_arr = [];
  68. if ($start_time && $end_time) {
  69. $start_time = strtotime(date('Y-m', $start_time));
  70. $end_time = strtotime(date('Y-m', $end_time));
  71. $time = $start_time;
  72. while ($time <= $end_time) {
  73. if (!isset($date_arr[date('Y', $time)])) {
  74. $date_arr[date('Y', $time)] = [
  75. date('m', $time)
  76. ];
  77. } else {
  78. $date_arr[date('Y', $time)][] = date('m', $time);
  79. }
  80. $time = strtotime('+1 month', $time);
  81. }
  82. }
  83. $new_date = [];
  84. $i = 0;
  85. foreach ($date_arr as $index => $item) {
  86. $new_date[$i] = [
  87. 'value' => 0,
  88. 'label' => (string)$index,
  89. 'children' => []
  90. ];
  91. foreach ($item as $day_item) {
  92. $new_date[$i]['children'][] = [
  93. 'value' => $index . '-' . $day_item,
  94. 'label' => (string)$day_item
  95. ];
  96. }
  97. $new_date[$i]['children'][] = [
  98. 'value' => (string)$index,
  99. 'label' => '全部'
  100. ];
  101. ++$i;
  102. }
  103. array_push($new_date, [
  104. 'value' => 0,
  105. 'label' => '全部',
  106. 'children' => [
  107. [
  108. 'value' => 0,
  109. 'label' => '全部'
  110. ]
  111. ]
  112. ]);
  113. return $new_date;
  114. }
  115. //资产转赠
  116. public function giveAssets() {
  117. $t = \Yii::$app->db->beginTransaction();
  118. try {
  119. $user = $this->user;
  120. $store_id = $this->store_id;
  121. $give_user_id = $this->give_user_id;
  122. $integral = $this->integral;
  123. $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
  124. $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
  125. $integral_give_profit = $integral_appreciation_setting['integral_give_profit'] ?: 0;
  126. $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  127. if (!$integral_user) {
  128. throw new \Exception('暂无积分');
  129. }
  130. if ($integral_user->integral < $integral) {
  131. throw new \Exception('积分不足');
  132. }
  133. if ($user->id == $give_user_id) {
  134. throw new \Exception('不被允许的操作');
  135. }
  136. $give_user = User::findOne(['id' => $give_user_id, 'is_delete' => 0, 'store_id' => $store_id]);
  137. if (!$give_user) {
  138. $give_user = User::findOne(['binding' => $give_user_id, 'is_delete' => 0, 'store_id' => $store_id]);
  139. if (!$give_user) {
  140. throw new \Exception('用户不存在');
  141. }
  142. }
  143. $integral_user->integral = bcsub($integral_user->integral, $integral, 2);
  144. if (!$integral_user->save()) {
  145. throw new \Exception(json_encode($integral_user->errors, JSON_UNESCAPED_UNICODE));
  146. }
  147. $result = IntegralAppreciationUserIntegralLog::saveIntegralLog(
  148. $integral_user->id,
  149. $integral,
  150. IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
  151. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_EXPEND
  152. );
  153. if ($result['code']) {
  154. throw new \Exception('减少转赠积分失败');
  155. }
  156. //计算手续费
  157. $integral_premium = bcdiv(bcmul($integral, $integral_give_profit, 2), 100, 2);
  158. $integral_after = bcsub($integral, $integral_premium, 2);
  159. $give_integral_user = IntegralAppreciationUser::findOne(['user_id' => $give_user->id]);
  160. if (!$give_integral_user) {
  161. $give_integral_user = new IntegralAppreciationUser();
  162. $give_integral_user->user_id = $give_user->id;
  163. $give_integral_user->store_id = $store_id;
  164. if (!$give_integral_user->save()) {
  165. throw new \Exception(json_encode($give_integral_user->errors, JSON_UNESCAPED_UNICODE));
  166. }
  167. }
  168. $give_integral_user->total_integral = bcadd($give_integral_user->total_integral, $integral_after, 2);
  169. $give_integral_user->integral = bcadd($give_integral_user->integral, $integral_after, 2);
  170. if (!$give_integral_user->save()) {
  171. throw new \Exception(json_encode($give_integral_user->errors, JSON_UNESCAPED_UNICODE));
  172. }
  173. $result = IntegralAppreciationUserIntegralLog::saveIntegralLog(
  174. $give_integral_user->id,
  175. $integral_after,
  176. IntegralAppreciationUserIntegralLog::TYPE_INCOME,
  177. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_INCOME,
  178. "收到转赠积分{$integral}减去手续费{$integral_premium}"
  179. );
  180. if ($result['code']) {
  181. throw new \Exception('增加转赠积分失败');
  182. }
  183. $t->commit();
  184. return [
  185. 'code' => 0,
  186. 'msg' => '转赠成功'
  187. ];
  188. } catch (\Exception $e) {
  189. $t->rollBack();
  190. return [
  191. 'code' => 1,
  192. 'msg' => $e->getMessage() . $e->getLine()
  193. ];
  194. }
  195. }
  196. //获取用户积分数量
  197. public function getUserIntegral() {
  198. $user = $this->user;
  199. $store_id = $this->store_id;
  200. $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
  201. $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
  202. $integral_give_profit = $integral_appreciation_setting['integral_give_profit'] ?: 0;
  203. $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  204. return [
  205. 'code' => 0,
  206. 'msg' => 'success',
  207. 'data' => [
  208. 'integral' => $integral_user->integral ?: 0,
  209. 'integral_give_profit' => $integral_give_profit
  210. ]
  211. ];
  212. }
  213. }