| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace addons\Store\common\models;
- use addons\Coupon\common\models\AddonsCouponGoodStatistics;
- use addons\Coupon\common\models\AddonsCouponStatistics;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\mall\MallAddons;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_coupon_use_log".
- *
- * @property int $id 主键
- * @property int $mall_id mall_id
- * @property int $user_id user_id
- * @property int $order_id order_id
- * @property int $coupon_id coupon_id
- * @property int $user_coupon_id user_coupon_id
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class StoreCouponUseLog extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_coupon_use_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => 'mall_id',
- 'user_id' => 'user_id',
- 'order_id' => 'order_id',
- 'coupon_id' => 'coupon_id',
- 'user_coupon_id' => 'user_coupon_id',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- public static function setData($params)
- {
- Yii::$app->db->close();
- try {
- $order_detail_id_arr = array_keys($params['order_detail_id_arr']);
- $list = StoreOrderMarketing::lists(['where' => [['order_detail_id' => $order_detail_id_arr]], 'select' => 'user_coupon_id,order_detail_id']);
- $user_coupon_id_arr = [];
- $temp_arr = [];
- $user_coupon_arr = [];
- $user_coupon_order_detail_id_arr = [];
- foreach ($list as $item) {
- $user_coupon_id = json_decode($item['user_coupon_id'], 'true');
- if (!empty($user_coupon_id)) {
- foreach ($user_coupon_id as $key => $val) {
- if(isset($user_coupon_arr[$key])){
- $user_coupon_arr[$key] += $val;
- }else{
- $user_coupon_arr[$key] = $val;
- }
- $user_coupon_order_detail_id_arr[$key][] = $item['order_detail_id'];
- $user_coupon_id_arr[] = $key;
- $temp_arr[$key] = $item['order_detail_id'];
- }
- }
- }
- if (!empty($user_coupon_id_arr)) {
- $coupon_user_relate_list = StoreCouponUserRelate::lists(['where' => [['id' => $user_coupon_id_arr]], 'select' => 'id,coupon_id,store_id,from_type']);
- $field = ['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'];
- $time = time();
- $insert = [];
- foreach ($coupon_user_relate_list as $val) {
- $arr['mall_id'] = $params['mall_id'];
- $arr['user_id'] = $params['user_id'];
- $arr['order_id'] = $params['order_detail_id_arr'][$temp_arr[$val['id']]];
- $arr['coupon_id'] = $val['coupon_id'];
- $arr['user_coupon_id'] = $val['id'];
- $arr['status'] = StatusEnum::ENABLED;
- $arr['created_at'] = $time;
- $arr['updated_at'] = $time;
- $insert[] = $arr;
- $set_data = [
- 'mall_id'=>$params['mall_id'],
- 'coupon_id'=>$val['coupon_id'],
- 'used_number'=>1,'use_user_number'=>1,'use_order_number'=>$params['counts']??0,
- 'total_payed_amount'=>$params['total_payed_amount']??0,'total_use_coupon_payed_amount'=>$params['total_use_coupon_payed_amount']??0,
- 'total_discount_amount'=>$user_coupon_arr[$val['id']]??0,
- ];
- $set_goods_data = [
- 'mall_id'=>$params['mall_id'],
- 'coupon_id'=>$val['coupon_id'],
- 'payed_number'=>0,'buyed_user_number'=>1,
- ];
- if(!empty($user_coupon_order_detail_id_arr[$val['id']])){
- $order_detail_ids = $user_coupon_order_detail_id_arr[$val['id']];
- foreach ($order_detail_ids as $order_detail_id){
- if(!empty($params['order_detail_arr'][$order_detail_id])){
- $set_goods_data['good_id'] = $params['order_detail_arr'][$order_detail_id]['goods_id'];
- $set_goods_data['payed_number'] = $params['order_detail_arr'][$order_detail_id]['num'];
- if($val['from_type']==1){
- if(MallAddons::isInstall($params['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
- AddonsCouponGoodStatistics::setData($set_goods_data);
- }
- }else{
- StoreCouponGoodStatistics::setData($set_goods_data);
- }
- }
- }
- }
- if($val['from_type']==1){
- if(MallAddons::isInstall($params['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
- AddonsCouponStatistics::setData($set_data);
- }
- }else{
- $set_data['store_id'] = $val['store_id'];
- StoreCouponStatistics::setData($set_data);
- }
- }
- // 批量插入数据
- $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- if ($res == false) throw new \Exception('添加优惠券使用记录失败');
- }
- return true;
- }catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|