StoreOrderMarketing.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Coupon\common\models\AddonsCouponGoodStatistics;
  4. use addons\Coupon\common\models\AddonsCouponStatistics;
  5. use common\enums\AddonsEnum;
  6. use common\enums\StatusEnum;
  7. use common\enums\UserWalletEnum;
  8. use common\models\base\BaseActiveRecord;
  9. use common\models\mall\MallAddons;
  10. use common\models\order\Order;
  11. use common\models\order\OrderDetail;
  12. use services\common\UserWalletService;
  13. use Yii;
  14. /**
  15. * This is the model class for table "qimall_addons_store_order_marketing".
  16. *
  17. * @property int $id 主键
  18. * @property int $mall_id mall_id
  19. * @property int $store_id store_id
  20. * @property int $order_detail_id 订单详情id
  21. * @property float $give_score 赠送积分
  22. * @property int $give_coupon_id 赠送优惠券id
  23. * @property int $give_coupon_from_type 1:平台优化券;2:门店优惠券
  24. * @property float $give_balance 赠送余额
  25. * @property string|null $give_currency 赠送货币(json字符串保存)
  26. * @property float $deduct_score 减扣积分
  27. * @property string|null $deduct_currency 减扣货币(json字符串保存)
  28. * @property float $pay_money 商品实际付款金额,单位:元
  29. * @property float $full_reduce_money 满减金额,单位:元
  30. * @property float $coupon_discount_money 优惠券优惠金额,单位:元
  31. * @property float $member_price_deduct 会员价优惠
  32. * @property string $user_coupon_id 用户优惠券id map
  33. * @property float $agent_deduct
  34. * @property string $agent_extra 经销商内购商品抵扣额外信息
  35. * @property int $status 状态[-1:删除;0:禁用;1启用]
  36. * @property int $created_at 创建时间
  37. * @property int $updated_at 修改时间
  38. */
  39. class StoreOrderMarketing extends BaseActiveRecord
  40. {
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function tableName()
  45. {
  46. return '{{%addons_store_order_marketing}}';
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function rules()
  52. {
  53. return [
  54. [['mall_id','store_id', 'order_detail_id','status', 'created_at', 'updated_at'], 'integer'],
  55. [['give_score', 'give_balance', 'deduct_score', 'pay_money', 'full_reduce_money', 'coupon_discount_money', 'member_price_deduct', 'agent_deduct'], 'number'],
  56. [['give_currency', 'deduct_currency'], 'string'],
  57. [['user_coupon_id','user_card_id'], 'string', 'max' => 1000],
  58. [['agent_extra'], 'string', 'max' => 255],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => '主键',
  68. 'mall_id' => 'mall_id',
  69. 'store_id' => 'store_id',
  70. 'order_detail_id' => '订单详情id',
  71. 'give_score' => '赠送积分',
  72. 'give_balance' => '赠送余额',
  73. 'give_currency' => '赠送货币(json字符串保存)',
  74. 'deduct_score' => '减扣积分',
  75. 'deduct_currency' => '减扣货币(json字符串保存)',
  76. 'pay_money' => '商品实际付款金额,单位:元',
  77. 'full_reduce_money' => '满减金额,单位:元',
  78. 'coupon_discount_money' => '优惠券优惠金额,单位:元',
  79. 'member_price_deduct' => '会员价优惠',
  80. 'user_coupon_id' => '用户优惠券id map',
  81. 'user_card_id' => '用户会员卡id map',
  82. 'agent_deduct' => 'Agent Deduct',
  83. 'agent_extra' => '经销商内购商品抵扣额外信息',
  84. 'status' => '状态[-1:删除;0:禁用;1启用]',
  85. 'created_at' => '创建时间',
  86. 'updated_at' => '修改时间',
  87. ];
  88. }
  89. public static function setData($deduct_data, $order_detail_id, $store_id, $mall_id)
  90. {
  91. $deduct_data['user_coupon_id'] = !empty($deduct_data['user_coupon_id'])? json_encode($deduct_data['user_coupon_id']):json_encode([]);
  92. $order_marketing = new StoreOrderMarketing();
  93. $order_marketing->loadDefaultValues();
  94. $order_marketing->load($deduct_data,'');
  95. $order_marketing->order_detail_id = $order_detail_id;
  96. $order_marketing->store_id = $store_id;
  97. $order_marketing->mall_id = $mall_id;
  98. $order_marketing->save();
  99. return $order_marketing;
  100. }
  101. public static function giveMarketing($mallId, $user_id, $source_table_id)
  102. {
  103. try {
  104. // 查询订单详情
  105. $detail_ids = StoreOrderDetail::find()->select('id')->where(['order_id' => $source_table_id])->asArray()->column();
  106. if (empty($detail_ids)) throw new \Exception('订单详情id查询为空');
  107. // 获取订单的营销数据
  108. $order_marketing_list = self::find()->andWhere(['in', 'order_detail_id', $detail_ids])->all();
  109. if (empty($order_marketing_list)) return true;
  110. $give_type = [UserWalletService::WALLET_TYPE_SCORE];
  111. $give_money = [];
  112. // 循环计算赠送数量
  113. foreach ($order_marketing_list as $marketing) {
  114. if (!isset($give_money[UserWalletService::WALLET_TYPE_SCORE])) $give_money[UserWalletService::WALLET_TYPE_SCORE] = 0;
  115. $give_money[UserWalletService::WALLET_TYPE_SCORE] += $marketing->give_score;
  116. }
  117. // 插入资金
  118. foreach ($give_type as $type) {
  119. if ($give_money[$type] <= 0) continue;
  120. $insert_wallet[] = [
  121. 'mall_id' => $mallId,
  122. 'user_id' => $user_id,
  123. 'is_frozen' => false,
  124. 'wallet_type' => $type,
  125. 'money' => $give_money[$type],
  126. 'desc' => 'o2o订单支付赠送',
  127. 'source_table' => StoreOrder::tableName(),
  128. 'source_table_id' => $source_table_id,
  129. 'from_type' => UserWalletEnum::CONSUME,
  130. 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE,
  131. ];
  132. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  133. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  134. }
  135. return true;
  136. } catch (\Exception $e) {
  137. self::setStaticError($e->getMessage());
  138. return false;
  139. }
  140. }
  141. public static function deductBack($store_order,$order_detail_id_all, $order_detail_ids, $mall_id, $user_id, $desc, $from_type = UserWalletEnum::CONSUME, $capital_type = UserWalletEnum::CONSUME_ORDER_CLOSE)
  142. {
  143. try {
  144. $order_id = $store_order['id'];
  145. $order_marketing_list = StoreOrderMarketing::lists([
  146. 'where' => [['order_detail_id' => $order_detail_ids], ['status' => StatusEnum::ENABLED]],
  147. 'select' => 'deduct_score,user_coupon_id,order_detail_id'
  148. ]);
  149. $refund_user_coupon_id_arr = [];
  150. $user_coupon_arr = $order_detail_arr = $user_coupon_order_detail_id_arr = [];
  151. $order_detail_list = StoreOrderDetail::lists(['where'=>[['order_id'=>$order_id]]]);
  152. foreach($order_detail_list as $order_detail) {
  153. $order_detail_arr[$order_detail['id']] = ['goods_id' => $order_detail['goods_id'], 'num' => $order_detail['num']];
  154. }
  155. $deduct_score = 0;
  156. if ($from_type == UserWalletEnum::REFUND) {
  157. $capital_type = 'consume_store_order_refund';
  158. //退款
  159. $order_detail_id_diff = array_diff($order_detail_id_all, $order_detail_ids);
  160. if (empty($order_detail_id_diff)) $order_detail_id_diff = $order_detail_id_all;
  161. $order_marketing_list_diff = StoreOrderMarketing::lists([
  162. 'where' => [['order_detail_id' => $order_detail_id_diff], ['status' => StatusEnum::ENABLED]],
  163. 'select' => 'user_coupon_id'
  164. ]);
  165. $user_coupon_id_arr_diff = [];
  166. foreach ($order_marketing_list_diff as $value) {
  167. $user_coupon_id = json_decode($value['user_coupon_id'], true);
  168. if (!empty($user_coupon_id)) $user_coupon_id_arr_diff = array_merge($user_coupon_id_arr_diff, array_keys($user_coupon_id));
  169. }
  170. foreach ($order_marketing_list as $item) {
  171. $deduct_score+=$item['deduct_score'];
  172. $user_coupon_id_arr = json_decode($item['user_coupon_id'], true);
  173. if (!empty($user_coupon_id_arr)) {
  174. foreach ($user_coupon_id_arr as $user_coupon_id => $money_) {
  175. if (in_array($user_coupon_id, $user_coupon_id_arr_diff)){
  176. $refund_user_coupon_id_arr[] = $user_coupon_id;
  177. if(isset($user_coupon_arr[$user_coupon_id])){
  178. $user_coupon_arr[$user_coupon_id] += $money_;
  179. }else{
  180. $user_coupon_arr[$user_coupon_id] = $money_;
  181. }
  182. }
  183. $user_coupon_order_detail_id_arr[$user_coupon_id][] = $item['order_detail_id'];
  184. }
  185. }
  186. }
  187. } else {
  188. //订单关闭
  189. $deduct_score = 0;
  190. foreach ($order_marketing_list as $item) {
  191. $deduct_score+=$item['deduct_score'];
  192. $user_coupon_id = json_decode($item['user_coupon_id'], true);
  193. if (!empty($user_coupon_id)) $refund_user_coupon_id_arr = array_merge($refund_user_coupon_id_arr, array_keys($user_coupon_id));
  194. foreach ($user_coupon_id as $key=>$value){
  195. if(isset($user_coupon_arr[$key])){
  196. $user_coupon_arr[$key] += $value;
  197. }else{
  198. $user_coupon_arr[$key] = $value;
  199. }
  200. $user_coupon_order_detail_id_arr[$key][] = $item['order_detail_id'];
  201. }
  202. }
  203. $capital_type = 'consume_store_order_close';
  204. }
  205. if($deduct_score>0){
  206. $insert_wallet[] = [
  207. 'mall_id' => $mall_id,
  208. 'user_id' => $user_id,
  209. 'is_frozen' => false,
  210. 'wallet_type' => 'score',
  211. 'money' => $deduct_score,
  212. 'desc' => $desc,
  213. 'source_table' => StoreOrder::tableName(),
  214. 'source_table_id' => $order_id,
  215. 'from_type' => UserWalletEnum::CONSUME,
  216. 'capital_type' =>$capital_type,
  217. ];
  218. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  219. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  220. }
  221. if (!empty($refund_user_coupon_id_arr)) {
  222. StoreCouponUserRelate::updateAll(['is_use' => 0], ['id' => $refund_user_coupon_id_arr]);
  223. StoreCouponUseLog::updateAll(['status' => StatusEnum::DELETE], ['order_id' => $order_id, 'user_coupon_id' => $refund_user_coupon_id_arr]);
  224. $coupon_user_relation_list = StoreCouponUserRelate::lists(['where'=>[['id'=>$refund_user_coupon_id_arr]]]);
  225. foreach ($coupon_user_relation_list as $coupon_user_relation){
  226. //优惠券-商品统计
  227. $set_goods_data = [
  228. 'mall_id'=>$mall_id,
  229. 'coupon_id'=>$coupon_user_relation['coupon_id'],
  230. 'payed_number'=>0,
  231. 'buyed_user_number'=>-1,
  232. ];
  233. if(!empty($user_coupon_order_detail_id_arr[$coupon_user_relation['id']])){
  234. $order_detail_ids = $user_coupon_order_detail_id_arr[$coupon_user_relation['id']];
  235. foreach ($order_detail_ids as $order_detail_id){
  236. if(!empty($order_detail_arr[$order_detail_id])){
  237. $set_goods_data['good_id'] = !empty($order_detail_arr[$order_detail_id]['goods_id'])?$order_detail_arr[$order_detail_id]['goods_id']:0;
  238. $set_goods_data['payed_number'] = !empty($order_detail_arr[$order_detail_id]['num'])?-$order_detail_arr[$order_detail_id]['num']:0;
  239. if($coupon_user_relation['from_type']==1){
  240. if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
  241. AddonsCouponGoodStatistics::setData($set_goods_data);
  242. }
  243. }else{
  244. StoreCouponGoodStatistics::setData($set_goods_data);
  245. }
  246. }
  247. }
  248. }
  249. //优惠券-统计
  250. $set_data = [
  251. 'mall_id'=>$mall_id,
  252. 'coupon_id'=>$coupon_user_relation['coupon_id'],
  253. 'used_number'=>-1,
  254. 'use_user_number'=>-1,
  255. 'use_order_number'=>-1,
  256. 'total_payed_amount'=>-($store_order['original_goods_price']+$store_order['shipping_money']),
  257. 'total_use_coupon_payed_amount'=>-($store_order['goods_price']+$store_order['shipping_money']),
  258. 'total_discount_amount'=>!empty($user_coupon_arr[$coupon_user_relation['id']])?-$user_coupon_arr[$coupon_user_relation['id']]:0,
  259. ];
  260. if($coupon_user_relation['from_type']==1){
  261. if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
  262. AddonsCouponStatistics::setData($set_data);
  263. }
  264. }else{
  265. $set_data['store_id'] = $coupon_user_relation['store_id'];
  266. StoreCouponStatistics::setData($set_data);
  267. }
  268. }
  269. }
  270. return true;
  271. } catch (\Exception $e) {
  272. self::setStaticError($e->getMessage());
  273. return false;
  274. }
  275. }
  276. }