UserBillRepository.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-05-07
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\user;
  11. use app\common\dao\BaseDao;
  12. use app\common\dao\shared\SharedProsperityBalanceLogDao;
  13. use app\common\dao\user\UserBillDao;
  14. use app\common\enum\CommonEnum;
  15. use app\common\enum\shared\SharedProsperityBalanceLogEnum;
  16. use app\common\enum\user\UserBillEnum;
  17. use app\common\enum\user\UserCertificationEnum;
  18. use app\common\model\user\UserBill;
  19. use app\common\repositories\BaseRepository;
  20. use app\common\repositories\gzc\GzcLogRepository;
  21. use app\common\repositories\shared\SharedProsperityUserRepository;
  22. use app\common\repositories\store\order\StoreOrderRepository;
  23. use app\entity\data\shared\SharedProsperityBalanceLogEntity;
  24. use app\entity\data\shared\SharedProsperityUserEntity;
  25. use app\entity\data\user\UserBillEntity;
  26. use app\entity\data\user\UserCertificationEntity;
  27. use app\entity\data\user\UserEntity;
  28. use app\utils\ArrayUtil;
  29. use think\db\exception\DbException;
  30. use think\exception\ValidateException;
  31. use think\facade\Cache;
  32. use think\facade\Db;
  33. use think\Model;
  34. /**
  35. * Class UserBillRepository
  36. * @package app\common\repositories\user
  37. * @author xaboy
  38. * @day 2020-05-07
  39. * @mixin UserBillDao
  40. */
  41. class UserBillRepository extends BaseRepository
  42. {
  43. /**
  44. * @var UserBillDao
  45. */
  46. protected $dao;
  47. /**
  48. * UserBillRepository constructor.
  49. * @param UserBillDao $dao
  50. */
  51. public function __construct(UserBillDao $dao)
  52. {
  53. $this->dao = $dao;
  54. }
  55. public function userList($where, $uid, $page, $limit)
  56. {
  57. $where['uid'] = $uid;
  58. $query = $this->dao->search($where)->order('create_time DESC')
  59. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  60. $query->whereLike('status', $where['status']);
  61. });
  62. $count = $query->count();
  63. $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status,order_sn,mer_id')->page($page, $limit)->select();
  64. return compact('count', 'list');
  65. }
  66. public function getList($where, $page, $limit)
  67. {
  68. if ($where["city"]){
  69. if ($where["city"] == '直辖区') {
  70. $city_id = Db::name("user_city")->where("city_name", $where["province"])->value("city_id");
  71. } else {
  72. $city_id = Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
  73. }
  74. $where['city_id'] = $city_id;
  75. unset($where["city"]);
  76. }
  77. $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
  78. $count = $query->count();
  79. $list = $query->page($page, $limit)->select();
  80. $order_type = 1;
  81. // dump($list);
  82. // return 1;
  83. $souxufee = systemConfig('yanglao');
  84. foreach ($list as &$v){
  85. if ($v["source"]==1) {
  86. $vorder_merchant=Db::name("order_merchant")->where(["out_trade_no"=>$v["order_sn"]])->find();
  87. $v["mer_id"]=$vorder_merchant["mer_id"];
  88. //消费金额
  89. $v["total_price"]=$vorder_merchant["money"];
  90. //可分佣金总额
  91. $v["extension_one"]=$vorder_merchant["available"];
  92. //分账给服务商的钱
  93. $v["service_money"]=$vorder_merchant["service_money"];
  94. //养老金
  95. $v["pension"]=$vorder_merchant["pension"];
  96. //刷卡手续费
  97. $v["swipe_fee"]=$vorder_merchant["swipe_fee"];
  98. $v["uid"]=$vorder_merchant["uid"];
  99. if ($vorder_merchant["uid"] > 0) {
  100. $users=Db::name("user")->where("uid",$vorder_merchant["uid"])->field("nickname,phone")->find();
  101. $v["nickname"]=$users["nickname"];
  102. $v["phone"]=$users["phone"];
  103. } else {
  104. $v["nickname"]=$vorder_merchant["nickname"]."【".($vorder_merchant["trade_type"]=="WX"?"微信用户":"支付宝用户")."】";
  105. $v["phone"]=$vorder_merchant["pay_user_id"];
  106. }
  107. } else {
  108. if(strpos($v['order_sn'],'channel') !== false || strpos($v['order_sn'],'sIC') !== false ){
  109. //渠道商
  110. $channel_order = Db::name('channel_order') -> where('order_no',$v['order_sn']) -> find();
  111. $v["mer_id"]='';
  112. $v["total_price"]=$channel_order["pay_price"];
  113. $v["extension_one"]=0;
  114. $users=Db::name("user")->where("uid",$channel_order["uid"])->field("nickname,phone")->find();
  115. $v["nickname"]=$users["nickname"];
  116. $v["phone"]=$users["phone"];
  117. $v["uid"]=$channel_order["uid"];
  118. $v['mer_name'] = '开通渠道商';
  119. $order_type = 2;
  120. }elseif (strpos($v['order_sn'],'annual') !== false || strpos($v['order_sn'],'sIM') !== false ){
  121. //商户
  122. $annual_cost_order=Db::name('merchant_annual_cost_order')-> where('order_no',$v['order_sn'])->find();
  123. $v["mer_id"]='';
  124. $v["total_price"]=$annual_cost_order["pay_price"];
  125. $v["extension_one"]=0;
  126. $users=Db::name("user")->where("uid",$annual_cost_order["uid"])->field("nickname,phone")->find();
  127. $v["nickname"]=$users["nickname"];
  128. $v["phone"]=$users["phone"];
  129. $v["uid"]=$annual_cost_order["uid"];
  130. $v['mer_name'] = '开通商户';
  131. $order_type = 2;
  132. }else{
  133. if(strpos($v['order_sn'],'sIO') !== false){
  134. $v["mer_id"]='';
  135. $v["total_price"]=strpos($v['mark'],'金卡') !== false ? '999' : '399';
  136. $order_type = 2;
  137. }else{
  138. $store_order=Db::name("store_order")->where("order_sn",$v["order_sn"])->find();
  139. $v["mer_id"]=$store_order["mer_id"];
  140. $v["total_price"]=$store_order["total_price"];
  141. $v["extension_one"]=$store_order["extension_one"];
  142. $users=Db::name("user")->where("uid",$store_order["uid"])->field("nickname,phone")->find();
  143. $v["nickname"]=$users["nickname"];
  144. $v["phone"]=$users["phone"];
  145. $v["uid"]=$store_order["uid"];
  146. }
  147. }
  148. $v["pension"]=Db::name('user_bill')
  149. ->where('commission_type',5)
  150. ->where("order_sn",$v["order_sn"])
  151. ->value('number');
  152. $v["swipe_fee"]=Db::name('admin_bill')
  153. ->where('type',1)
  154. ->where('status',1)
  155. ->where("order_sn",$v["order_sn"])
  156. ->value('number');
  157. //服务商分账的钱
  158. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  159. // $service_money=bcmul($v["total_price"],0.05,2);
  160. $v["service_money"]=$service_money;
  161. }
  162. $user_bill=Db::name("user_bill")
  163. ->alias('a')
  164. ->join('User b', 'a.uid = b.uid')
  165. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,b.phone,a.uid,a.order_sn,a.source,identity')
  166. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  167. ->select();
  168. $admin_bill=Db::name("admin_bill")->where("order_sn",$v["order_sn"])->select();
  169. foreach ($admin_bill as $q){
  170. $q["nickname"]="平台";
  171. $q["phone"]="---------";
  172. $user_bill[]=$q;
  173. }
  174. if ($v["uid"]==0){
  175. $user_bill[]=[
  176. "nickname"=>"注:该用户未绑定app,未给到养老金",
  177. "mark"=>$v["pension"]
  178. ];
  179. }
  180. $v["datalist"]=$user_bill;
  181. $mer =Db::name("merchant")->where("mer_id",$v["mer_id"])->find();
  182. $v["mer_name"]=$mer["mer_name"];
  183. if($order_type == 1){
  184. $mer_mory=bcsub(bcsub($v["total_price"],$v["extension_one"],2),$v["swipe_fee"],2);
  185. $user_bill[]=[
  186. "identity"=>4,
  187. "nickname"=>$mer["mer_name"],
  188. "phone"=>$mer["mer_phone"],
  189. "number"=>$mer_mory,
  190. "mark"=>"用户到店支付成功消费".$v["total_price"]."元,商家货款".$mer_mory
  191. ];
  192. }
  193. if($order_type == 1){
  194. $countcommission=Db::name("user_bill")
  195. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  196. ->where("commission_type","<>",5)
  197. ->sum("number");
  198. $jieyu=bcsub($v["service_money"],$countcommission,2);
  199. $user_bill[]=[
  200. "nickname"=>"平台",
  201. "phone"=>"----------",
  202. "number"=>$jieyu,
  203. "mark"=>"分账给服务商的钱".$v["service_money"]."元,结余".$jieyu
  204. ];
  205. $v['pingtai'] = $jieyu;
  206. }else{
  207. $countcommission=Db::name("user_bill")
  208. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  209. ->sum("number");
  210. $v["extension_one"] = $countcommission;
  211. $jieyu=bcsub($v["total_price"],$countcommission,2);
  212. $user_bill[]=[
  213. "nickname"=>"平台",
  214. "phone"=>"----------",
  215. "number"=>$jieyu,
  216. "mark"=>"总佣金+养老金:".$countcommission."元,结余".$jieyu
  217. ];
  218. $v['pingtai'] = $jieyu;
  219. //服务商分账的钱
  220. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  221. $v["service_money"]=$service_money;
  222. }
  223. }
  224. unset($v);
  225. $c = UserBill::getDB() -> count();
  226. $userBill = json_decode(Cache::get('userBill'),true);
  227. // dump($userBill);
  228. if(isset($userBill['count']) && $userBill['count'] == $c){
  229. $total1_query = [];
  230. $cardLists = $userBill['query'];
  231. }else{
  232. $total1_query = UserBill::getDB()
  233. ->alias('a')
  234. ->join('order_merchant b', 'a.order_sn = b.out_trade_no')
  235. ->join('merchant_intention c', 'a.mer_id = c.mer_id')
  236. ->field("b.money,b.available,b.service_money,b.pension,b.swipe_fee")
  237. ->where('a.source', 1)
  238. ->where("a.status",1)
  239. ->where("a.type_shop",0)
  240. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  241. $query->where('a.type', $where['type']);
  242. })
  243. ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
  244. getModelTime($query, $where['user_time'], 'a.create_time');
  245. })
  246. ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
  247. $query->where('c.city_id', $where['city_id']);
  248. })
  249. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  250. $query->whereLike('c.mer_name', "%{$where['keyword']}%");
  251. })
  252. ->group("a.order_sn")
  253. ->cursor();
  254. $total1_price = 0;
  255. $total1_extension_one = 0;
  256. $total1_service_money = 0;
  257. $total1_pension = 0;
  258. $total1_swipe_fee = 0;
  259. foreach ($total1_query as $total1) {
  260. $total1_price += $total1['money'];
  261. $total1_extension_one += $total1['available'];
  262. $total1_service_money += $total1['service_money'];
  263. $total1_pension += $total1['pension'];
  264. $total1_swipe_fee += $total1['swipe_fee'];
  265. }
  266. $cardLists=[
  267. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_price, 0, 2), 'field'=>'元', 'name'=> '消费总额' ],
  268. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_extension_one, 0, 2), 'field'=>'元', 'name'=> '可分佣总额' ],
  269. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_service_money, 0, 2), 'field'=>'元', 'name'=> '分给服务商的金额' ],
  270. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_pension, 0, 2), 'field'=>'元', 'name'=> '养老金' ],
  271. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_swipe_fee, 0, 2), 'field'=>'元', 'name'=> '刷卡手续费' ],
  272. ];
  273. $userBill1['count'] = $c;
  274. $userBill1['query'] = $cardLists;
  275. Cache::set('userBill',json_encode($userBill1));
  276. }
  277. return compact('count', 'list', 'cardLists', 'total1_query');
  278. }
  279. /*平台收益记录*/
  280. public function getAdminList($where, $page, $limit)
  281. {
  282. $query = Db::name("admin_bill")->alias('a')->join('User b', 'a.link_id = b.uid')
  283. ->field('a.*,b.nickname,b.account')
  284. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  285. $query->where('a.type', $where['type']);
  286. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  287. getModelTime($query, $where['date'], 'a.create_time');
  288. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  289. $query->whereLike('a.order_sn|b.nickname|b.account', "%{$where['keyword']}%");
  290. });
  291. $query = $query->order('a.create_time DESC');
  292. $count = $query->count();
  293. $list = $query->page($page, $limit)->select();
  294. $cardLists=[];
  295. // $cardLists=[
  296. // ['className'=> 'el-icon-s-goods', 'count'=>$count1, 'field'=>'个', 'name'=> '平台收入' ],
  297. // ['className'=> 'el-icon-s-goods', 'count'=>$count2, 'field'=>'个', 'name'=> '审核中' ],
  298. // ['className'=> 'el-icon-s-goods', 'count'=>$count3, 'field'=>'个', 'name'=> '未通过' ],
  299. // ];
  300. return compact('count', 'list','cardLists');
  301. }
  302. /**
  303. * @param int $uid
  304. * @param string $category
  305. * @param string $type
  306. * @param int $pm
  307. * @param array $data
  308. * @return BaseDao|Model
  309. * @author xaboy
  310. * @day 2020-05-07
  311. */
  312. public function bill(int $uid, string $category, string $type, int $pm, array $data)
  313. {
  314. $data['category'] = $category;
  315. $data['type'] = $type;
  316. $data['uid'] = $uid;
  317. $data['pm'] = $pm;
  318. $bill = $this->dao->create($data);
  319. // if($category == 'now_money'){
  320. // $tempData = ['tempCode' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id];
  321. // Queue::push(SendTemplateMessageJob::class,$tempData);
  322. // }
  323. return $bill;
  324. }
  325. /**
  326. * @param int $uid
  327. * @param string $category
  328. * @param string $type
  329. * @param array $data
  330. * @return BaseDao|Model
  331. * @author xaboy
  332. * @day 2020-05-07
  333. */
  334. public function incBill(int $uid, string $category, string $type, array $data)
  335. {
  336. return $this->bill($uid, $category, $type, 1, $data);
  337. }
  338. /**
  339. * @param int $uid
  340. * @param string $category
  341. * @param string $type
  342. * @param array $data
  343. * @return BaseDao|Model
  344. * @author xaboy
  345. * @day 2020-05-07
  346. */
  347. public function decBill(int $uid, string $category, string $type, array $data)
  348. {
  349. return $this->bill($uid, $category, $type, 0, $data);
  350. }
  351. /**
  352. * 添加 bill 数据
  353. * @param int $uid
  354. * @param int $commissionType
  355. * @param int $pm
  356. * @param float $number
  357. * @param string $category
  358. * @param string $type
  359. * @param string $source
  360. * @param int $orderType
  361. * @param int $typeShop
  362. * @param int $orderId
  363. * @param string $mark
  364. * @param string $title
  365. * @param string $orderSn
  366. * @return UserBillEntity
  367. */
  368. public function addUserBill(int $uid, int $commissionType, int $pm, float $number, string $category, string $type, string $source, int $orderType, int $typeShop = 0, int $orderId = 0, string $mark = '', string $title = '', string $orderSn = ''): UserBillEntity
  369. {
  370. $merId = 0;
  371. if ($typeShop == UserBillEnum::TYPE_SHOP['Platform']['code']) {
  372. /** @var StoreOrderRepository $storeOrderRepository */
  373. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  374. $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId($orderId);
  375. if (empty($storeOrderEntity->getOrderId())) {
  376. throw new ValidateException('未查询到订单信息');
  377. }
  378. $orderId = $storeOrderEntity->getOrderId();
  379. $orderSn = $storeOrderEntity->getOrderSn();
  380. $merId = $storeOrderEntity->getMerId();
  381. }
  382. /** @var UserBillEntity $userBillEntity */
  383. $userBillEntity = UserBillEntity::newInstance();
  384. $userBillEntity->setUid($uid)
  385. ->setLinkId($orderId)
  386. //UserBillEnum::PM['INCOME']['code']
  387. ->setPm($pm)
  388. ->setTitle($title ?: ArrayUtil::getEnumNameByCode($commissionType, UserBillEnum::COMMISSION_TYPE))
  389. ->setCategory($category)
  390. ->setType($type)
  391. ->setNumber($number)
  392. ->setMark($mark)
  393. ->setStatus(UserBillEnum::STATUS['PENDING']['code'])
  394. ->setCommissionType($commissionType)
  395. ->setOrderSn($orderSn)
  396. ->setTypeShop($typeShop)
  397. ->setMerId($merId)
  398. // UserBillEnum::SOURCE['ONLINE']['code']
  399. ->setSource($source)
  400. ->setOrderType($orderType)
  401. ->setIsRedBrokerage(UserBillEnum::IS_RED_BROKERAGE['NORMAL']['code'])
  402. ->setGzc(UserBillEnum::GZC_STATUS['NOT_ENTERED']['code'])
  403. ->setIsGzc(UserBillEnum::IS_GZC['NOT_STARTED']['code']);
  404. return $this->createByEntity($userBillEntity);
  405. }
  406. /**
  407. * 写入 数据
  408. * @param UserBillEntity $userBillEntity
  409. * @return UserBillEntity
  410. */
  411. public function createByEntity(UserBillEntity $userBillEntity): UserBillEntity
  412. {
  413. $result = $this->dao->create($userBillEntity->toUnderlineArray())->toArray();
  414. /** @var UserBillEntity $entity */
  415. $entity = UserBillEntity::newInstance($result);
  416. return $entity;
  417. }
  418. /**
  419. * @param int $billId
  420. * @return UserBillEntity
  421. */
  422. public function getUserBillEntityByBillId(int $billId): UserBillEntity
  423. {
  424. $entityList = $this->getUserBillEntityListByBillIdList([$billId]);
  425. $entity = array_shift($entityList);
  426. if ($entity instanceof UserBillEntity) {
  427. return $entity;
  428. } else {
  429. /** @var UserBillEntity */
  430. return UserBillEntity::newInstance();
  431. }
  432. }
  433. /**
  434. * 获取Bill实体集合 根据 Id集合
  435. * @param array $billIdList
  436. * @return UserBillEntity[]
  437. */
  438. public function getUserBillEntityListByBillIdList(array $billIdList): array
  439. {
  440. return $this->dao->getUserBillEntityListByBillIdList($billIdList);
  441. }
  442. /**
  443. * 获取Bill实体集合 根据 订单类型 订单Id集合 待结算状态
  444. * @param string $typeShop
  445. * @param array $orderIdList
  446. * @return UserBillEntity[]
  447. */
  448. public function getUserBillEntityListByOrderIdListAndPending(string $typeShop, array $orderIdList): array
  449. {
  450. return $this->dao->getUserBillEntityListByOrderIdListAndPending($typeShop, $orderIdList);
  451. }
  452. /**
  453. * @param string $typeShop
  454. * @param string $orderSn
  455. * @return UserBillEntity
  456. */
  457. public function getUserBillEntityByTypeShopAndOrderSn(string $typeShop, string $orderSn): UserBillEntity
  458. {
  459. return $this->dao->getUserBillEntityByTypeShopAndOrderSn($typeShop, $orderSn);
  460. }
  461. /**
  462. * @param $dataList
  463. * @return array
  464. */
  465. public function batchUpdateUserBillDataByDataList($dataList): array
  466. {
  467. return $this->dao->saveAll($dataList);
  468. }
  469. /**
  470. * 将 账单 记录设置为生效状态 并对用户的 对应 字段进行增减
  471. * @param array $idList
  472. * @return array
  473. */
  474. public function executeByIdList(array $idList): array
  475. {
  476. $userBillEntityList = $this->getUserBillEntityListByBillIdList($idList);
  477. // 忽略 已经生效的 记录
  478. $userBillEntityList = array_filter($userBillEntityList, function($entity) {
  479. return $entity->getStatus() != UserBillEnum::STATUS['VALID']['code'];
  480. });
  481. if (!empty($userBillEntityList)) {
  482. $pensionUidList = [];
  483. // 获取所有记录的 归属人
  484. $userIdList = array_map(function ($userBillEntity) use (&$pensionUidList) {
  485. // 获取 养老金记录
  486. if ($userBillEntity->getCommissionType() == UserBillEnum::COMMISSION_TYPE['PENSION']['code']) {
  487. $pensionUidList[] = $userBillEntity->getUid();
  488. }
  489. return $userBillEntity->getUid();
  490. }, $userBillEntityList);
  491. // 获取用户 映射信息
  492. /** @var UserRepository $userRepository */
  493. $userRepository = app()->make(UserRepository::class);
  494. /** @var UserEntity[] $userEntityMapByUid */
  495. $userEntityMapByUid = [];
  496. if (!empty($userIdList)) {
  497. $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
  498. }
  499. // 获取实名认证 映射信息 (养老金记录 结算时 需要确定 所属人是否已经实名认证 如果实名认证了 需要推到养老金公证处明细表里 并修改养老金记录的推送状态)
  500. /** @var UserCertificationEntity[] $userCertificationEntityMapByUid */
  501. $userCertificationEntityMapByUid = [];
  502. if (!empty($pensionUidList)) {
  503. /** @var UserCertificationRepository $userCertificationRepository */
  504. $userCertificationRepository = app()->make(UserCertificationRepository::class);
  505. $userCertificationEntityMapByUid = $userCertificationRepository->getUserCertificationEntityMapByUidListApproved($pensionUidList);
  506. }
  507. /** @var GzcLogRepository $gzcLogRepository */
  508. $gzcLogRepository = app()->make(GzcLogRepository::class);
  509. $updateUserEntityList = [];
  510. $updateUserBillEntityList = [];
  511. foreach ($userBillEntityList as $userBillEntity) {
  512. if (empty($userBillEntity->getBillId()) || empty($userBillEntity->getUid())) {
  513. // throw new ValidateException('无效的账单记录');
  514. continue;
  515. }
  516. if (!empty($userBillEntity->getTakeTime())) {
  517. throw new ValidateException("该账单记录已更新,请勿重复操作({$userBillEntity->getBillId()})");
  518. }
  519. $userEntity = $userEntityMapByUid[$userBillEntity->getUid()];
  520. if (empty($userEntity) || empty($userEntity->getUid())) {
  521. throw new ValidateException("未查询到用户信息({$userBillEntity->getBillId()})");
  522. }
  523. // 更新 用户信息
  524. /** @var UserEntity $updateUserEntity */
  525. $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
  526. $updateUserEntity->setUid($userEntity->getUid());
  527. // 更新 账单 记录
  528. /** @var UserBillEntity $updateUserBillEntity */
  529. $updateUserBillEntity = $updateUserBillEntityList[$userBillEntity->getBillId()] ?? UserBillEntity::newInstance();
  530. // 更新 账单记录 状态
  531. $updateUserBillEntity
  532. ->setBillId($userBillEntity->getBillId())
  533. ->setStatus(UserBillEnum::STATUS['VALID']['code'])
  534. ->setTakeTime(time());
  535. if (in_array($userBillEntity->getCommissionType(), [
  536. UserBillEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code'],
  537. UserBillEnum::COMMISSION_TYPE['LOCKED_CUSTOMER']['code'],
  538. UserBillEnum::COMMISSION_TYPE['PLATFORM_REWARD']['code'],
  539. UserBillEnum::COMMISSION_TYPE['SHARE_EARNINGS']['code']
  540. ])) {
  541. // 直推奖
  542. if (is_null($updateUserEntity->getBrokeragePrice())) {
  543. $updateUserEntity->setBrokeragePrice($userEntity->getBrokeragePrice());
  544. }
  545. // 更新用户 佣金 信息
  546. if ($userBillEntity->getPm() == UserBillEnum::PM['EXPEND']['code']) {
  547. if ($updateUserEntity->getBrokeragePrice() < $userBillEntity->getNumber()) {
  548. throw new ValidateException('用户佣金不足');
  549. }
  550. $updateUserEntity->setBrokeragePrice(bcsub($updateUserEntity->getBrokeragePrice(), $userBillEntity->getNumber(), 2));
  551. } else {
  552. $updateUserEntity->setBrokeragePrice(bcadd($updateUserEntity->getBrokeragePrice(), $userBillEntity->getNumber(), 2));
  553. }
  554. } else if ($userBillEntity->getCommissionType() == UserBillEnum::COMMISSION_TYPE['PENSION']['code']) {
  555. if ($userBillEntity->getGzc() == UserBillEnum::GZC_STATUS['NOT_ENTERED']['code']) {
  556. if (!empty($userCertificationEntityMapByUid[$userBillEntity->getUid()])) {
  557. $userCertificationEntity = $userCertificationEntityMapByUid[$userBillEntity->getUid()];
  558. // 创建公证处记录
  559. $gzcLogRepository->addGzcLog(
  560. $userCertificationEntity->getUid(),
  561. $userCertificationEntity->getUserName(),
  562. $userCertificationEntity->getMobile(),
  563. $userCertificationEntity->getUserCard(),
  564. $userBillEntity->getTypeShop(),
  565. $userBillEntity->getLinkId(),
  566. $userBillEntity->getNumber()
  567. );
  568. // 养老金
  569. if (is_null($updateUserEntity->getAnnuity())) {
  570. $updateUserEntity->setAnnuity($userEntity->getAnnuity());
  571. }
  572. $updateUserEntity->setAnnuity(bcadd($updateUserEntity->getAnnuity(), $userBillEntity->getNumber(), 2));
  573. // 几笔养老金
  574. if (is_null($updateUserEntity->getAnnuityNum())) {
  575. $updateUserEntity->setAnnuityNum($userEntity->getAnnuityNum());
  576. }
  577. $updateUserEntity->setAnnuityNum(bcadd($updateUserEntity->getAnnuityNum(), 1));
  578. // 更新佣金记录
  579. $updateUserBillEntity
  580. ->setGzc(UserBillEnum::GZC_STATUS['ENTERED']['code'])
  581. ->setIsGzc(UserBillEnum::IS_GZC['NOT_STARTED']['code']);
  582. }
  583. }
  584. }
  585. $updateUserEntityList[$userEntity->getUid()] = $updateUserEntity;
  586. $updateUserBillEntityList[$userBillEntity->getBillId()] = $updateUserBillEntity;
  587. }
  588. if (!empty($updateUserEntityList)) {
  589. $userRepository->batchUpdateUserDataByDataList(
  590. array_map(function (UserEntity $updateUserEntity) {
  591. return $updateUserEntity->toUnderlineArray();
  592. }, $updateUserEntityList)
  593. );
  594. }
  595. if (!empty($updateUserBillEntityList)) {
  596. $this->batchUpdateUserBillDataByDataList(
  597. array_map(function (UserBillEntity $updateUserBillEntity) {
  598. return $updateUserBillEntity->toUnderlineArray();
  599. }, $updateUserBillEntityList)
  600. );
  601. }
  602. }
  603. return $idList;
  604. }
  605. /**
  606. * @param $start
  607. * @param $end
  608. * @return array
  609. * @author 史晨
  610. * @date 2026/2/11 14:29
  611. * 共富感恩奖佣金查询
  612. */
  613. public function getListForSharedProsperityThank($start, $end)
  614. {
  615. $status = UserBillEnum::STATUS['VALID']['code'];
  616. $merId = [
  617. CommonEnum::DESIGN_MERCHANT_ID['WonderfulLiving']['code'],
  618. CommonEnum::DESIGN_MERCHANT_ID['Repurchase']['code'],
  619. CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code'],
  620. ];
  621. $commission_type = [
  622. CommonEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code'],
  623. CommonEnum::COMMISSION_TYPE['SHARE_EARNINGS']['code']
  624. ];
  625. $params = [
  626. 'start' => $start,
  627. 'end' => $end,
  628. 'status' => $status,
  629. 'merId' => $merId,
  630. 'commissionType' => $commission_type
  631. ];
  632. return $this->dao->getListData($params);
  633. }
  634. }