| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace app\modules\client\models\v1\integralAppreciation;
- use app\models\IntegralAppreciationUser;
- use app\models\IntegralAppreciationUserIntegralLog;
- use app\models\Option;
- use app\models\User;
- use yii\base\Model;
- class IntegralForm extends Model
- {
- public $user;
- public $store_id;
- public $integral;
- public $type;
- public $give_user_id;
- public $time;
- public function rules()
- {
- return [
- [['type', 'store_id'], 'integer'],
- [['time'], 'string'],
- [['integral'], 'number'],
- [['give_user_id'], 'safe']
- ];
- }
- public function getList() {
- $user = $this->user;
- $type = $this->type;
- $time = $this->time;
- $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- $query = IntegralAppreciationUserIntegralLog::find()
- ->where(['integral_user_id' => $integral_user->id]);
- if (isset($type) && in_array($type, [IntegralAppreciationUserIntegralLog::TYPE_INCOME, IntegralAppreciationUserIntegralLog::TYPE_EXPEND])) {
- $query->andWhere(['type' => $type]);
- }
- if ($time > 0) {
- $start_time = strtotime($time);
- if (strpos($time, '-') !== false) {
- $end_time = strtotime('+1 month', strtotime($time));
- } else {
- $start_time = strtotime($time . '-01-01');
- $end_time = strtotime('+1 year', strtotime($time . '-01-01'));
- }
- $query->andWhere(['AND', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]]);
- }
- $query->orderBy('id DESC')->select('id, type, amount, source_type, created_at');
- $pagination = pagination_make($query);
- $first_log = IntegralAppreciationUserIntegralLog::find()->where(['integral_user_id' => $integral_user->id])
- ->orderBy('id ASC')->select('created_at')->asArray()->one();
- $last_log = IntegralAppreciationUserIntegralLog::find()->where(['integral_user_id' => $integral_user->id])
- ->orderBy('id DESC')->select('created_at')->asArray()->one();
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date('Y/m/d H:i:s', $item['created_at']);
- $item['type'] = intval($item['type']);
- $item['source_type'] = intval($item['source_type']);
- $item['source_type_text'] = IntegralAppreciationUserIntegralLog::$source_type_desc[$item['source_type']];
- }
- $pagination['date'] = self::handleDate($first_log['created_at'] ?: 0, $last_log['created_at'] ?: 0);
- $pagination['integral'] = $integral_user->integral;
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination
- ];
- }
- //将两个时间之间的年还有月返回成数组
- public static function handleDate($start_time, $end_time) {
- $date_arr = [];
- if ($start_time && $end_time) {
- $start_time = strtotime(date('Y-m', $start_time));
- $end_time = strtotime(date('Y-m', $end_time));
- $time = $start_time;
- while ($time <= $end_time) {
- if (!isset($date_arr[date('Y', $time)])) {
- $date_arr[date('Y', $time)] = [
- date('m', $time)
- ];
- } else {
- $date_arr[date('Y', $time)][] = date('m', $time);
- }
- $time = strtotime('+1 month', $time);
- }
- }
- $new_date = [];
- $i = 0;
- foreach ($date_arr as $index => $item) {
- $new_date[$i] = [
- 'value' => 0,
- 'label' => (string)$index,
- 'children' => []
- ];
- foreach ($item as $day_item) {
- $new_date[$i]['children'][] = [
- 'value' => $index . '-' . $day_item,
- 'label' => (string)$day_item
- ];
- }
- $new_date[$i]['children'][] = [
- 'value' => (string)$index,
- 'label' => '全部'
- ];
- ++$i;
- }
- array_push($new_date, [
- 'value' => 0,
- 'label' => '全部',
- 'children' => [
- [
- 'value' => 0,
- 'label' => '全部'
- ]
- ]
- ]);
- return $new_date;
- }
- //资产转赠
- public function giveAssets() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $user = $this->user;
- $store_id = $this->store_id;
- $give_user_id = $this->give_user_id;
- $integral = $this->integral;
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
- $integral_give_profit = $integral_appreciation_setting['integral_give_profit'] ?: 0;
- $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- if (!$integral_user) {
- throw new \Exception('暂无积分');
- }
- if ($integral_user->integral < $integral) {
- throw new \Exception('积分不足');
- }
- if ($user->id == $give_user_id) {
- throw new \Exception('不被允许的操作');
- }
- $give_user = User::findOne(['id' => $give_user_id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$give_user) {
- $give_user = User::findOne(['binding' => $give_user_id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$give_user) {
- throw new \Exception('用户不存在');
- }
- }
- $integral_user->integral = bcsub($integral_user->integral, $integral, 2);
- if (!$integral_user->save()) {
- throw new \Exception(json_encode($integral_user->errors, JSON_UNESCAPED_UNICODE));
- }
- $result = IntegralAppreciationUserIntegralLog::saveIntegralLog(
- $integral_user->id,
- $integral,
- IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
- IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_EXPEND
- );
- if ($result['code']) {
- throw new \Exception('减少转赠积分失败');
- }
- //计算手续费
- $integral_premium = bcdiv(bcmul($integral, $integral_give_profit, 2), 100, 2);
- $integral_after = bcsub($integral, $integral_premium, 2);
- $give_integral_user = IntegralAppreciationUser::findOne(['user_id' => $give_user->id]);
- if (!$give_integral_user) {
- $give_integral_user = new IntegralAppreciationUser();
- $give_integral_user->user_id = $give_user->id;
- $give_integral_user->store_id = $store_id;
- if (!$give_integral_user->save()) {
- throw new \Exception(json_encode($give_integral_user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $give_integral_user->total_integral = bcadd($give_integral_user->total_integral, $integral_after, 2);
- $give_integral_user->integral = bcadd($give_integral_user->integral, $integral_after, 2);
- if (!$give_integral_user->save()) {
- throw new \Exception(json_encode($give_integral_user->errors, JSON_UNESCAPED_UNICODE));
- }
- $result = IntegralAppreciationUserIntegralLog::saveIntegralLog(
- $give_integral_user->id,
- $integral_after,
- IntegralAppreciationUserIntegralLog::TYPE_INCOME,
- IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_INCOME,
- "收到转赠积分{$integral}减去手续费{$integral_premium}"
- );
- if ($result['code']) {
- throw new \Exception('增加转赠积分失败');
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '转赠成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- //获取用户积分数量
- public function getUserIntegral() {
- $user = $this->user;
- $store_id = $this->store_id;
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
- $integral_give_profit = $integral_appreciation_setting['integral_give_profit'] ?: 0;
- $integral_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'integral' => $integral_user->integral ?: 0,
- 'integral_give_profit' => $integral_give_profit
- ]
- ];
- }
- }
|