ManyOrder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\models\many;
  7. use app\admin\model\system\SystemConfig;
  8. use app\models\user\User;
  9. use app\models\user\UserBill;
  10. use crmeb\services\PHPExcelService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. /**
  14. * Class StoreCategory
  15. * @package app\admin\model\store
  16. */
  17. class ManyOrder extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'many_order';
  29. use ModelTrait;
  30. protected $autoWriteTimestamp = true;
  31. public static function list($where)
  32. {
  33. $model = self::alias('a')
  34. ->order('a.id DESC')
  35. ->field('a.*,b.name,u.nickname')
  36. ->leftJoin('many b', 'b.id = a.many_id')
  37. ->leftJoin('user u', 'u.uid = a.uid')
  38. ->where('a.uid', $where['uid']);
  39. if ($where['status'] == 0 or $where['status'])$model->where('a.status' , '=', $where['status']);
  40. $data['count'] = $model->count();
  41. if ($where['page'] && $where['limit']){
  42. $model->page($where['page'], $where['limit']);
  43. }else{
  44. $model->page(20, 1);
  45. }
  46. $list = $model->select()->toArray();
  47. $data['data'] = $list;
  48. return $data;
  49. }
  50. /**
  51. * 众筹成功订单返还
  52. * @return void
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public static function suc_return()
  58. {
  59. $order = self::where('is_return', 1)->where('status', 0)->select(); // 查询需要返还的订单
  60. if ($order){
  61. foreach ($order as $item)
  62. {
  63. if (strtotime($item['create_time'])+(86400*7) < time()){
  64. $user = User::where('uid', $item['uid'])->find();//用户
  65. $user1 = [];
  66. $user2 = [];
  67. if ($user['spread_uid'] > 0){
  68. $user1 = User::where('uid', $user['spread_uid'])->find();//用户
  69. if ($user1['spread_uid'] > 0) $user2 = User::where('uid', $user1['spread_uid'])->find();//用户
  70. }
  71. $purple_integral = round($item['price'] * 1.07, 2);// 奖励紫积分积分
  72. $business_integral = round($item['price'] * 0.03, 2);// 奖励商家积分
  73. $user['purple_integral'] += $purple_integral;
  74. $user['business_integral'] += $business_integral;
  75. if ($user1){
  76. // 直推收益的百分之十
  77. $sy1 = $item['price'] * 0.01;
  78. $user1['purple_integral'] += round($sy1 * 0.7, 2);
  79. $user1['business_integral'] += round($sy1 * 0.3, 2);
  80. }
  81. if ($user2){
  82. // 间推收益的百分之五
  83. $sy2 = $item['price'] * 0.005;
  84. $user2['purple_integral'] += round($sy2 * 0.7, 2);
  85. $user2['business_integral'] += round($sy2 * 0.3, 2);
  86. }
  87. self::where('id', $item['id'])->update(['status' => 1, 'return_time' => time()]);
  88. $user->save();
  89. UserBill::income('众筹成功奖励紫积分', $user['uid'], 'purple_integral', 'zccg', $purple_integral, $user['spread_uid'], $user['purple_integral'], '众筹成功返还紫积分');
  90. UserBill::income('众筹成功奖励商家积分', $user['uid'], 'business_integral', 'zccg', $business_integral, $user['spread_uid'], $user['business_integral'], '众筹成功返还商家积分');
  91. if ($user1){
  92. UserBill::income('直推奖励紫积分', $user1['uid'], 'purple_integral', 'zccg', $sy1*0.7, $user1['spread_uid'], $user1['purple_integral'], '直推奖励紫积分');
  93. UserBill::income('直推奖励商家积分', $user1['uid'], 'business_integral', 'zccg', $sy1*0.3, $user1['spread_uid'], $user1['business_integral'], '直推奖励商家积分');
  94. $user1->save();
  95. }
  96. if ($user2){
  97. UserBill::income('间推奖励紫积分', $user1['uid'], 'purple_integral', 'zccg', $sy2*0.7, $user2['spread_uid'], $user2['purple_integral'], '间推奖励紫积分');
  98. UserBill::income('间推奖励商家积分', $user1['uid'], 'business_integral', 'zccg', $sy2*0.3, $user2['spread_uid'], $user2['business_integral'], '间推奖励商家积分');
  99. $user2->save();
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * 团队奖励
  107. * @return void
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public static function push()
  113. {
  114. if (!Push::where('add_time', strtotime('today'))->find()){
  115. $user = User::select();
  116. foreach ($user as $item)
  117. {
  118. $price = ManyOrder::whereBetweenTime('return_time', strtotime('yesterday'), strtotime('today'))->where('uid', $item['uid'])->sum('price');// 昨天众筹成功返还的金额流水
  119. if ($item['spread_uid'] > 0 and $price > 0){
  120. $spread = getParent($item['uid']);// 找到所有上级
  121. $v1 = 0;
  122. $v2 = 0;
  123. $v3 = 0;
  124. $one = SystemConfig::getConfigValue('v1')/100; // v1比例
  125. $tow = SystemConfig::getConfigValue('v2')/100;// v2比例
  126. $three = SystemConfig::getConfigValue('v3')/100;// v3比例
  127. foreach ($spread as $value) {
  128. $details = User::where('uid', $value)->find();
  129. if ($details['level'] == 1){
  130. if ($v2 == 0 and $v3 == 0){
  131. // 没有发放v2和v3的奖励
  132. if ($v1 == 0) { // 没有发放v1的奖励
  133. $jl = $price * $one;
  134. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  135. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  136. $v1++;
  137. }elseif ($v1 == 1){// 发放v1奖励1次
  138. $jl = ($price*$one)*0.05;//平级的百分之五
  139. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  140. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  141. $v1++;
  142. }
  143. }
  144. }elseif ($details['level'] == 2){
  145. if ($v3 == 0){
  146. // 没有发放v3的奖励
  147. if ($v1 == 0 and $v2 == 0) { // 没有发放v1和v2的奖励的奖励
  148. $jl = $price * $tow; // 拿到流水的百分之八
  149. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  150. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  151. $v2++;
  152. }elseif($v1 == 0 and $v2 == 1) { // 没有发放v1和v2的奖励的奖励
  153. $jl = ($price * $tow)*0.05; // 拿到流水的百分之八
  154. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  155. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  156. $v2++;
  157. }elseif ($v1 > 0 and $v2 == 0){// 发放v1奖励,没有发放v2的奖励
  158. $jl = $price * ($tow - $one); // 拿到流水减掉v1的百分之五
  159. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  160. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  161. $v2++;
  162. }elseif ($v1 > 0 and $v2 == 1){// 发放v1奖励,发放v2的奖励一次
  163. $jl = ($price * ($tow - $one))*0.05; // 拿到平级的百分之五
  164. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  165. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  166. $v2++;
  167. }
  168. }
  169. }elseif ($details['level'] == 3){
  170. if ($v1 == 0 and $v2 == 0 and $v3 == 0) { // 没有发放v1和v2v3的奖励的奖励
  171. $jl = $price * $three; // 拿到流水的百分之11
  172. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  173. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  174. $v3++;
  175. }elseif ($v1 == 0 and $v2 == 0 and $v3 == 1) {// 没有发放v1和v2的奖励的奖励
  176. $jl = ($price * $three) * 0.05; // 拿到平级的百分之五
  177. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  178. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  179. $v3++;
  180. }elseif ($v1 > 0 and $v2 == 0 and $v3 == 0){// 发放v1奖励,没有发放v2v3的奖励
  181. $jl = $price * ($three - $one); // 拿到流水减掉v1的百分之五
  182. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  183. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  184. $v3++;
  185. }elseif ($v1 > 0 and $v2 == 0 and $v3 == 1){// 发放v1奖励,没有发放v2v3的奖励
  186. $jl = ($price * ($three - $one)) * 0.05; // 拿到平级的百分之五
  187. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  188. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  189. $v3++;
  190. }elseif ($v1 == 0 and $v2 > 0 and $v3 == 0){// 发放v1奖励,发放v2的奖励,没有发放v3的奖励
  191. $jl = ($price * ($three - $tow)); // 拿到平级的百分之五
  192. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  193. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  194. $v3++;
  195. }elseif ($v1 == 0 and $v2 > 0 and $v3 == 1){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  196. $jl = ($price * ($three - $tow)) * 0.05; // 拿到平级的百分之五
  197. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  198. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  199. $v3++;
  200. }elseif ($v1 > 0 and $v2 > 0 and $v3 == 0){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  201. $jl = ($price * ($three - $tow)); // 拿到平级的百分之五
  202. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  203. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  204. $v3++;
  205. }elseif ($v1 > 0 and $v2 > 0 and $v3 == 1){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  206. $jl = ($price * ($three - $tow)) * 0.05; // 拿到平级的百分之五
  207. $details['purple_integral'] = $jl * 0.7; // 百分之70的紫积分
  208. $details['business_integral'] = $jl * 0.3; // 百分之30的商家积分
  209. $v3++;
  210. }
  211. }
  212. if ($jl > 0){
  213. $details->save();
  214. UserBill::income('团队奖励紫积分', $details['uid'], 'purple_integral', 'team', $jl * 0.7, 0,$details['purple_integral'], 'v'.$details['level'].'团队奖励紫积分');
  215. UserBill::income('团队奖励商家积分', $details['uid'], 'business_integral', 'team', $jl * 0.3, 0,$details['business_integral'], 'v'.$details['level'].'团队奖励商家积分');
  216. }
  217. $jl = 0;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * 流水分红
  225. * @return void
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. */
  230. public static function flowing_water()
  231. {
  232. if (!Push::where('add_time', strtotime('today'))->find()) {
  233. $user = User::where('flowing_water', '>', 0)->select();
  234. if ($user) {
  235. foreach ($user as $item) {
  236. $price = ManyOrder::whereBetweenTime('return_time', strtotime('yesterday'), strtotime('today'))->sum('price');// 昨天众筹成功返还的流水
  237. if ($price > 0){
  238. $details = User::where('uid', $item['uid'])->find();
  239. $details['purple_integral'] = ($price * ($details['flowing_water'] / 100)) * 0.7; // 百分之70的紫积分
  240. $details['business_integral'] = ($price * ($details['flowing_water'] / 100)) * 0.3; // 百分之30的商家积分
  241. $details->save();
  242. UserBill::income('分红流水奖励紫积分', $details['uid'], 'purple_integral', 'team', ($price * ($details['flowing_water'] / 100)) * 0.7, 0, $details['purple_integral'], '分红流水奖励紫积分');
  243. UserBill::income('分红流水奖励商家积分', $details['uid'], 'business_integral', 'team', ($price * ($details['flowing_water'] / 100)) * 0.3, 0, $details['business_integral'], '分红流水奖励商家积分');
  244. }
  245. }
  246. }
  247. }
  248. }
  249. /**
  250. * 更新推送时间
  251. * @return void
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\DbException
  254. * @throws \think\db\exception\ModelNotFoundException
  255. */
  256. public static function time()
  257. {
  258. if (!Push::where('add_time', strtotime('today'))->find()) {
  259. Push::create(['add_time' => strtotime('today')]); //存入数据库信息
  260. }
  261. }
  262. }