LeagueForm.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Cash;
  4. use app\models\District;
  5. use app\models\Option;
  6. use app\models\Order;
  7. use app\models\RechargeReOrder;
  8. use app\models\SaasCategory;
  9. use app\models\SaaSLeaguePriceLog;
  10. use app\models\SaasUser;
  11. use app\models\Store;
  12. use app\models\User;
  13. use yii\base\Model;
  14. class LeagueForm extends Model
  15. {
  16. public $id;
  17. public $store_id;
  18. public $user_name;
  19. public $mobile;
  20. public $store_name;
  21. public $store_category_id;
  22. public $start_time;
  23. public $end_time;
  24. public function rules()
  25. {
  26. return [
  27. [['store_id', 'id', 'store_category_id'], 'integer'],
  28. [['mobile', 'start_time', 'end_time', 'user_name', 'store_name'], 'string'],
  29. [['mobile', 'user_name', 'store_name'], 'trim'],
  30. ];
  31. }
  32. //用户联盟券记录
  33. public function userLeagueInfo()
  34. {
  35. $id = $this->id;
  36. $saasUser = SaasUser::findOne($id);
  37. $user = null;
  38. $store = Store::findOne($saasUser->store_id);
  39. if ($store) {
  40. $user = User::findOne(['store_id' => $store->id, 'binding' => $saasUser->mobile, 'is_delete' => 1]);
  41. }
  42. $parentSaasUser = SaasUser::findOne($saasUser->parent_id);
  43. $data = [
  44. 'mobile' => $saasUser->mobile,
  45. 'name' => $saasUser->name,
  46. 'avatar' => $saasUser->avatar,
  47. 'league_price' => $saasUser->league_price,
  48. 'store_name' => $store->name ?: '',
  49. 'store_user_name' => $user ? $user->nickname : '',
  50. 'parent_name' => $parentSaasUser->name ?: '总店',
  51. 'parent_id' => $parentSaasUser->id ?: 0,
  52. 'created_at' => date('Y-m-d H:i:s', $saasUser->created_at)
  53. ];
  54. return [
  55. 'code' => 0,
  56. 'msg' => 'success',
  57. 'data' => $data
  58. ];
  59. }
  60. //用户联盟券
  61. public function userLeague() {
  62. $user_name = $this->user_name;
  63. $mobile = $this->mobile;
  64. $query = SaasUser::find()->where(['is_delete' => 0]);
  65. if (!empty($user_name)) {
  66. $query->andWhere(['LIKE', 'name', $user_name]);
  67. }
  68. if (!empty($mobile)) {
  69. $query->andWhere(['LIKE', 'mobile', $mobile]);
  70. }
  71. $query->select('id, name, mobile, avatar, league_price')->orderBy('league_price desc, id desc');
  72. $pagination = pagination_make($query);
  73. $pagination['total_user_league'] = SaasUser::find()->where(['is_delete' => 0])->sum('league_price') ?: 0;
  74. return [
  75. 'code' => 0,
  76. 'msg' => '',
  77. 'data' => $pagination
  78. ];
  79. }
  80. //商家联盟券
  81. public function storeLeague() {
  82. $store_name = $this->store_name;
  83. $store_category_id = $this->store_category_id;
  84. $query = Store::find()->where(['is_delete' => 0]);
  85. if (!empty($store_name)) {
  86. $query->andWhere(['LIKE', 'name', $store_name]);
  87. }
  88. if (!empty($store_category_id)) {
  89. $query->andWhere(['category_id' => $store_category_id]);
  90. }
  91. $query->select('id, name, category_id, logo, league_price')->orderBy('league_price desc, id desc');
  92. $pagination = pagination_make($query);
  93. foreach ($pagination['list'] as &$item) {
  94. $item['category_name'] = SaasCategory::findOne($item['category_id'])->name ?: '';
  95. if (empty($item['logo'])) {
  96. $item['logo'] = Option::get('logo', $item['id'], 'store')['value'];
  97. if (empty($item['logo'])) {
  98. $item['logo'] = Option::get('web_log', $item['id'], 'web')['value'];
  99. }
  100. }
  101. }
  102. $pagination['total_user_league'] = SaasUser::find()->where(['is_delete' => 0])->sum('league_price') ?: 0;
  103. return [
  104. 'code' => 0,
  105. 'msg' => '',
  106. 'data' => $pagination
  107. ];
  108. }
  109. //商家联盟券记录
  110. public function storeLeagueLog() {
  111. $id = $this->id;
  112. $store_name = $this->store_name;
  113. $store_category_id = $this->store_category_id;
  114. $start_time = $this->start_time;
  115. $end_time = $this->end_time;
  116. //
  117. $query = SaaSLeaguePriceLog::find()->alias('lpl')->where(['lpl.role' => SaaSLeaguePriceLog::ROLE_STORE, 'lpl.is_delete' => 0]);
  118. $query->leftJoin(['s' => Store::tableName()], 's.id = lpl.store_id');
  119. if ($id) {
  120. $query->andWhere(['lpl.store_id' => $id]);
  121. }
  122. if ($store_name) {
  123. $query->andWhere(['LIKE', 's.name', $store_name]);
  124. }
  125. if ($store_category_id) {
  126. $query->andWhere(['s.category_id' => $store_category_id]);
  127. }
  128. if ($start_time) {
  129. $start_time = strtotime($start_time);
  130. $query->andWhere(['>=', 'lpl.addtime', $start_time]);
  131. }
  132. if ($end_time) {
  133. $end_time = strtotime($end_time);
  134. $query->andWhere(['<=', 'lpl.addtime', $end_time]);
  135. }
  136. $query->andWhere(['>', 'lpl.league_price', 0]);
  137. $query->select('lpl.id, lpl.type, lpl.order_id, lpl.send_or_take_type, lpl.saas_user_id,
  138. lpl.after, lpl.before, lpl.league_price, lpl.addtime, s.name, s.category_id, s.logo, s.id store_id')
  139. ->orderBy('lpl.id desc');
  140. $pagination = pagination_make($query);
  141. foreach ($pagination['list'] as &$item) {
  142. $item['addtime'] = date("Y-m-d H:i:s", $item['addtime']);
  143. $item['send_or_take_type'] = (int)$item['send_or_take_type'];
  144. $item['category_name'] = SaasCategory::findOne($item['category_id'])->name ?: '';
  145. $item['type'] = (int)$item['type'];
  146. $item['type_name'] = SaaSLeaguePriceLog::getTypeStr($item['type']);
  147. if (empty($item['logo'])) {
  148. $item['logo'] = Option::get('logo', $item['store_id'], 'store')['value'];
  149. if (empty($item['logo'])) {
  150. $item['logo'] = Option::get('web_log', $item['store_id'], 'web')['value'];
  151. }
  152. }
  153. $item['order_no'] = '';
  154. if ($item['order_id']) {
  155. if (in_array($item['type'], [
  156. SaaSLeaguePriceLog::TYPE_ORDER_REBATE,//下单返利
  157. SaaSLeaguePriceLog::TYPE_CANCEL,//订单取消退回
  158. SaaSLeaguePriceLog::TYPE_DEDUCTION,//下单抵扣
  159. ])) {
  160. $order = Order::findOne($item['order_id']);
  161. }
  162. if ($item['type'] === SaaSLeaguePriceLog::TYPE_LEAGUE_RECHARGE) {//联盟券
  163. $order = RechargeReOrder::findOne($item['order_id']);
  164. }
  165. if (in_array($item['type'], [
  166. SaaSLeaguePriceLog::TYPE_WITHDRAW,
  167. SaaSLeaguePriceLog::TYPE_WITHDRAW_REJECT,
  168. ])) {//联盟券提现
  169. $order = Cash::findOne($item['order_id']);
  170. }
  171. if ($item['type'] === SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER) {//当面付
  172. $order = \app\plugins\scanCodePay\models\Order::findOne($item['order_id']);
  173. }
  174. if (isset($order)) {
  175. $item['order_no'] = $order->order_no ?: '';
  176. }
  177. }
  178. $item['user_name'] = SaasUser::findOne($item['saas_user_id'])->name ?: '';
  179. }
  180. $league_price_query = Store::find()->where(['is_delete' => 0]);
  181. if ($id) {
  182. $league_price_query->andWhere(['id' => $id]);
  183. }
  184. $league_price = $league_price_query->select('league_price')->sum('league_price') ?: 0;
  185. return [
  186. 'code' => 0,
  187. 'msg' => 'success',
  188. 'data' => [
  189. 'data' => $pagination['list'],
  190. 'pageNo' => $pagination['pageNo'],
  191. 'totalCount' => $pagination['totalCount'],
  192. 'league_price' => $league_price
  193. ],
  194. ];
  195. }
  196. public function storeLeagueInfo() {
  197. $id = $this->id;
  198. $store = Store::findOne($id);
  199. $store_district = District::find()->where(['id' => [$store->district_id, $store->city_id, $store->province_id]])->select('name')->column();
  200. $data = [
  201. 'store_name' => $store->name,
  202. 'category_name' => SaasCategory::findOne($store->category_id)->name ?: '',
  203. 'address' => implode('', $store_district) . $store->address,
  204. 'logo' => $store->logo ?: (Option::get('logo', $store->id, 'store')['value'] ?: Option::get('web_log', $store->id, 'web')['value']),
  205. 'league_price' => $store->league_price
  206. ];
  207. return [
  208. 'code' => 0,
  209. 'msg' => 'success',
  210. 'data' => $data
  211. ];
  212. }
  213. }