ScoreMallExchangeRecord.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace addons\ScoreMall\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_score_mall_exchange_record}}".
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $user_id
  11. * @property int $goods_id 商品ID
  12. * @property int $goods_attr_id 属性ID
  13. * @property string $exchange_price 兑换价格
  14. * @property string $goods 商品信息
  15. * @property int $order_id
  16. * @property int $num 兑换数量
  17. * @property string $order_no 订单号
  18. * @property int $status
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 修改时间
  21. */
  22. class ScoreMallExchangeRecord extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_score_mall_exchange_record}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules(){
  35. return [
  36. [['mall_id', 'user_id'], 'required'],
  37. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'goods_id', 'goods_attr_id', 'num', 'order_id'], 'integer'],
  38. [['order_no'], 'string', 'max' => 64],
  39. [['exchange_price','goods'], 'string'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels(){
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'user_id' => 'User ID',
  50. 'order_no' => '订单号',
  51. 'order_id' => '订单ID',
  52. 'goods_id' => '商品ID',
  53. 'goods_attr_id' => '属性ID',
  54. 'exchange_price'=> '兑换价格',
  55. 'goods' => '商品信息',
  56. 'num' => '兑换数量',
  57. 'status' => '状态[-1:删除;0:禁用;1启用]',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '修改时间'
  60. ];
  61. }
  62. /**
  63. * 保存数据
  64. * @Author: lun
  65. * @DateTime: 2022/1/12 0012 19:27
  66. * @Copyright: copyright (c) 2021 广东七件事集团
  67. * @param $params
  68. * @return bool
  69. */
  70. public static function setData($params)
  71. {
  72. try {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. if (!$model->load($params, '')) throw new Exception($model->getErrorMessage());
  76. if (!$model->save()) throw new Exception($model->getErrorMessage());
  77. return true;
  78. } catch (Exception $e) {
  79. self::setStaticError($e->getMessage());
  80. return false;
  81. }
  82. }
  83. public static function fixExchangePrice(){
  84. $i = 1;
  85. $page_size = 1000;
  86. while ($list = self::find()->where(['>', 'mall_id', 0])
  87. ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  88. $order_ids = array_column($list,'order_id');
  89. $order_list = \common\models\order\Order::lists(['where'=>[['id'=>$order_ids]],'index'=>'id']);
  90. foreach ($list as $item) {
  91. if(empty($order_list[$item['order_id']])) continue;
  92. $order = $order_list[$item['order_id']];
  93. $exchange_price = $order['extra_info'];
  94. $extra_info = json_decode($order['extra_info'],true);
  95. if(!empty($extra_info)&&is_array($extra_info)){
  96. $exchange_price = $extra_info[0]??'';
  97. }
  98. if($exchange_price !=$item['exchange_price']){
  99. $item->exchange_price = $exchange_price;
  100. $res = $item->save();
  101. var_dump($res);
  102. }
  103. }
  104. $i++;
  105. }
  106. }
  107. }