UserBillRepository.php 28 KB

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