ManyOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. $many = Many::where('id', $item['many_id'])->find();
  63. $user = User::where('uid', $item['uid'])->find();//用户
  64. $user1 = [];
  65. $user2 = [];
  66. if ($user['spread_uid'] > 0) {
  67. $user1 = User::where('uid', $user['spread_uid'])->find();//用户
  68. if ($user1['spread_uid'] > 0) $user2 = User::where('uid', $user1['spread_uid'])->find();//用户
  69. }
  70. $purple_integral = round($item['price'] * 1.07, 2);// 奖励紫积分积分
  71. $business_integral = round($item['price'] * 0.03, 2);// 奖励商家积分
  72. $user['purple_integral'] += $purple_integral;
  73. $user['integral'] += $business_integral;
  74. if ($user1) {
  75. // 直推收益的百分之十
  76. $sy1 = $item['price'] * 0.01;
  77. $user1['purple_integral'] += round($sy1 * 0.7, 2);
  78. $user1['integral'] += round($sy1 * 0.3, 2);
  79. }
  80. if ($user2) {
  81. // 间推收益的百分之五
  82. $sy2 = $item['price'] * 0.005;
  83. $user2['purple_integral'] += round($sy2 * 0.7, 2);
  84. $user2['integral'] += round($sy2 * 0.3, 2);
  85. }
  86. self::where('id', $item['id'])->update(['status' => 1, 'return_time' => time()]);
  87. $user->save();
  88. ManyOrder::push($item['uid'], $item);
  89. UserBill::income('种树成功补贴阳光', $user['uid'], 'purple_integral', 'zccg_purple_integral', $purple_integral, $user['spread_uid'], $user['purple_integral'], '种树成功补贴' . $many['name'] . '-第' . $item['stage'] . '期阳光');
  90. UserBill::income('种树成功补贴商城积分', $user['uid'], 'integral', 'zccg_integral', $business_integral, $user['spread_uid'], $user['integral'], '种树成功补贴' . $many['name'] . '-第' . $item['stage'] . '期商家积分');
  91. if ($user1) {
  92. UserBill::income('直推奖励阳光', $user1['uid'], 'purple_integral', 'zt_purple_integral', $sy1 * 0.7, $user1['spread_uid'], $user1['purple_integral'], '直推奖励阳光');
  93. UserBill::income('直推奖励商城积分', $user1['uid'], 'integral', 'zt_integral', $sy1 * 0.3, $user1['spread_uid'], $user1['integral'], '直推奖励商家积分');
  94. $user1->save();
  95. }
  96. if ($user2) {
  97. UserBill::income('间推奖励阳光', $user1['uid'], 'purple_integral', 'jt_purple_integral', $sy2 * 0.7, $user2['spread_uid'], $user2['purple_integral'], '间推奖励阳光');
  98. UserBill::income('间推奖励商城积分', $user1['uid'], 'integral', 'jt_integral', $sy2 * 0.3, $user2['spread_uid'], $user2['integral'], '间推奖励商家积分');
  99. $user2->save();
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 团队奖励
  106. * @return void
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public static function push($uid, $order)
  112. {
  113. $user = User::select();
  114. $find = User::where('uid', $uid)->find();
  115. $many = Many::where('id', $order['many_id'])->find();
  116. $price = $order['price'];
  117. if ($find['spread_uid'] > 0 and $price > 0){
  118. $spread = getParent($uid, $user->toArray());// 找到所有上级
  119. $v1 = 0;
  120. $v2 = 0;
  121. $v3 = 0;
  122. $one = SystemConfig::getConfigValue('v1')/100; // v1比例
  123. $tow = SystemConfig::getConfigValue('v2')/100;// v2比例
  124. $three = SystemConfig::getConfigValue('v3')/100;// v3比例
  125. foreach ($spread as $value) {
  126. $details = User::where('uid', $value)->find();
  127. if ($details['level'] == 1){
  128. if ($v2 == 0 and $v3 == 0){
  129. // 没有发放v2和v3的奖励
  130. if ($v1 == 0) { // 没有发放v1的奖励
  131. $jl = $price * $one;
  132. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  133. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  134. $v1++;
  135. }elseif ($v1 == 1){// 发放v1奖励1次
  136. $jl = ($price*$one)*0.05;//平级的百分之五
  137. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  138. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  139. $v1++;
  140. }
  141. }
  142. }elseif ($details['level'] == 2){
  143. if ($v3 == 0){
  144. // 没有发放v3的奖励
  145. if ($v1 == 0 and $v2 == 0) { // 没有发放v1和v2的奖励的奖励
  146. $jl = $price * $tow; // 拿到流水的百分之八
  147. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  148. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  149. $v2++;
  150. }elseif($v1 == 0 and $v2 == 1) { // 没有发放v1和v2的奖励的奖励
  151. $jl = ($price * $tow)*0.05; // 拿到流水的百分之八
  152. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  153. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  154. $v2++;
  155. }elseif ($v1 > 0 and $v2 == 0){// 发放v1奖励,没有发放v2的奖励
  156. $jl = $price * ($tow - $one); // 拿到流水减掉v1的百分之五
  157. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  158. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  159. $v2++;
  160. }elseif ($v1 > 0 and $v2 == 1){// 发放v1奖励,发放v2的奖励一次
  161. $jl = ($price * ($tow - $one))*0.05; // 拿到平级的百分之五
  162. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  163. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  164. $v2++;
  165. }
  166. }
  167. }elseif ($details['level'] == 3){
  168. if ($v1 == 0 and $v2 == 0 and $v3 == 0) { // 没有发放v1和v2v3的奖励的奖励
  169. $jl = $price * $three; // 拿到流水的百分之11
  170. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  171. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  172. $v3++;
  173. }elseif ($v1 == 0 and $v2 == 0 and $v3 == 1) {// 没有发放v1和v2的奖励的奖励
  174. $jl = ($price * $three) * 0.05; // 拿到平级的百分之五
  175. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  176. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  177. $v3++;
  178. }elseif ($v1 > 0 and $v2 == 0 and $v3 == 0){// 发放v1奖励,没有发放v2v3的奖励
  179. $jl = $price * ($three - $one); // 拿到流水减掉v1的百分之五
  180. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  181. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  182. $v3++;
  183. }elseif ($v1 > 0 and $v2 == 0 and $v3 == 1){// 发放v1奖励,没有发放v2v3的奖励
  184. $jl = ($price * ($three - $one)) * 0.05; // 拿到平级的百分之五
  185. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  186. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  187. $v3++;
  188. }elseif ($v1 == 0 and $v2 > 0 and $v3 == 0){// 发放v1奖励,发放v2的奖励,没有发放v3的奖励
  189. $jl = ($price * ($three - $tow)); // 拿到平级的百分之五
  190. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  191. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  192. $v3++;
  193. }elseif ($v1 == 0 and $v2 > 0 and $v3 == 1){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  194. $jl = ($price * ($three - $tow)) * 0.05; // 拿到平级的百分之五
  195. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  196. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  197. $v3++;
  198. }elseif ($v1 > 0 and $v2 > 0 and $v3 == 0){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  199. $jl = ($price * ($three - $tow)); // 拿到平级的百分之五
  200. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  201. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  202. $v3++;
  203. }elseif ($v1 > 0 and $v2 > 0 and $v3 == 1){// 发放v1奖励,发放v2的奖励,发放v3的奖励
  204. $jl = ($price * ($three - $tow)) * 0.05; // 拿到平级的百分之五
  205. $details['purple_integral'] += $jl * 0.7; // 百分之70的紫积分
  206. $details['integral'] += $jl * 0.3; // 百分之30的商家积分
  207. $v3++;
  208. }
  209. }else{
  210. $jl = 0;
  211. }
  212. if ($jl > 0){
  213. $details->save();
  214. UserBill::income('团队奖励阳光', $details['uid'], 'purple_integral', 'td_purple_integral', $jl * 0.7, 0,$details['purple_integral'], '打怪胜利补贴'.$many['name'].'-第'.$order['stage'].'期团队v'.$details['level'].'奖励阳光');
  215. UserBill::income('团队奖励商城积分', $details['uid'], 'integral', 'td_integral', $jl * 0.3, 0,$details['integral'], '打怪胜利补贴'.$many['name'].'-第'.$order['stage'].'期期团队v'.$details['level'].'奖励商城积分');
  216. }
  217. $jl = 0;
  218. }
  219. }
  220. }
  221. /**
  222. * 流水分红
  223. * @return void
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\DbException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. */
  228. public static function flowing_water()
  229. {
  230. if (!Push::where('add_time', strtotime('today'))->find()) {
  231. $user = User::where('flowing_water', '>', 0)->select();
  232. if ($user) {
  233. foreach ($user as $item) {
  234. $price = ManyOrder::whereBetweenTime('return_time', strtotime('yesterday'), strtotime('today'))->sum('price');// 昨天众筹成功返还的流水
  235. if ($price > 0) {
  236. $details = User::where('uid', $item['uid'])->find();
  237. $details['purple_integral'] += ($price * ($details['flowing_water'] / 100)) * 0.7; // 百分之70的紫积分
  238. $details['integral'] += ($price * ($details['flowing_water'] / 100)) * 0.3; // 百分之30的商家积分
  239. $details->save();
  240. UserBill::income('分红流水奖励阳光', $details['uid'], 'purple_integral', 'team_purple_integral', ($price * ($details['flowing_water'] / 100)) * 0.7, 0, $details['purple_integral'], '分红流水奖励阳光');
  241. UserBill::income('分红流水奖励商城积分', $details['uid'], 'integral', 'team_integral', ($price * ($details['flowing_water'] / 100)) * 0.3, 0, $details['integral'], '分红流水奖励商城积分');
  242. }
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * 更新推送时间
  249. * @return void
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\DbException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. */
  254. public static function time()
  255. {
  256. if (!Push::where('add_time', strtotime('today'))->find()) {
  257. Push::create(['add_time' => strtotime('today')]); //存入数据库信息
  258. }
  259. }
  260. }