UserThirdRepository.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-28
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\user;
  11. use app\common\dao\user\UserThirdDao;
  12. use app\common\model\user\User;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use app\model\common\TbOrderModel;
  17. use think\exception\ValidateException;
  18. use think\facade\Log;
  19. use think\test\queue\LoopBreakerException;
  20. use function GuzzleHttp\Psr7\str;
  21. use think\facade\Db;
  22. //use think\Db;
  23. /**
  24. * Class UserRepository
  25. * @package app\common\repositories\user
  26. * @author xaboy
  27. * @day 2020-04-28
  28. * @mixin UserThirdDao
  29. */
  30. class UserThirdRepository extends BaseRepository
  31. {
  32. /**
  33. * UserRepository constructor.
  34. * @param UserThirdDao $dao
  35. */
  36. public function __construct(UserThirdDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /*创建用户第三方pid*/
  41. public function createPid($data = [])
  42. {
  43. $res = Db::name("user_third")->insertGetId($data);
  44. return $res;
  45. }
  46. /*获取第三方pid*/
  47. public function getDetail($where = [], $field = "*")
  48. {
  49. $res = Db::name("user_third")->where($where)->field($field)->find();
  50. return $res;
  51. }
  52. /*获取配置信息*/
  53. public function config()
  54. {
  55. $config = $data = [
  56. 'promoter_rate_technology' => systemConfig('promoter_rate_technology'), //技术服务费
  57. 'yanglao_Meeting' => systemConfig('yanglao_Meeting'),//会费比例
  58. 'yanglao_Meeting_total' => systemConfig('yanglao_Meeting_total'),//会费总金额
  59. 'yanglaojin_admin' => systemConfig('yanglaojin_admin'), //养老金比例
  60. 'mer_rate' => systemConfig('mer_rate'), //商家比例
  61. 'third_day' => systemConfig('third_day'), //第三方订单结算天数
  62. ];
  63. return $config;
  64. }
  65. /*获取信息*/
  66. public function getOrderDetail($table, $where = [], $field = "*")
  67. {
  68. $res = DB::name("$table")->where($where)->field($field)->find();
  69. return $res;
  70. }
  71. /*创建订单*/
  72. public function createOrder($table, $data, $time = 0)
  73. {
  74. //获取订单结算时间
  75. $day = $this->config();
  76. if ($time > 0) {
  77. $data['bill_end_time'] = $time + $day['third_day'] * 24 * 60 * 60;
  78. }
  79. $res = Db::name("$table")->insertGetId($data);
  80. return $res;
  81. }
  82. //获取第三方订单的计算时间
  83. public function getSettleTime($time)
  84. {
  85. //获取订单结算时间
  86. $day = $this->config();
  87. if ($time > 0) {
  88. $time = $time + $day['third_day'] * 24 * 60 * 60;
  89. }
  90. return $time;
  91. }
  92. /*修改订单*/
  93. public function saveOrder($table, $where = [], $data = [])
  94. {
  95. $res = Db::name("$table")->where($where)->update($data);
  96. return $res;
  97. }
  98. /*计算用户本年已经扣除的会费*/
  99. public function getTotalHui($uid)
  100. {
  101. $data = Db::name("admin_bill")->where(['pm' => 0, 'status' => 1, 'type' => 2, 'link_id' => $uid])->count("number");
  102. return $data;
  103. }
  104. /*获取用户商家信息*/
  105. public function getMer($uid)
  106. {
  107. $data = Db::name("user")->where(['uid' => $uid])->value("mer_id");
  108. return $data;
  109. }
  110. /*处理订单反养老金逻辑
  111. *$order_id 订单id
  112. *$commission 佣金
  113. * $pid 用户广告位id
  114. * $order_sn 订单号
  115. * $table 表名
  116. */
  117. public function successOrder($order_id, $commission, $pid, $order_sn, $type, $table)
  118. {
  119. try {
  120. $bill= Db::name("user_bill")->where("order_sn",$order_sn)->select();
  121. foreach ($bill as $v){
  122. if ($v["commission_type"]==5){
  123. Db::name("user")->where("uid",$v["uid"])->inc('annuity',$v["number"]) ->update();
  124. Db::name("user")->where("uid",$v["uid"])->inc('annuity_num', 1) ->update();
  125. }else{
  126. if($v['title']!='平台奖励'){
  127. Db::name("user")->where("uid",$v["uid"])->inc('brokerage_price', $v["number"]) ->update();
  128. }else{
  129. Db::name("user")->where("uid",$v["uid"])->inc('now_score', $v["number"]) ->update();
  130. }
  131. }
  132. $annuity_once = Db::name("user")->where("uid",$v['uid'])->value('annuity_once');
  133. if($annuity_once == 0){
  134. $spreadUid = Db::name('user') -> where('uid',$v['uid']) -> value('spread_uid');
  135. // Db::name('user')->where('uid',$spreadUid)->inc('score', systemConfig('ylj_first_score'))->update();
  136. // $ins_data['uid'] = $spreadUid;
  137. // $ins_data['reward_socre'] = systemConfig('ylj_first_score');
  138. // $ins_data['addtime'] = time();
  139. // $ins_data['status'] = 1;
  140. // $ins_data['mark'] = '好友首次获得已结算养老金奖励积分';
  141. // Db::name('user_sign_score')->insert($ins_data);
  142. $userRepository = app()->make(UserRepository::class);
  143. $userRepository -> sign_add($spreadUid,systemConfig('ylj_first_score'),1,'好友首次获得已结算养老金奖励积分','','','');
  144. Db::name("user")->where("uid",$v['uid'])->update(['annuity_once'=>1]);
  145. }
  146. }
  147. Db::name("user_bill")->where("order_sn",$order_sn)->update(["status"=>1]);
  148. $orderUpdate = ['bill_status' => 1];
  149. if ($type == 6) {//京东订单
  150. $orderUpdate['bill_create_time'] = time();
  151. } else {
  152. $orderUpdate['bill_create_time'] = date("Y-m-d H:i:s");
  153. }
  154. Db::name("$table")->where($table . "_id", $order_id)->update($orderUpdate);
  155. } catch (\Exception $e) {
  156. log::error($e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  157. throw new ValidateException($e->getMessage());
  158. }
  159. }
  160. public function invalidOrder($order_sn){
  161. return Db::name("user_bill")->where("order_sn",$order_sn)->update(["status"=>-1]);
  162. }
  163. /*处理订单预估反养老金逻辑
  164. *$order_id 订单id
  165. *$commission 佣金
  166. * $pid 用户广告位id
  167. * $order_sn 订单号
  168. * $table 表名
  169. */
  170. public function yuguOrder($order_id, $commission, $pid, $order_sn, $type, $table)
  171. {
  172. //配置信息
  173. // $config = $this->config();
  174. //用户pid信息
  175. // if ($type == 6) {//京东联盟改成京推推
  176. // $user['uid'] = intval($pid - pow(10, 5));
  177. // } else {
  178. // $user = $this->getDetail(['pid' => $pid, 'type' => $type]);
  179. // }
  180. //用户商家信息
  181. $user_mer = $this->getMer($pid);
  182. try {
  183. $money=$commission;
  184. //返佣比例
  185. $base_commission= floatval(systemConfig('extension_tao_rate'));
  186. //刷卡手续费
  187. $souxufees=0;
  188. $souxufee =floatval(systemConfig('yanglao'));
  189. if ($souxufee){
  190. $souxufees = bcmul($money, $souxufee * 0.01, 3);
  191. }
  192. //返佣总额 返利总额=(交易金额-手续费)*返利比例
  193. $moneys=bcmul($money-$souxufees,$base_commission/100,3);
  194. $extension_one=$moneys;
  195. $order=[
  196. "order_sn"=>$order_sn,
  197. "order_id"=>$order_id,
  198. "uid"=>$pid,
  199. "mer_id"=>$user_mer,
  200. "total_price"=>$commission,
  201. "extension_one"=>$extension_one,
  202. "type_shop"=>$type,
  203. "table"=>$table
  204. ];
  205. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  206. // $StoreOrderRepository->yugu_rebate($order);
  207. log::info("===待进入养老金22222222222");
  208. $StoreOrderRepository->yugu_rebate_gong_fu($order);
  209. // $money_hui = 0;
  210. // $money_user = 0;
  211. // $money_jishu = 0;
  212. // $money_mer = 0;
  213. // //给用户返
  214. // $huiTotal = $this->getTotalHui($user['uid']); //计算已经扣除的会费
  215. // if ($huiTotal < $config['yanglao_Meeting_total']) {
  216. // $money_hui = $commission * ($config['yanglaojin_admin'] / 100) * ($config['yanglao_Meeting'] / 100);//会费
  217. // //存储会费记录
  218. // if (number_format($money_hui, 2) >= 0.01) {
  219. // $this->admin_bill($user['uid'], $money_hui, '养老金扣除会费' . $config['yanglao_Meeting'] . '%', '第三方平台', $order_sn, '2');
  220. // }
  221. // $money_user = ($commission * $config['yanglaojin_admin']) / 100 - $money_hui;//养老金
  222. // } else {
  223. // $money_user = $commission * $config['yanglaojin_admin'] / 100;
  224. // }
  225. //// if (number_format($money_user, 2) >= 0.01) {
  226. //// $this->add_bill($order_id, $user['uid'], $money_user, $order_sn, $type, '购买商品获得养老金' . $config['yanglaojin_admin'] . '%', '第三方平台',$type,0);
  227. //// //修改用户养老金信息
  228. //// Db::name("user")->where("uid", $user['uid'])->inc('annuity', $money_user)->update();
  229. //// }
  230. // //给用户所属商家返
  231. // if ($user_mer) {
  232. // $money_jishu = $commission * $config['mer_rate'] / 100 * $config['promoter_rate_technology'] / 100;
  233. // $money_mer = $commission * $config['mer_rate'] / 100 - $money_jishu;
  234. // if (number_format($money_jishu, 2) >= 0.01) {
  235. // $this->admin_bill($user_mer, $money_jishu, '佣金扣除技术服务费' . $config['yanglao_Meeting'] . '%', '第三方平台', $order_sn, '4');
  236. // }
  237. // if (number_format($money_mer, 2) >= 0.01) {
  238. // $this->add_bill($order_id, $user_mer, $money_mer, $order_sn, $type, '用户购买商品获得佣金' . $config['mer_rate'] . '%', '第三方平台',$type,0);
  239. // //修改商户佣金
  240. // Db::name("user")->where("uid", $user_mer)->inc('brokerage_price', $money_mer)->update();
  241. // }
  242. // }
  243. // //所有金额都小于0.01 佣金金额全归平台所有
  244. // if (number_format($money_hui, 2) <= 0 && number_format($money_jishu, 2) <= 0 && number_format($money_user, 2) <= 0 && number_format($money_mer, 2) <= 0) {
  245. // $this->admin_bill($user['uid'], $commission, '分成后所有金额小于一分存于平台', '第三方平台,不参与用户分成', $order_sn, '5');
  246. // }
  247. //修改订单结算状态
  248. // $orderUpdate = ['bill_status' => 1];
  249. // if ($type == 6) {//京东订单
  250. // $orderUpdate['bill_create_time'] = time();
  251. // } else {
  252. // $orderUpdate['bill_create_time'] = date("Y-m-d H:i:s");
  253. // }
  254. // Db::name("$table")->where($table . "_id", $order_id)->update($orderUpdate);
  255. } catch (\Exception $e) {
  256. throw new ValidateException($e->getMessage());
  257. }
  258. }
  259. /**
  260. * 平台账单记录
  261. * `type` int(1) DEFAULT '0' COMMENT '类型 1刷卡手续费 2 会费',
  262. * `number` decimal(8,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '明细数字',
  263. * `title` varchar(64) NOT NULL COMMENT '账单标题',
  264. * `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0 = 待确定 1 = 有效 -1 = 无效',
  265. */
  266. public function admin_bill($uid, $number, $title, $mark, $order_sn, $type)
  267. {
  268. $number = round($number, 2);
  269. $data = [
  270. 'link_id' => $uid,
  271. "pm" => 1,
  272. "title" => $title,
  273. // "category"=>"now_money",
  274. "type" => $type,
  275. "number" => $number,
  276. // "balance"=>0,
  277. "mark" => $mark,
  278. "status" => 1,
  279. "order_sn" => $order_sn,
  280. ];
  281. Db::name("admin_bill")->insert($data);
  282. }
  283. /**
  284. * 账单记录
  285. * @param $order
  286. * @param $spread_uid
  287. * @param $commission
  288. * @param string $title
  289. * @param $mark
  290. * @throws \think\db\exception\DbException
  291. */
  292. public function add_bill($order_id, $uid, $commission, $order_sn, $type, $title = "养老金", $mark = '',$type_shop=0,$status=1)
  293. {
  294. $data = [
  295. 'link_id' => $order_id,
  296. "uid" => $uid,
  297. "title" => $title,
  298. "category" => "now_money",
  299. "type" => "commission",
  300. "number" => $commission,
  301. "balance" => 0,
  302. "mark" => $mark,
  303. "status" =>$status,
  304. // 'type' => $type,
  305. "commission_type" => 3,
  306. "order_sn" => $order_sn,
  307. "type_shop"=>$type_shop
  308. ];
  309. Db::name("user_bill")->insert($data);
  310. // Db::name("user")->where("uid",$spread_uid)->inc('brokerage_price', $commission) ->update();
  311. }
  312. /*淘宝物料分类*/
  313. public function getMaterial()
  314. {
  315. $data = Db::name("material")->where(['status' => 1])->select();
  316. return $data;
  317. }
  318. /*京东订单*/
  319. public function GetJdOrder($where = [], $page = 1, $limit = 10)
  320. {
  321. $query = Db::name("order_jd");
  322. $query = $query->when(isset($where['date']) && $where['date'] != '', function ($query) use ($where) {
  323. getModelTime($query, $where['date']);
  324. })->when(isset($where['status']) && $where['status'] != '', function ($query) use ($where) {
  325. $query->where('status', $where['status']);
  326. })->when(isset($where['order_sn']) && $where['order_sn'] != '', function ($query) use ($where) {
  327. $query->where('order_sn', $where['order_sn']);
  328. })->when($where['bill_status'] != '', function ($query) use ($where) {
  329. $query->where('bill_status', $where['bill_status']);
  330. })->when(isset($where['keywords']) && $where['keywords'] != '', function ($query) use ($where) {
  331. $query->whereLike("shop_name|uid", "%" . $where['keywords'] . "%");
  332. });
  333. $count = $query->count();
  334. $list = $query->page($page, $limit)->order('order_jd_id DESC')->select();
  335. $sql = $query->getLastSql();
  336. $userList = User::whereIn('uid', $list->column('uid'))->column('*', 'uid');
  337. $list->each(function ($value) use ($userList) {
  338. $info = $userList[$value['uid']] ?? [];
  339. if ($info) {
  340. $value['account'] = $info['account'];
  341. } else {
  342. $value['account'] = '匹配用户不存在';
  343. }
  344. return $value;
  345. });
  346. return compact('count', 'list', 'sql');
  347. }
  348. /*拼多多订单*/
  349. public function GetPddOrder($where = [], $page = 1, $limit = 10)
  350. {
  351. $query = Db::name("order_pdd");
  352. $query = $query->when(isset($where['date']) && $where['date'] != '', function ($query) use ($where) {
  353. getModelTime($query, $where['date']);
  354. })->when(isset($where['pin_order_status']) && $where['pin_order_status'] != '', function ($query) use ($where) {
  355. $query->where('pin_order_status', $where['pin_order_status']);
  356. })->when(isset($where['order_sn']) && $where['order_sn'] != '', function ($query) use ($where) {
  357. $query->where('order_sn', $where['order_sn']);
  358. })->when($where['bill_status'] != '', function ($query) use ($where) {
  359. $query->where('bill_status', $where['bill_status']);
  360. })->when(isset($where['keywords']) && $where['keywords'] != '', function ($query) use ($where) {
  361. $query->whereLike("goods_name|uid", "%" . $where['keywords'] . "%");
  362. });
  363. $count = $query->count();
  364. $list = $query->page($page, $limit)->order('create_time DESC')->select();
  365. $userList = User::whereIn('uid', $list->column('uid'))->column('*', 'uid');
  366. $list->each(function ($value) use ($userList) {
  367. $info = $userList[$value['uid']] ?? [];
  368. if ($info) {
  369. $value['account'] = $info['account'];
  370. } else {
  371. $value['account'] = '匹配用户不存在';
  372. }
  373. return $value;
  374. });
  375. return compact('count', 'list');
  376. }
  377. /*唯品会订单*/
  378. public function GetVipOrder($where = [], $page = 1, $limit = 10)
  379. {
  380. $query = Db::name("order_vip");
  381. $query = $query->when(isset($where['date']) && $where['date'] != '', function ($query) use ($where) {
  382. getModelTime($query, $where['date']);
  383. })
  384. ->when(isset($where['settled']) && $where['settled'] != '', function ($query) use ($where) {
  385. $query->where('settled', $where['settled']);
  386. })->when(isset($where['order_status']) && $where['order_status'] != '', function ($query) use ($where) {
  387. $query->where('order_status', $where['order_status']);
  388. })->when(isset($where['order_sn']) && $where['order_sn'] != '', function ($query) use ($where) {
  389. $query->where('order_sn', $where['order_sn']);
  390. })->when($where['bill_status'] != '', function ($query) use ($where) {
  391. $query->where('bill_status', $where['bill_status']);
  392. })->when(isset($where['keywords']) && $where['keywords'] != '', function ($query) use ($where) {
  393. $query->whereLike("goods_name|uid", "%" . $where['keywords'] . "%");
  394. });
  395. $count = $query->count();
  396. $list = $query->page($page, $limit)->order('order_vip_id DESC')->select()->each(function ($value) {
  397. $orderTime = $value['order_time'];
  398. // $value['order_time'] = date('Y-m-d H:i:s', $orderTime);
  399. //订单关联用户信息
  400. $value['account'] = User::where('uid', $value['uid'])->value('account', '匹配用户不存在');
  401. return $value;
  402. });
  403. return compact('count', 'list');
  404. }
  405. /*苏宁订单*/
  406. public function GetSuOrder($where = [], $page = 1, $limit = 10)
  407. {
  408. $query = Db::name("order_su");
  409. $query = $query->when(isset($where['date']) && $where['date'] != '', function ($query) use ($where) {
  410. getModelTime($query, $where['date']);
  411. })->when(isset($where['status']) && $where['status'] != '', function ($query) use ($where) {
  412. $query->where('status', $where['status']);
  413. })->when(isset($where['order_sn']) && $where['order_sn'] != '', function ($query) use ($where) {
  414. $query->where('order_sn', $where['order_sn']);
  415. })->when($where['bill_status'] != '', function ($query) use ($where) {
  416. $query->where('bill_status', $where['bill_status']);
  417. })->when(isset($where['keywords']) && $where['keywords'] != '', function ($query) use ($where) {
  418. $query->whereLike("goods_name|uid", "%" . $where['keywords'] . "%");
  419. });
  420. $count = $query->count();
  421. $list = $query->page($page, $limit)->order('create_time DESC')->select()->each(function ($value) {
  422. $orderTime = $value['order_time'];
  423. $value['order_time'] = date('Y-m-d H:i:s', $orderTime);
  424. $payTime = $value['pay_time'];
  425. $value['pay_time'] = date('Y-m-d H:i:s', $payTime);
  426. //订单关联用户信息
  427. $value['account'] = User::where('uid', $value['uid'])->value('account', '匹配用户不存在');
  428. return $value;
  429. });
  430. return compact('count', 'list');
  431. }
  432. /*淘宝订单*/
  433. public function GetTbOrder($where = [], $page = 1, $limit = 10)
  434. {
  435. $query = Db::name("order_tb");
  436. $query = $query->when(isset($where['date']) && $where['date'] != '', function ($query) use ($where) {
  437. getModelTime($query, $where['date']);
  438. })->when(isset($where['tk_status']) && $where['tk_status'] != '', function ($query) use ($where) {
  439. $query->where('tk_status', $where['tk_status']);
  440. })->when(isset($where['order_sn']) && $where['order_sn'] != '', function ($query) use ($where) {
  441. $query->where('order_sn', $where['order_sn']);
  442. })->when($where['bill_status'] != '', function ($query) use ($where) {
  443. $query->where('bill_status', $where['bill_status']);
  444. })->when(isset($where['keywords']) && $where['keywords'] != '', function ($query) use ($where) {
  445. $query->whereLike("item_title|uid", "%" . $where['keywords'] . "%");
  446. });
  447. $count = $query->count();
  448. $list = $query->page($page, $limit)->order('create_time DESC')->select()->each(function ($value) {
  449. $orderTime = $value['create_time'];
  450. // $value['create_time'] = date('Y-m-d H:i:s', $orderTime);
  451. //淘客订单状态
  452. $tkStatus = $value['tk_status'];
  453. $tkStatusName = TbOrderModel::$statusName[$tkStatus] ?? '未知状态';
  454. $value['status_name'] = $tkStatusName;
  455. //订单关联用户信息
  456. $value['account'] = User::where('uid', $value['uid'])->value('account', '匹配用户不存在');
  457. return $value;
  458. });
  459. return compact('count', 'list');
  460. }
  461. /*详情*/
  462. public function GetThirdDetail($id, $table)
  463. {
  464. $data = Db::name("order_" . $table)->where(['order_' . $table . '_id' => $id])->find();
  465. //养老金修改
  466. $commission = $data['commission'];
  467. $yanglao = 0;
  468. if ($commission) {
  469. $yanglao = app()->make(ProductRepository::class)->yanglaojin(0, $commission);
  470. }
  471. $data['yanglao'] = $yanglao;
  472. switch ($table) {
  473. case 'jd':
  474. $billEndTime = $data['bill_end_time'];
  475. $data['bill_end_time'] = $billEndTime ? date('Y-m-d H:i:s', $billEndTime) : '';
  476. break;
  477. case 'vip':
  478. $orderTime = $data['order_time'];
  479. $data['order_time'] = date('Y-m-d H:i:s', $orderTime);
  480. $updateTime = $data['last_update_time'];
  481. $data['last_update_time'] = date('Y-m-d H:i:s', $updateTime);
  482. $billEndTime = $data['bill_end_time'];
  483. $data['bill_end_time'] = $billEndTime ? date('Y-m-d H:i:s', $billEndTime) : '';
  484. break;
  485. case 'pdd':
  486. $payTime = $data['pay_time'];
  487. $data['pay_time'] = $payTime ? date('Y-m-d H:i:s', $payTime) : '';
  488. $groupTime = $data['group_time'];
  489. $data['group_time'] = $groupTime ? date('Y-m-d H:i:s', $groupTime) : '';
  490. $billEndTime = $data['bill_end_time'];
  491. $data['bill_end_time'] = $billEndTime ? date('Y-m-d H:i:s', $billEndTime) : '';
  492. break;
  493. case 'su':
  494. $payTime = $data['pay_time'];
  495. $data['pay_time'] = $payTime ? date('Y-m-d H:i:s', $payTime) : '';
  496. $billEndTime = $data['bill_end_time'];
  497. $data['bill_end_time'] = $billEndTime ? date('Y-m-d H:i:s', $billEndTime) : '';
  498. break;
  499. case 'tb':
  500. $createTime = $data['create_time'];
  501. $data['create_time'] = $createTime ? $createTime : '';
  502. $billEndTime = $data['bill_end_time'];
  503. $data['bill_end_time'] = $billEndTime ? date('Y-m-d H:i:s', $billEndTime) : '';
  504. break;
  505. }
  506. return $data;
  507. }
  508. /*pdd满足分佣的订单*/
  509. public function pddOrder()
  510. {
  511. $query = Db::name("order_pdd");
  512. $data = $query->where(['pin_order_status' => 5, 'bill_status' => 0])
  513. ->whereTime("bill_end_time", '<=', time())
  514. ->order('create_time DESC')
  515. ->select()->toArray();
  516. return $data;
  517. }
  518. /*京东满足分佣的订单*/
  519. public function JdOrder()
  520. {
  521. $query = Db::name("order_jd");
  522. $data = $query->where(['status' => 17, 'bill_status' => 0])
  523. ->whereTime("bill_end_time", '<=', time())
  524. ->order('order_jd_id DESC')
  525. ->select()->toArray();
  526. return $data;
  527. }
  528. /*抖音满足分佣的订单*/
  529. public function dyOrder()
  530. {
  531. $query = Db::name("order_dy");
  532. $data = $query->where(['flow_point' => 'SETTLE', 'bill_status' => 0])
  533. ->whereTime("bill_end_time", '<=', time())
  534. ->order('order_dy_id DESC')
  535. ->select()->toArray();
  536. return $data;
  537. }
  538. /*苏宁满足分佣的订单*/
  539. public function SuOrder()
  540. {
  541. $query = Db::name("order_su");
  542. $data = $query->where(['status' => '已结算', 'bill_status' => 0])
  543. ->whereTime("bill_end_time", '<=', time())
  544. ->order('create_time DESC')
  545. ->select()->toArray();
  546. return $data;
  547. }
  548. /**
  549. * 修改积分,养老金,贡献值记录状态
  550. */
  551. public static function updateJilu($user_id,$order_sn,$type_shop){
  552. // if (empty($user_id)) {
  553. // Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, $order_sn, 修改积分,养老金,贡献值记录, 不存在用户ID"]);
  554. // return false;
  555. // }
  556. log::info("订单修改开始");
  557. // 更新养老金
  558. Db::name("user_bill")->where("type_shop", $type_shop)->where("order_sn", $order_sn)->update(['status'=>-1]);
  559. //更新积分
  560. Db::name("user_sign_score")->where("order_type", $type_shop)->where("order_id", $order_sn)->update(['status'=>0]);
  561. //更新贡献值
  562. Db::name("user_sign_gongxian")->where("order_type", $type_shop)->where("order_id", $order_sn)->update(['status'=>0]);
  563. //更新红包
  564. Db::name("user_sign_hongbao")->where("order_type", $type_shop)->where("order_id", $order_sn)->update(['status'=>0]);
  565. log::info("订单 {$order_sn} 用户id ={$user_id} 三方来源={$type_shop} 积分,贡献值,养老金,红包更新更新");
  566. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, $order_sn, 修改积分,养老金,贡献值,红包记录, 更新成功!!!"]);
  567. }
  568. /**
  569. * 添加预估积分记录
  570. */
  571. //预估养老金
  572. public static function yuguJifen($price,$user_id,$order_sn,$type_shop)
  573. {
  574. if (empty($user_id)) {
  575. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, $order_sn, 赠送积分, 不存在用户ID"]);
  576. return false;
  577. }
  578. //查询用户是否已经存在该订单
  579. $user_sign_score_list = Db::name('user_sign_score')
  580. ->where('uid', $user_id)
  581. ->where('order_type', $type_shop)
  582. ->where('order_id', $order_sn)
  583. ->find();
  584. if (!empty($user_sign_score_list)) {
  585. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, $order_sn, 赠送积分, 订单已存在积分发放记录"]);
  586. return false;
  587. }
  588. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, $order_sn, 赠送积分, $price"]);
  589. try {
  590. //更新用户积分
  591. $user = Db::name('user')->where('uid', $user_id)->find();
  592. $systemvalueObj= $goods_obj = Db::name('system_config_value')
  593. -> where('config_key','sanfangfenpei')->find();;
  594. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  595. $inc_score = round($price*$sanfangfenpei['pv'],2);//pv=佣金的50%=积分=贡献值
  596. // log::info("===========dddddddddddddddddddd");
  597. //每个表的统计值
  598. $jfzongwei=Db::name('user_sign_score')->where('status', -1)->where('uid', $user_id)->sum('reward_socre');
  599. $gxzongwei=Db::name('user_sign_gongxian')->where('status', -1)->where('uid', $user_id)->sum('number');
  600. // type_shop 1 拼多多 ,5淘宝
  601. // log::info("==========={$jfzongwei}");
  602. $make="";
  603. if($type_shop==1){
  604. $make="拼多多";
  605. }else if($type_shop==5){
  606. $make="淘宝";
  607. }else if($type_shop==6){
  608. $make="京东";
  609. }else if($type_shop==3){
  610. $make="苏宁";
  611. }else if($type_shop==2){
  612. $make="唯品会";
  613. }
  614. $user_score = $user['score'];
  615. $surplus=$user_score + $inc_score+$jfzongwei;
  616. $surplusgongxian=$user['contribute'] + $gxzongwei;
  617. $data = [
  618. "uid" => $user_id,
  619. "reward_socre" => $inc_score,
  620. "addtime" => time(),
  621. "status" => -1,
  622. "order_type" => $type_shop,
  623. "order_id" => $order_sn,
  624. "mark" => "购买{$make}商品赠送积分",
  625. "surplus" => $surplus,
  626. "pm" => 1,
  627. "source_type" => 3
  628. ];
  629. log::info("{$make}==准备开始添加积分,贡献");
  630. Db::name("user_sign_score")->insert($data);//添加积分记录
  631. log::info("准备开始添加积分");
  632. // $ins_data['uid'] = $user_id;
  633. // $ins_data['reward_socre'] = $inc_score;
  634. // $ins_data['addtime'] = time();
  635. // $ins_data['status'] = -1;
  636. // $ins_data['order_type'] = $type_shop;
  637. // $ins_data['order_id'] = $order_sn;
  638. // $ins_data['mark'] = "购买三方商品赠送积分";
  639. // $ins_data['desc'] = '';
  640. // $ins_data['surplus'] = $user_score + $inc_score;
  641. // $ins_data['type'] = 1;
  642. // $ins_data['pm'] = 1;
  643. // $ins_data['source_type'] =3;
  644. //赠送贡献值
  645. $gongxian_data = [
  646. "uid" =>$user_id,
  647. "number" => $inc_score,
  648. "addtime" =>time(),
  649. "status" =>-1,
  650. "order_type" =>$type_shop,
  651. "order_id" =>$order_sn,
  652. "mark" =>"购买{$make}商品赠送贡献值",
  653. "desc" =>'',
  654. "surplus" =>$surplusgongxian+$gxzongwei,
  655. "type" =>1
  656. ];
  657. // $gongxian_data['uid'] = $user_id;
  658. // $gongxian_data['number'] = $inc_score;
  659. // $gongxian_data['addtime'] = time();
  660. // $gongxian_data['status'] = -1;
  661. // $gongxian_data['order_type'] = $type_shop;
  662. // $gongxian_data['order_id'] = $order_sn;
  663. // $gongxian_data['mark'] = "购买三方商品赠送贡献值";
  664. // $gongxian_data['desc'] = '';
  665. // $gongxian_data['surplus'] = $user_score + $inc_score;
  666. // $gongxian_data['type'] = 1;
  667. // $gongxian_data['pm'] = 1;
  668. // $gongxian_data['source_type'] =3;
  669. Db::name("user_sign_gongxian")->insert($gongxian_data);//添加贡献记录
  670. log::info("===添加完积分和贡献值");
  671. //判断是否是老用户 is_old=1是1.0的用户,1.0的用户要总pv*45%的红包
  672. if($user["is_old"]==1){
  673. //添加红包
  674. $hongbao=round($inc_score*0.45,2);
  675. $hongbao_data = [
  676. "uid" =>$user_id,
  677. "number" => $hongbao,
  678. "addtime" =>time(),
  679. "status" =>-1,
  680. "order_type" =>$type_shop,
  681. "order_id" =>$order_sn,
  682. "mark" =>"购买{$make}商品赠送红包",
  683. "desc" =>'',
  684. "surplus" =>$hongbao,
  685. "type" =>1
  686. ];
  687. Db::name("user_sign_hongbao")->insert($hongbao_data);//添加贡献记录
  688. log::info("===添加完红包奖励");
  689. }
  690. // 添加推荐奖励
  691. $sprerdUser= Db::name("user")->where('spread_id',$user_id)->find();
  692. //不为空的时候进行添加推荐奖励
  693. if($sprerdUser){
  694. //奖励推广佣金
  695. $yongjinbili=0;
  696. //普通用户
  697. if($sprerdUser["user_group"]==1){
  698. $yongjinbili=5;
  699. }
  700. //业务员
  701. if($sprerdUser["user_group"]==2){
  702. $yongjinbili=7;
  703. }
  704. //业务经理
  705. if($sprerdUser["user_group"]==3){
  706. $yongjinbili=9;
  707. }
  708. //初级合伙人
  709. if($sprerdUser["user_group"]==4){
  710. $yongjinbili=11;
  711. }
  712. //中级合伙人
  713. if($sprerdUser["user_group"]==5){
  714. $yongjinbili=13;
  715. }
  716. //高级合伙人
  717. if($sprerdUser["user_group"]==6){
  718. $yongjinbili=15;
  719. }
  720. //懂事
  721. if($sprerdUser["user_group"]==7){
  722. $yongjinbili=17;
  723. }
  724. $tuiguanyongjin=round($yongjinbili*$inc_score/100,2);//按照身份获得佣金
  725. //添加推广积分
  726. $tuijifendata = [
  727. "uid" => $sprerdUser['uid'],
  728. "reward_socre" => $tuiguanyongjin,
  729. "addtime" => time(),
  730. "status" => -1,
  731. "order_type" => $type_shop,
  732. "order_id" => $order_sn,
  733. "mark" => "推广消费从{$make}购买商品赠送积分",
  734. "surplus" => $surplus,
  735. "pm" => 1,
  736. "source_type" => 4
  737. ];
  738. log::info("{$make}==准备开始添加推广积分,贡献");
  739. Db::name("user_sign_score")->insert($tuijifendata);//添加积分记录
  740. //添加献值
  741. $tuigangongxian_data = [
  742. "uid" =>$user_id,
  743. "number" => $tuiguanyongjin,
  744. "addtime" =>time(),
  745. "status" =>-1,
  746. "order_type" =>$type_shop,
  747. "order_id" =>$order_sn,
  748. "mark" =>"推广消费从{$make}购商品赠送贡献值",
  749. "desc" =>'',
  750. "surplus" =>$surplusgongxian+$gxzongwei,
  751. "type" =>1
  752. ];
  753. Db::name("user_sign_gongxian")->insert($tuigangongxian_data);//添加贡献记录
  754. log::info("===添加完积分和贡献值");
  755. }
  756. //
  757. // Db::name('user')->where('uid', $user_id)->inc('score', $inc_score)->update();
  758. // Db::name('user')->where('uid', $user_id)->inc('contribute', $inc_score)->update();
  759. // $now_score= Db::name('user')->where('uid',$user_id)->value('now_score_estimate');
  760. // $now_score_no= Db::name('user')->where('uid',$user_id)->value('now_score');
  761. // Db::name('user')->where('uid',$user_id)->update(['now_score_estimate'=>$now_score+$price]);
  762. // $insertDta=[
  763. // 'user_id'=>$user_id,
  764. // 'before_price'=>$now_score,//变化前的积分
  765. // 'price'=>$price,//此次变化的积分
  766. // 'after_price'=>$now_score+$price,//变化后的积分
  767. // 'type'=>$type,// 类型 1自营订单 2 第三方订单 3 签到积分 6 其它渠道的积分
  768. // 'third_type'=>$third_type,//第三方的类型 1 淘宝 2 京东 3 唯品会 4 拼多多 5 苏宁易购
  769. // 'other'=>$order_sn,//商城订单 记录订单号 签到记录日期 其它渠道记录获取方式
  770. // 'ctime'=>time(),//创建时间
  771. // 'thaw_time'=>$bill_end_time,//结算的时间
  772. // ];
  773. // $billData=[
  774. // 'uid'=>$user_id,
  775. // 'link_id'=>1,
  776. // 'pm'=>1,
  777. // 'title'=>'平台奖励',
  778. // 'category'=>'now_money',
  779. // 'type'=>'integral',
  780. // 'number'=>$price,
  781. // 'balance'=>$now_score_no,
  782. // 'mark'=>'购买商品获得积分 比例 1:1',
  783. // 'create_time'=>date("Y-m-d H:i:s",time()),
  784. // 'status'=>0,
  785. // 'order_sn'=>$order_sn,
  786. // 'type_shop'=>$type_shop,
  787. // 'commission_type'=>8,
  788. // 'order_type'=>2
  789. // ];
  790. // Db::name('integral_log')->insert($insertDta);
  791. // Db::name('user_bill')->insert($billData);
  792. } catch (Exception $e) {
  793. return app("json")->fail($e->getMessage());
  794. }
  795. }
  796. public static function yuguZhitui($commission, $user_id, $type_shop, $order_sn, $order_id)
  797. {
  798. //查询直推人身份
  799. $user_info = Db::name('user')->where('uid', $user_id)->find();
  800. if (empty($user_info)) {
  801. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, 三方订单直推奖, 用户信息不存在"]);
  802. return false;
  803. }
  804. $spread_uid = $user_info['spread_uid'];
  805. $spread_info = Db::name('user')->where('uid', $spread_uid)->find();
  806. if (empty($user_info)) {
  807. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, 三方订单直推奖, 不存在推荐人"]);
  808. return false;
  809. }
  810. $user_bill = Db::name('user_bill')->where('uid', $spread_uid)->where('order_sn', $order_sn)->where('type_shop', $type_shop)->find();
  811. if (!empty($user_bill)) {
  812. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, 订单已分润"]);
  813. return false;
  814. }
  815. $base_commission = 0;
  816. if ($spread_info['user_group'] == 2) {
  817. $base_commission = 20;
  818. }
  819. if ($spread_info['user_group'] >= 3) {
  820. $base_commission = 30;
  821. }
  822. if ($base_commission == 0) {
  823. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_sn, 三方订单直推奖, 收益比例不能为0"]);
  824. return false;
  825. }
  826. $souxufees=0;
  827. $souxufee =floatval(systemConfig('yanglao'));
  828. if ($souxufee){
  829. $souxufees = bcmul($commission, $souxufee * 0.01, 3);
  830. }
  831. $zhitui_money = bcmul($commission-$souxufees,$base_commission/100,3);
  832. if (empty($user_id)) {
  833. return false;
  834. }
  835. Db::name('log')->insert(['data' => "type_shop:{$type_shop}, $order_id, 三方订单直推奖, 收益:{$commission},手续费:{$souxufees},分润比例:{$base_commission},最终收益:{$zhitui_money}"]);
  836. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  837. $mark = $user_info['nickname'] . '购买商品';
  838. $title = "直推奖".date('m.d');
  839. $commission_type = 3;
  840. $StoreOrderRepository->add_bill_gongfu($title, $zhitui_money, $spread_uid, $mark, $commission_type, $order_sn, $order_id, 0, 0, 0, $type_shop);
  841. }
  842. }