OrderUnreadMsg.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace common\models\order;
  3. use common\enums\GoodsTypeEnum;
  4. use common\enums\OrderSourceEnum;
  5. use common\enums\OrderStatusEnum;
  6. use common\enums\RefundStatusEnum;
  7. use common\enums\StatusEnum;
  8. use common\models\goods\Goods;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%order_user_statistics}}".
  12. *
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商场ID
  16. * @property int|null $order_pay_num 待付款数量
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 修改时间
  20. * @property int|null $order_received_num 待收货数量
  21. * @property int|null $order_goods_num 待发货数量
  22. * @property int|null $order_evaluate_num 待评价数量
  23. * @property int|null $order_return_num 退货数量
  24. * @property int|null $user_id 会员ID
  25. * @property int|null $is_statistics 是否已经执行过统计,因为这功能是后面上的,需要执行一次统计数据才会正确
  26. */
  27. class OrderUnreadMsg extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%order_unread_msg}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'order_pay_num', 'status', 'created_at', 'updated_at', 'order_received_num', 'order_goods_num', 'order_evaluate_num', 'order_return_num', 'user_id', 'is_statistics'], 'integer'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商场ID',
  53. 'order_pay_num' => '待付款数量',
  54. 'status' => '状态[-1:删除;0:禁用;1启用]',
  55. 'created_at' => '创建时间',
  56. 'updated_at' => '修改时间',
  57. 'order_received_num' => '待收货数量',
  58. 'order_goods_num' => '待发货数量',
  59. 'order_evaluate_num' => '待评价数量',
  60. 'order_return_num' => '退货数量',
  61. 'user_id' => '会员ID',
  62. 'is_statistics' => '是否已经执行过统计,因为这功能是后面上的,需要执行一次统计数据才会正确',
  63. ];
  64. }
  65. //完成订单调用方法
  66. public static function orderFinish($params){
  67. $orderStatis['user_id'] = $params['user_id'];
  68. $orderStatis['mall_id'] = $params['mall_id'];
  69. $is_virtual = 0;;
  70. if($params['goods_ids']){
  71. $param['where'][] = ['in', 'id', $params['goods_ids']];
  72. $param['select'] = ['goods_type'];
  73. $goodsList = Goods::lists($param); //var_dump($goodsList);die;
  74. if($goodsList){
  75. foreach($goodsList as $k=>$v){
  76. if($v['goods_type']==GoodsTypeEnum::GoodsTypeVirtual){
  77. $is_virtual = 1;
  78. }
  79. }
  80. }
  81. }
  82. if($is_virtual==0){
  83. $orderStatis['order_evaluate_num'] = -1;
  84. self::orderStastics($orderStatis);
  85. }
  86. }
  87. //收货调用方法
  88. public static function takeDelivery($params){
  89. $order = Order::getOne([['id' => $params['id']]],true);
  90. $orderStatis['user_id'] = $order['user_id'];
  91. $orderStatis['mall_id'] = $order['mall_id'];
  92. $orderStatis['order_received_num'] = -1;
  93. $orderStatis['order_evaluate_num'] = 1;
  94. self::orderStastics($orderStatis);
  95. }
  96. //发货调用方法
  97. public static function delivery($params){
  98. $order_express = OrderExpress::find()->select('order_id')->where(['id' => $params['id']])->asArray()->one();
  99. $order = Order::getOne([['id' => $order_express['order_id']]],true);
  100. if ($order['order_status'] == OrderStatusEnum::SHIPMENTS) {// 全部发货的情况下才可以增加发货数量
  101. $orderStatis['user_id'] = $order['user_id'];
  102. $orderStatis['mall_id'] = $order['mall_id'];
  103. $orderStatis['order_received_num'] = 1;
  104. $orderStatis['order_goods_num'] = -1;
  105. self::orderStastics($orderStatis);
  106. }
  107. }
  108. public static function orderStastics($params){
  109. self::updateData($params['mall_id'],$params['user_id']);
  110. return true;
  111. try {
  112. $model = self::findOne(['user_id' => $params['user_id'],'mall_id'=>$params['mall_id']]);
  113. if (empty($model)) {
  114. $model = new self();
  115. $model->loadDefaultValues();
  116. }
  117. if($params['order_pay_num']) $model->order_pay_num = $model->order_pay_num + $params['order_pay_num'];
  118. if($params['order_received_num']) $model->order_received_num = $model->order_received_num + $params['order_received_num'];
  119. if($params['order_goods_num']) $model->order_goods_num = $model->order_goods_num + $params['order_goods_num'];
  120. if($params['order_evaluate_num']) $model->order_evaluate_num = $model->order_evaluate_num + $params['order_evaluate_num'];
  121. if($params['order_return_num']) $model->order_return_num = $model->order_return_num + $params['order_return_num'];
  122. unset($params['order_pay_num'],$params['order_received_num'],$params['order_goods_num'],$params['order_evaluate_num'],$params['order_return_num']);
  123. //var_dump($params,$model);die;
  124. if (empty($model)) throw new \Exception('查无此订单');
  125. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  126. return $model;
  127. } catch (\Exception $e) {
  128. self::setStaticError($e->getMessage());
  129. return false;
  130. }
  131. }
  132. /**
  133. * 保存数据
  134. * @Author: lun
  135. * @DateTime: 2022/3/3 0003 17:24
  136. * @Copyright: copyright (c) 2021 广东七件事集团
  137. * @param $params
  138. * @return bool|PlatformOrder
  139. */
  140. public static function setData($params)
  141. {
  142. try {
  143. if (!empty($params['id'])) {
  144. $model = self::findOne(['id' => $params['id']]);
  145. } else {
  146. $model = new self();
  147. $model->loadDefaultValues();
  148. }
  149. if (empty($model)) throw new \Exception('查无此订单');
  150. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  151. return $model;
  152. } catch (\Exception $e) {
  153. self::setStaticError($e->getMessage());
  154. return false;
  155. }
  156. }
  157. //订单统计初始化
  158. public static function orderStatistic($params,$return_data = false){
  159. //查询订单数量
  160. $orderStatis['user_id'] = $params['user_id'];
  161. $orderStatis['mall_id'] = $params['mall_id'];
  162. $orderStatis['is_statistics'] = 1;
  163. //统计代付款
  164. $where = [['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']],['=','is_feedback',StatusEnum::DISABLED]];
  165. $paywhere = $where;
  166. $paywhere[] = ['=','order_status',OrderStatusEnum::NOT_PAY];
  167. $paywhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
  168. $orderStatis['order_pay_num'] = Order::count(['where'=>$paywhere]);
  169. //统计待发货
  170. $goodswhere = $where;
  171. $goodswhere[] = ['=','order_status',OrderStatusEnum::PAY];
  172. $goodswhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
  173. $orderStatis['order_goods_num'] = Order::count(['where'=>$goodswhere]);
  174. //统计待收货
  175. $receivedwhere = $where;
  176. $receivedwhere[] = ['=','order_status',OrderStatusEnum::SHIPMENTS];
  177. $receivedwhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
  178. $orderStatis['order_received_num'] = Order::count(['where'=>$receivedwhere]);
  179. //待评价
  180. $evaluatewhere = $where;
  181. $evaluatewhere[] = ['=','order_status',OrderStatusEnum::SING];
  182. $evaluatewhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
  183. $orderStatis['order_evaluate_num'] = Order::count(['where'=>$evaluatewhere]);
  184. //待退换货
  185. $order_refund_1 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::APPLY],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
  186. //$order_refund_2 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::FINISH],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
  187. //$order_refund_3 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::REVOKE],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
  188. $orderStatis['order_return_num'] = $order_refund_1;
  189. if($return_data) return $orderStatis;
  190. $model = self::orderStastics($orderStatis);
  191. return $model;
  192. }
  193. /**
  194. * 修改统计数据
  195. * @param $mall_id
  196. * @param $user_id
  197. * @return OrderUnreadMsg|null
  198. * @throws
  199. */
  200. public static function updateData($mall_id,$user_id)
  201. {
  202. $params = self::orderStatistic(['user_id' => $user_id,'mall_id' => $mall_id],true);
  203. //print_r($params);
  204. $model = self::findOne(['user_id' => $user_id,'mall_id'=>$mall_id]);
  205. if($model){
  206. }else{
  207. $model = new self();
  208. $model->loadDefaultValues();
  209. }
  210. if (!$model->load($params, '') || !$model->save()){
  211. Yii::error($model->getErrorMessage());
  212. throw new \Exception($model->getErrorMessage());
  213. }
  214. return $model;
  215. }
  216. }