AchievementService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace common\services\mall;
  3. use common\enums\EventNameEnum;
  4. use common\enums\PayTypeEnum;
  5. use common\enums\RefundStatusEnum;
  6. use common\enums\RefundTypeEnum;
  7. use common\models\order\Order;
  8. use common\models\order\OrderAchievementGoods;
  9. use common\models\order\OrderRefund;
  10. use common\models\statistics\UserAchievementStatistics;
  11. use common\models\statistics\UserAchievementStatisticsTotal;
  12. use common\models\statistics\UserTeamAchievementStatistics;
  13. use common\models\statistics\UserTeamDateAchievementStatistics;
  14. use Yii;
  15. /**
  16. * 业绩service
  17. *
  18. * @property-read null|array|mixed $achievementConfig
  19. */
  20. class AchievementService extends BaseService
  21. {
  22. /**
  23. * 获取业绩相关配置
  24. *
  25. * @return array|mixed|null
  26. */
  27. public function getAchievementConfig()
  28. {
  29. return Yii::$app->services->setting->mall->get($this->mall_id, 'order');
  30. }
  31. /**
  32. * 返回业绩商品是不是全部都退了
  33. *
  34. * @param int $orderId
  35. * @param bool $isRefund
  36. *
  37. * @return bool
  38. */
  39. public function achievementGoodsIsAllRefund(int $orderId, bool $isRefund = false): bool
  40. {
  41. $order = Order::getOne(
  42. [
  43. ['id' => $orderId]
  44. ],
  45. true,
  46. [
  47. 'detail' => function ($query) {
  48. $query->select('id, order_id, goods_id');
  49. }
  50. ],
  51. false,
  52. 'id'
  53. );
  54. $detailsIds = array_column($this->getAchievementOrderDetail($order['detail'], $isRefund), 'id');
  55. // 找订单退款表
  56. $exchangeOrderIds = OrderRefund::find()
  57. ->where(['order_detail_id' => $detailsIds, 'refund_status' => RefundStatusEnum::FINISH, 'type' => [RefundTypeEnum::MONEY, RefundTypeEnum::MONEY_AND_PRODUCT]])
  58. ->count();
  59. if (!$exchangeOrderIds) {
  60. return false;
  61. }
  62. return count($detailsIds) == $exchangeOrderIds;
  63. }
  64. /**
  65. * 根据订单详情返回满足业绩配置的订单详情
  66. *
  67. * @param array $orderDetails
  68. * @param bool $isRefund
  69. *
  70. * @return array
  71. */
  72. public function getAchievementOrderDetail(array $orderDetails, bool $isRefund = false): array
  73. {
  74. // 如果是退款直接查询当时有没有加过业绩,不管当前的配置是怎么样
  75. if ($isRefund) {
  76. $orderId = $orderDetails[0]['order_id'] ?? 0;
  77. $achievementGoodsIds = json_decode(
  78. OrderAchievementGoods::find()
  79. ->where(['order_id' => $orderId])
  80. ->select('goods_ids')
  81. ->scalar(),
  82. true
  83. );
  84. } else {
  85. $config = $this->getAchievementConfig();
  86. if (empty($config['is_designated_achievement_goods'])) {
  87. return $orderDetails;
  88. }
  89. $achievementGoodsIds = json_decode($config['achievement_goods_ids'], true);
  90. }
  91. if (empty($achievementGoodsIds)) {
  92. return [];
  93. }
  94. $resOrderDetails = [];
  95. foreach ($orderDetails as $detail) {
  96. if (in_array($detail['goods_id'], $achievementGoodsIds)) {
  97. $resOrderDetails[] = $detail;
  98. }
  99. }
  100. return $resOrderDetails;
  101. }
  102. /**
  103. * 根据事件类型返回用户和团队统计所需保存的数据
  104. *
  105. * @param string $type
  106. * @param array $order 订单创建时传的是多个订单,二维数组
  107. *
  108. * @return array
  109. */
  110. public function getStatisticsDataByType(string $type, array $order): array
  111. {
  112. $resData = [
  113. 'user' => [],
  114. 'team' => [
  115. 'direct' => [],
  116. 'indirect' => [],
  117. 'team' => [],
  118. ]
  119. ];
  120. $isRefund = $type === EventNameEnum::ORDER_REFUND;
  121. $resOrderDetails = $this->getAchievementOrderDetail($order['detail'], $isRefund);
  122. if (empty($resOrderDetails)) {
  123. return $resData;
  124. }
  125. $orderAchievement = array_sum(array_column($resOrderDetails, 'goods_price'));
  126. $orderOriginalGoodsPrice = array_sum(array_column($resOrderDetails, 'original_goods_price'));
  127. switch ($type) {
  128. // 维护创建订单支付和订单退款时所需维护的字段
  129. case EventNameEnum::ORDER_PAY:
  130. case EventNameEnum::ORDER_REFUND:
  131. // 已支付时,维护订单的业绩商品id
  132. if (EventNameEnum::ORDER_PAY === $type) {
  133. OrderAchievementGoods::setData([
  134. 'mall_id' => $this->mall_id,
  135. 'user_id' => $order['user_id'],
  136. 'order_id' => $order['id'],
  137. 'order_details_ids' => array_column($resOrderDetails, 'id'),
  138. 'goods_ids' => array_column($resOrderDetails, 'goods_id')
  139. ]);
  140. }
  141. $resData['user'] = [
  142. 'user_goods_price_total' => $orderAchievement,
  143. 'pay_goods_num' => array_sum(array_column($resOrderDetails, 'num')),
  144. 'pay_selling_price' => $orderOriginalGoodsPrice,
  145. 'balance_pay' => $order['payment_type'] === PayTypeEnum::BALANCE ? $orderAchievement : 0,
  146. 'wechat_pay' => $order['payment_type'] === PayTypeEnum::WECHAT ? $orderAchievement : 0,
  147. 'ali_pay' => $order['payment_type'] === PayTypeEnum::ALI ? $orderAchievement : 0,
  148. ];
  149. $resData['team']['direct'] = [
  150. 'user_direct_order_price_total' => $orderAchievement,
  151. 'user_direct_pay_selling_price' => $orderOriginalGoodsPrice,
  152. ];
  153. $resData['team']['indirect'] = [
  154. 'user_indirect_order_price_total' => $orderAchievement,
  155. 'user_indirect_pay_selling_price' => $orderOriginalGoodsPrice,
  156. ];
  157. $resData['team']['team'] = [
  158. 'team_pay_selling_price' => $orderOriginalGoodsPrice,
  159. 'team_goods_price_total' => $orderAchievement,
  160. ];
  161. // 已支付和或者退款并且全退了的订单才减少数量
  162. if (
  163. $type === EventNameEnum::ORDER_PAY ||
  164. $this->achievementGoodsIsAllRefund($order['id'], $isRefund)
  165. ) {
  166. $resData['user']['user_order_num'] = 1;
  167. $resData['team']['direct']['user_direct_order_total'] = 1;
  168. $resData['team']['indirect']['user_indirect_order_total'] = 1;
  169. $resData['team']['team']['team_order_num'] = 1;
  170. }
  171. // 退款就负数
  172. if ($type === EventNameEnum::ORDER_REFUND) {
  173. $this->convertValuesToNegative($resData);
  174. }
  175. break;
  176. // 维护创建订单完成所需维护的字段
  177. case EventNameEnum::ORDER_FINISH:
  178. $resData['user'] = [
  179. 'finish_order_num' => 1,
  180. 'finish_order_money' => $orderAchievement,
  181. 'finish_goods_num' => array_sum(array_column($resOrderDetails, 'num')),
  182. 'finish_selling_price' => $orderOriginalGoodsPrice,
  183. ];
  184. $resData['team']['direct'] = [
  185. 'user_direct_finish_selling_price' => $orderOriginalGoodsPrice
  186. ];
  187. $resData['team']['indirect'] = [
  188. 'user_indirect_finish_selling_price' => $orderOriginalGoodsPrice,
  189. ];
  190. $resData['team']['team'] = [
  191. 'team_finish_order_total' => 1,
  192. 'team_finish_order_price_total' => $orderAchievement,
  193. 'team_finish_selling_price' => $orderOriginalGoodsPrice,
  194. ];
  195. break;
  196. }
  197. return $resData;
  198. }
  199. /**
  200. * 数字转为负数
  201. *
  202. * @param $array
  203. *
  204. * @return void
  205. */
  206. public function convertValuesToNegative(&$array) {
  207. foreach ($array as &$value) {
  208. if (is_array($value)) {
  209. $this->convertValuesToNegative($value);
  210. } elseif (is_numeric($value)) {
  211. $value = -abs($value);
  212. }
  213. }
  214. }
  215. /**
  216. * 保存业绩统计数据
  217. *
  218. * @param string $eventName
  219. * @param array $baseData 包含:日期 用户id
  220. * @param array $order
  221. * @param bool $isIncr
  222. *
  223. * @return void
  224. */
  225. public function saveAchievementStatisticsData(
  226. string $eventName,
  227. array $baseData,
  228. array $order,
  229. bool $isIncr = true
  230. )
  231. {
  232. $statisticsData = $this->getStatisticsDataByType($eventName, $order);
  233. $baseData['mall_id'] = $this->mall_id;
  234. if (!empty($statisticsData['user'])) {
  235. UserAchievementStatistics::saveUserStatisticsData($baseData, $statisticsData['user'], $isIncr);
  236. UserAchievementStatisticsTotal::saveUserStatisticsData($baseData, $statisticsData['user'], $isIncr);
  237. }
  238. if (!empty($statisticsData['team']['team']) || !empty($statisticsData['team']['direct']) || !empty($statisticsData['team']['indirect'])) {
  239. UserTeamAchievementStatistics::saveStatisticsTotal(
  240. $baseData,
  241. $statisticsData['team']['team'],
  242. $isIncr,
  243. $statisticsData['team']['direct'],
  244. $statisticsData['team']['indirect']
  245. );
  246. UserTeamDateAchievementStatistics::saveStatistics(
  247. $baseData,
  248. $statisticsData['team']['team'],
  249. $isIncr,
  250. $statisticsData['team']['direct'],
  251. $statisticsData['team']['indirect']
  252. );
  253. }
  254. }
  255. }