UserBillRepository.php 27 KB

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