ScoreExpansion.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace addons\ScoreExpansion\common\models;
  3. use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
  4. use common\models\order\OrderDetail;
  5. use common\models\order\Order;
  6. use common\enums\AddonsEnum;
  7. use common\enums\StatusEnum;
  8. use Yii;
  9. use yii\db\Exception;
  10. /**
  11. * This is the model class for table "{{%addons_score_expansion}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城ID
  15. * @property string $title 活动标题
  16. * @property string $description 活动描述
  17. * @property string|null $cover 活动封面
  18. * @property float $award 总积分
  19. * @property float $virtual_award 虚拟积分
  20. * @property float $day_award 每日总积分
  21. * @property int $grab_user_num 已抢积分总人数
  22. * @property float $grab_integral 已抢积分总数额
  23. * @property float $surplus_integral 剩余积分总数额
  24. * @property int|null $data_at 数据更新时间
  25. * @property string $time_type 活动时间类型[1:每日积分,2:时间段积分]
  26. * @property string $start_time 活动开始时间
  27. * @property string $end_time 活动结束时间
  28. * @property int $integral_type 积分活动类型[1:冻结到激活积分,2:冻结积分,3:激活积分]
  29. * @property string|null $rule_values 规则条件的值集合 [
  30. * first_money_start:第一次抢积分数额最小值
  31. * first_money_end:第一次抢积分数额最大值
  32. * everyday_invitation:每日邀请人数
  33. * everyday_invitation_start:每日邀请人数可抢积分一次,单次数额在最小值
  34. * everyday_invitation_end:每日邀请人数可抢积分一次,单次数额在最大值
  35. * times:每日活动最多抢次数
  36. * ]
  37. * @property string $auth 参与权限
  38. * @property string|null $rule 规则说明
  39. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  40. * @property string|null $limit_user_level 会员限制
  41. * @property int $created_at
  42. * @property int $updated_at
  43. */
  44. class ScoreExpansion extends \common\models\base\BaseActiveRecord
  45. {
  46. const DAILY_INTEGRAL = 1;//每日积分
  47. const TIME_SLOT_INTEGRAL = 2;//时间段积分
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public static function tableName()
  52. {
  53. return '{{%addons_score_expansion}}';
  54. }
  55. public static function briefTableName()
  56. {
  57. return 'addons_score_expansion';
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function rules()
  63. {
  64. return [
  65. [['mall_id', 'grab_user_num', 'data_at', 'integral_type', 'status', 'start_time', 'end_time'], 'integer'],
  66. [['award', 'day_award', 'grab_integral', 'surplus_integral','virtual_award'], 'number'],
  67. [['rule_values', 'rule','limit_user_level'], 'string'],
  68. [['title'], 'string', 'max' => 100],
  69. [['description', 'cover', 'auth'], 'string', 'max' => 255],
  70. [['time_type'], 'string', 'max' => 45],
  71. ];
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function attributeLabels()
  77. {
  78. return [
  79. 'id' => 'ID',
  80. 'mall_id' => '商城ID',
  81. 'title' => '活动标题',
  82. 'description' => '活动描述',
  83. 'cover' => '活动封面',
  84. 'award' => '总积分',
  85. 'virtual_award' => '虚拟积分',
  86. 'day_award' => '每日积分池',
  87. 'grab_user_num' => '已抢积分总人数',
  88. 'grab_integral' => '已抢积分总金额',
  89. 'surplus_integral' => '剩余积分总数额',
  90. 'data_at' => '数据更新时间',
  91. 'time_type' => '活动时间类型[1:每日积分,2:时间段积分]',
  92. 'start_time' => '活动开始时间',
  93. 'end_time' => '活动结束时间',
  94. 'integral_type' => '红包活动类型[1:冻结到激活积分,2:增值冻结积分,3:增值激活积分]',
  95. 'rule_values' => '规则条件的值集合',
  96. 'auth' => '参与权限',
  97. 'rule' => '规则说明',
  98. 'status' => '状态[-1:删除;0:禁用;1启用]',
  99. 'limit_user_level' => '会员限制',
  100. 'created_at' => '创建时间',
  101. 'updated_at' => '更新时间',
  102. ];
  103. }
  104. /**
  105. * @Author: ltm
  106. * @DateTime: 2022/3/18 0018 16:21
  107. * @Copyright: copyright (c) 2021 广东七件事集团
  108. * @param $data
  109. * @return string
  110. */
  111. public static function setData(array $params){
  112. try{
  113. if (!empty($params['id'])) {
  114. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  115. if($params['type'] == StatusEnum::ENABLED && $model->time_type == StatusEnum::ENABLED){ //每日积分活动重新启用的时候更新开始时间和结束时间
  116. $daytime = strtotime(date("Y-m-d"));
  117. $params['start_time'] = $daytime + strtotime(date("Y-m-d H:i:s",$model->start_time)) - strtotime(date("Y-m-d",$model->start_time));
  118. $params['end_time'] = $daytime + strtotime(date("Y-m-d H:i:s",$model->end_time)) - strtotime(date("Y-m-d",$model->end_time));
  119. }
  120. if (empty($model)) throw new Exception('记录不存在或已删除');
  121. }else{
  122. $model = new self();
  123. $model->loadDefaultValues();
  124. }
  125. $model->mall_id = $params['mall_id'];
  126. // echo '<pre>';print_r($params);die;
  127. if(!$model->load($params,'') || !$model->save()){
  128. throw new Exception($model->getErrorMessage());
  129. }
  130. return $model;
  131. }catch(\Exception $e){
  132. self::$static_error = $e->getMessage();
  133. return false;
  134. }
  135. }
  136. /**
  137. * 获取商品的优惠券设置
  138. * @Author: lun
  139. * @DateTime: 2022/5/12 0012 14:49
  140. * @Copyright: copyright (c) 2021 广东七件事集团
  141. * @param $params
  142. * @return bool|string[]
  143. */
  144. public static function getGoodSetting($params)
  145. {
  146. // 后台页面组件展示
  147. if ($params['type'] == 'show_component') {
  148. return [
  149. 'component' => 'com-score-expansion',
  150. 'path' => Yii::$app->basePath . '/../addons/ScoreExpansion/backend/views/goods',
  151. ];
  152. } else {
  153. try {
  154. if (empty($params['setting'][AddonsEnum::ADDONS_NAME_SCORE_EXPANSION])) return true;
  155. $setting = $params['setting'][AddonsEnum::ADDONS_NAME_SCORE_EXPANSION];
  156. // 数据保存
  157. $res = ScoreExpansionGoodsSetting::setGoodsSetting($params['mall_id'], $params['goods_id'], $setting);
  158. if ($res === false) throw new Exception(ScoreExpansionGoodsSetting::getStaticError());
  159. return true;
  160. } catch (Exception $e) {
  161. self::setStaticError($e->getMessage());
  162. return false;
  163. }
  164. }
  165. }
  166. /**
  167. * 分润明细类型
  168. * @return array[]
  169. */
  170. public function getGoodsModeCommssionType()
  171. {
  172. return [
  173. [
  174. 'name' => 'integral',
  175. 'label' => '激活积分'
  176. ],
  177. [
  178. 'name' => 'frozen_integral',
  179. 'label' => '冻结积分'
  180. ],
  181. ];
  182. }
  183. /**
  184. * 获取订单分润列表
  185. * @Author: lun
  186. * @DateTime: 2023/10/33 0012 14:49
  187. * @Copyright: copyright (c) 2021 广东七件事集团
  188. * @param $params
  189. * @return bool|string[]
  190. */
  191. public static function getOrderListTab($params)
  192. {
  193. if (!isset($params['list']) || !is_array($params['list'])) return [];
  194. $order_id = $params['order_id'] ?? 0;
  195. $order_detail_id = $params['order_detail_id'] ?? null;
  196. $mall_id = $params['mall_id'] ?? 0;
  197. if ($order_id <= 0 && empty($order_detail_id)) return [];
  198. $wallet_type = $params['wallet_type'] ?? '';
  199. if (!in_array($wallet_type, [ScoreExpansionEnum::WALLET_INTEGRAL, ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN])) {
  200. return [];
  201. }
  202. $order_detail_id_arr = $order_detail_id;
  203. if (!$order_detail_id_arr) {
  204. //查询order_detail
  205. $params = [];
  206. $params['where'] = [
  207. ['order_id' => $order_id],
  208. ['mall_id' => $mall_id],
  209. ];
  210. $order_detail_list = OrderDetail::lists($params);
  211. $order_detail_id_arr = array_column($order_detail_list, 'id');
  212. }
  213. $cond = [];
  214. $cond['where'] = [
  215. ['integral_wallet_type' => $wallet_type],
  216. //['source_table_id' => $order_id],
  217. // ['source_table' => '{{%order}}'],
  218. ['mall_id' => $mall_id],
  219. ['or',
  220. ['and',['source_table_id' => $order_id],['source_table' => Order::tableName()]],
  221. ['and',['source_table_id' => $order_detail_id_arr],['source_table' => 'order_detail']],
  222. ]
  223. ];
  224. // if($order_detail_id_arr){
  225. // $cond['where'][] = ['or',
  226. // ['or',['source_table_id' => $order_id],['source_table' => 'order']],
  227. // ['or',['source_table_id' => $order_detail_id_arr],['source_table' => 'order_detail']],
  228. // ];
  229. // }else{
  230. // $cond['where'][] = ['source_table_id' => $order_id];
  231. // $cond['where'][] = ['source_table' => Order::tableName()];
  232. // }
  233. $cond['with'] = ['user' => function ($query) {
  234. $query->select('id,nickname,avatar_url');
  235. }];
  236. $list = ScoreExpansionWalletLog::lists($cond);
  237. $wallet_log_status = ScoreExpansionEnum::getRedpackText();
  238. $from_type_map = ScoreExpansionEnum::getFromType();
  239. $addons_name_map = AddonsEnum::getAddonsNameMap();
  240. foreach ($list as &$value) {
  241. // $value['is_frozen'] = $wallet_log_status[$value['integral_wallet_type']] ?? '';//状态
  242. $value['is_frozen'] = '已结算';//状态
  243. $value['capital_type_text'] = $from_type_map[$value['from_type']] ?? '';//类型
  244. $value['from_type_text'] = $addons_name_map[ScoreExpansionEnum::ADDONS_NAME] ?? '';
  245. $value['nickname'] = $value['user']['nickname'];
  246. $value['avatar_url'] = $value['user']['avatar_url'];
  247. unset($value['user']['avatar_url'], $value['user']['nickname']);
  248. }
  249. return $list;
  250. }
  251. }