UserBillRepository.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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\model\user\UserBill;
  15. use app\common\repositories\BaseRepository;
  16. use app\common\repositories\store\order\StoreOrderRepository;
  17. use app\entity\data\user\UserBillEntity;
  18. use app\utils\ArrayUtil;
  19. use think\db\exception\DbException;
  20. use think\exception\ValidateException;
  21. use think\facade\Cache;
  22. use think\facade\Db;
  23. use think\Model;
  24. /**
  25. * Class UserBillRepository
  26. * @package app\common\repositories\user
  27. * @author xaboy
  28. * @day 2020-05-07
  29. * @mixin UserBillDao
  30. */
  31. class UserBillRepository extends BaseRepository
  32. {
  33. /**
  34. * @var UserBillDao
  35. */
  36. protected $dao;
  37. /**
  38. * UserBillRepository constructor.
  39. * @param UserBillDao $dao
  40. */
  41. public function __construct(UserBillDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. public function userList($where, $uid, $page, $limit)
  46. {
  47. $where['uid'] = $uid;
  48. $query = $this->dao->search($where)->order('create_time DESC')
  49. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  50. $query->whereLike('status', $where['status']);
  51. });
  52. $count = $query->count();
  53. $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status,order_sn,mer_id')->page($page, $limit)->select();
  54. return compact('count', 'list');
  55. }
  56. public function getList($where, $page, $limit)
  57. {
  58. if ($where["city"]){
  59. if ($where["city"] == '直辖区') {
  60. $city_id = Db::name("user_city")->where("city_name", $where["province"])->value("city_id");
  61. } else {
  62. $city_id = Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
  63. }
  64. $where['city_id'] = $city_id;
  65. unset($where["city"]);
  66. }
  67. $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
  68. $count = $query->count();
  69. $list = $query->page($page, $limit)->select();
  70. $order_type = 1;
  71. // dump($list);
  72. // return 1;
  73. $souxufee = systemConfig('yanglao');
  74. foreach ($list as &$v){
  75. if ($v["source"]==1) {
  76. $vorder_merchant=Db::name("order_merchant")->where(["out_trade_no"=>$v["order_sn"]])->find();
  77. $v["mer_id"]=$vorder_merchant["mer_id"];
  78. //消费金额
  79. $v["total_price"]=$vorder_merchant["money"];
  80. //可分佣金总额
  81. $v["extension_one"]=$vorder_merchant["available"];
  82. //分账给服务商的钱
  83. $v["service_money"]=$vorder_merchant["service_money"];
  84. //养老金
  85. $v["pension"]=$vorder_merchant["pension"];
  86. //刷卡手续费
  87. $v["swipe_fee"]=$vorder_merchant["swipe_fee"];
  88. $v["uid"]=$vorder_merchant["uid"];
  89. if ($vorder_merchant["uid"] > 0) {
  90. $users=Db::name("user")->where("uid",$vorder_merchant["uid"])->field("nickname,phone")->find();
  91. $v["nickname"]=$users["nickname"];
  92. $v["phone"]=$users["phone"];
  93. } else {
  94. $v["nickname"]=$vorder_merchant["nickname"]."【".($vorder_merchant["trade_type"]=="WX"?"微信用户":"支付宝用户")."】";
  95. $v["phone"]=$vorder_merchant["pay_user_id"];
  96. }
  97. } else {
  98. if(strpos($v['order_sn'],'channel') !== false || strpos($v['order_sn'],'sIC') !== false ){
  99. //渠道商
  100. $channel_order = Db::name('channel_order') -> where('order_no',$v['order_sn']) -> find();
  101. $v["mer_id"]='';
  102. $v["total_price"]=$channel_order["pay_price"];
  103. $v["extension_one"]=0;
  104. $users=Db::name("user")->where("uid",$channel_order["uid"])->field("nickname,phone")->find();
  105. $v["nickname"]=$users["nickname"];
  106. $v["phone"]=$users["phone"];
  107. $v["uid"]=$channel_order["uid"];
  108. $v['mer_name'] = '开通渠道商';
  109. $order_type = 2;
  110. }elseif (strpos($v['order_sn'],'annual') !== false || strpos($v['order_sn'],'sIM') !== false ){
  111. //商户
  112. $annual_cost_order=Db::name('merchant_annual_cost_order')-> where('order_no',$v['order_sn'])->find();
  113. $v["mer_id"]='';
  114. $v["total_price"]=$annual_cost_order["pay_price"];
  115. $v["extension_one"]=0;
  116. $users=Db::name("user")->where("uid",$annual_cost_order["uid"])->field("nickname,phone")->find();
  117. $v["nickname"]=$users["nickname"];
  118. $v["phone"]=$users["phone"];
  119. $v["uid"]=$annual_cost_order["uid"];
  120. $v['mer_name'] = '开通商户';
  121. $order_type = 2;
  122. }else{
  123. if(strpos($v['order_sn'],'sIO') !== false){
  124. $v["mer_id"]='';
  125. $v["total_price"]=strpos($v['mark'],'金卡') !== false ? '999' : '399';
  126. $order_type = 2;
  127. }else{
  128. $store_order=Db::name("store_order")->where("order_sn",$v["order_sn"])->find();
  129. $v["mer_id"]=$store_order["mer_id"];
  130. $v["total_price"]=$store_order["total_price"];
  131. $v["extension_one"]=$store_order["extension_one"];
  132. $users=Db::name("user")->where("uid",$store_order["uid"])->field("nickname,phone")->find();
  133. $v["nickname"]=$users["nickname"];
  134. $v["phone"]=$users["phone"];
  135. $v["uid"]=$store_order["uid"];
  136. }
  137. }
  138. $v["pension"]=Db::name('user_bill')
  139. ->where('commission_type',5)
  140. ->where("order_sn",$v["order_sn"])
  141. ->value('number');
  142. $v["swipe_fee"]=Db::name('admin_bill')
  143. ->where('type',1)
  144. ->where('status',1)
  145. ->where("order_sn",$v["order_sn"])
  146. ->value('number');
  147. //服务商分账的钱
  148. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  149. // $service_money=bcmul($v["total_price"],0.05,2);
  150. $v["service_money"]=$service_money;
  151. }
  152. $user_bill=Db::name("user_bill")
  153. ->alias('a')
  154. ->join('User b', 'a.uid = b.uid')
  155. ->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')
  156. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  157. ->select();
  158. $admin_bill=Db::name("admin_bill")->where("order_sn",$v["order_sn"])->select();
  159. foreach ($admin_bill as $q){
  160. $q["nickname"]="平台";
  161. $q["phone"]="---------";
  162. $user_bill[]=$q;
  163. }
  164. if ($v["uid"]==0){
  165. $user_bill[]=[
  166. "nickname"=>"注:该用户未绑定app,未给到养老金",
  167. "mark"=>$v["pension"]
  168. ];
  169. }
  170. $v["datalist"]=$user_bill;
  171. $mer =Db::name("merchant")->where("mer_id",$v["mer_id"])->find();
  172. $v["mer_name"]=$mer["mer_name"];
  173. if($order_type == 1){
  174. $mer_mory=bcsub(bcsub($v["total_price"],$v["extension_one"],2),$v["swipe_fee"],2);
  175. $user_bill[]=[
  176. "identity"=>4,
  177. "nickname"=>$mer["mer_name"],
  178. "phone"=>$mer["mer_phone"],
  179. "number"=>$mer_mory,
  180. "mark"=>"用户到店支付成功消费".$v["total_price"]."元,商家货款".$mer_mory
  181. ];
  182. }
  183. if($order_type == 1){
  184. $countcommission=Db::name("user_bill")
  185. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  186. ->where("commission_type","<>",5)
  187. ->sum("number");
  188. $jieyu=bcsub($v["service_money"],$countcommission,2);
  189. $user_bill[]=[
  190. "nickname"=>"平台",
  191. "phone"=>"----------",
  192. "number"=>$jieyu,
  193. "mark"=>"分账给服务商的钱".$v["service_money"]."元,结余".$jieyu
  194. ];
  195. $v['pingtai'] = $jieyu;
  196. }else{
  197. $countcommission=Db::name("user_bill")
  198. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  199. ->sum("number");
  200. $v["extension_one"] = $countcommission;
  201. $jieyu=bcsub($v["total_price"],$countcommission,2);
  202. $user_bill[]=[
  203. "nickname"=>"平台",
  204. "phone"=>"----------",
  205. "number"=>$jieyu,
  206. "mark"=>"总佣金+养老金:".$countcommission."元,结余".$jieyu
  207. ];
  208. $v['pingtai'] = $jieyu;
  209. //服务商分账的钱
  210. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  211. $v["service_money"]=$service_money;
  212. }
  213. }
  214. unset($v);
  215. $c = UserBill::getDB() -> count();
  216. $userBill = json_decode(Cache::get('userBill'),true);
  217. // dump($userBill);
  218. if(isset($userBill['count']) && $userBill['count'] == $c){
  219. $total1_query = [];
  220. $cardLists = $userBill['query'];
  221. }else{
  222. $total1_query = UserBill::getDB()
  223. ->alias('a')
  224. ->join('order_merchant b', 'a.order_sn = b.out_trade_no')
  225. ->join('merchant_intention c', 'a.mer_id = c.mer_id')
  226. ->field("b.money,b.available,b.service_money,b.pension,b.swipe_fee")
  227. ->where('a.source', 1)
  228. ->where("a.status",1)
  229. ->where("a.type_shop",0)
  230. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  231. $query->where('a.type', $where['type']);
  232. })
  233. ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
  234. getModelTime($query, $where['user_time'], 'a.create_time');
  235. })
  236. ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
  237. $query->where('c.city_id', $where['city_id']);
  238. })
  239. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  240. $query->whereLike('c.mer_name', "%{$where['keyword']}%");
  241. })
  242. ->group("a.order_sn")
  243. ->cursor();
  244. $total1_price = 0;
  245. $total1_extension_one = 0;
  246. $total1_service_money = 0;
  247. $total1_pension = 0;
  248. $total1_swipe_fee = 0;
  249. foreach ($total1_query as $total1) {
  250. $total1_price += $total1['money'];
  251. $total1_extension_one += $total1['available'];
  252. $total1_service_money += $total1['service_money'];
  253. $total1_pension += $total1['pension'];
  254. $total1_swipe_fee += $total1['swipe_fee'];
  255. }
  256. $cardLists=[
  257. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_price, 0, 2), 'field'=>'元', 'name'=> '消费总额' ],
  258. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_extension_one, 0, 2), 'field'=>'元', 'name'=> '可分佣总额' ],
  259. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_service_money, 0, 2), 'field'=>'元', 'name'=> '分给服务商的金额' ],
  260. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_pension, 0, 2), 'field'=>'元', 'name'=> '养老金' ],
  261. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_swipe_fee, 0, 2), 'field'=>'元', 'name'=> '刷卡手续费' ],
  262. ];
  263. $userBill1['count'] = $c;
  264. $userBill1['query'] = $cardLists;
  265. Cache::set('userBill',json_encode($userBill1));
  266. }
  267. return compact('count', 'list', 'cardLists', 'total1_query');
  268. }
  269. /*平台收益记录*/
  270. public function getAdminList($where, $page, $limit)
  271. {
  272. $query = Db::name("admin_bill")->alias('a')->join('User b', 'a.link_id = b.uid')
  273. ->field('a.*,b.nickname,b.account')
  274. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  275. $query->where('a.type', $where['type']);
  276. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  277. getModelTime($query, $where['date'], 'a.create_time');
  278. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  279. $query->whereLike('a.order_sn|b.nickname|b.account', "%{$where['keyword']}%");
  280. });
  281. $query = $query->order('a.create_time DESC');
  282. $count = $query->count();
  283. $list = $query->page($page, $limit)->select();
  284. $cardLists=[];
  285. // $cardLists=[
  286. // ['className'=> 'el-icon-s-goods', 'count'=>$count1, 'field'=>'个', 'name'=> '平台收入' ],
  287. // ['className'=> 'el-icon-s-goods', 'count'=>$count2, 'field'=>'个', 'name'=> '审核中' ],
  288. // ['className'=> 'el-icon-s-goods', 'count'=>$count3, 'field'=>'个', 'name'=> '未通过' ],
  289. // ];
  290. return compact('count', 'list','cardLists');
  291. }
  292. /**
  293. * @param int $uid
  294. * @param string $category
  295. * @param string $type
  296. * @param int $pm
  297. * @param array $data
  298. * @return BaseDao|Model
  299. * @author xaboy
  300. * @day 2020-05-07
  301. */
  302. public function bill(int $uid, string $category, string $type, int $pm, array $data)
  303. {
  304. $data['category'] = $category;
  305. $data['type'] = $type;
  306. $data['uid'] = $uid;
  307. $data['pm'] = $pm;
  308. $bill = $this->dao->create($data);
  309. // if($category == 'now_money'){
  310. // $tempData = ['tempCode' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id];
  311. // Queue::push(SendTemplateMessageJob::class,$tempData);
  312. // }
  313. return $bill;
  314. }
  315. /**
  316. * @param int $uid
  317. * @param string $category
  318. * @param string $type
  319. * @param array $data
  320. * @return BaseDao|Model
  321. * @author xaboy
  322. * @day 2020-05-07
  323. */
  324. public function incBill(int $uid, string $category, string $type, array $data)
  325. {
  326. return $this->bill($uid, $category, $type, 1, $data);
  327. }
  328. /**
  329. * @param int $uid
  330. * @param string $category
  331. * @param string $type
  332. * @param array $data
  333. * @return BaseDao|Model
  334. * @author xaboy
  335. * @day 2020-05-07
  336. */
  337. public function decBill(int $uid, string $category, string $type, array $data)
  338. {
  339. return $this->bill($uid, $category, $type, 0, $data);
  340. }
  341. /**
  342. * 添加 bill 数据
  343. * @param int $uid
  344. * @param int $typeShop
  345. * @param int $orderId
  346. * @param int $commissionType
  347. * @param float $number
  348. * @param string $mark
  349. * @return UserBillEntity
  350. */
  351. public function addUserBill(int $uid, int $typeShop, int $orderId, int $commissionType, float $number, string $mark): UserBillEntity
  352. {
  353. $orderSn = '';
  354. $merId = 0;
  355. if ($typeShop == UserBillEnum::TYPE_SHOP['Platform']['code']) {
  356. /** @var StoreOrderRepository $storeOrderRepository */
  357. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  358. $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId($orderId);
  359. if (empty($storeOrderEntity->getOrderId())) {
  360. throw new ValidateException('未查询到订单信息');
  361. }
  362. $orderId = $storeOrderEntity->getOrderId();
  363. $orderSn = $storeOrderEntity->getOrderSn();
  364. $merId = $storeOrderEntity->getMerId();
  365. }
  366. /** @var UserBillEntity $userBillEntity */
  367. $userBillEntity = UserBillEntity::newInstance();
  368. $userBillEntity->setUid($uid)
  369. ->setLinkId($orderId)
  370. ->setPm(UserBillEnum::PM['INCOME']['code'])
  371. ->setTitle(ArrayUtil::getEnumNameByCode($commissionType, UserBillEnum::COMMISSION_TYPE))
  372. ->setCategory('now_money')
  373. ->setType('commission')
  374. ->setNumber($number)
  375. ->setMark($mark)
  376. ->setStatus(UserBillEnum::STATUS['PENDING']['code'])
  377. ->setCommissionType($commissionType)
  378. ->setOrderSn($orderSn)
  379. ->setTypeShop($typeShop)
  380. ->setMerId($merId)
  381. ->setSource(UserBillEnum::SOURCE['ONLINE']['code'])
  382. ->setOrderType(UserBillEnum::ORDER_TYPE['SELF_OPERATED']['code'])
  383. ->setIsRedBrokerage(UserBillEnum::IS_RED_BROKERAGE['NORMAL']['code'])
  384. ->setGzc(UserBillEnum::GZC_STATUS['NOT_ENTERED']['code'])
  385. ->setIsGzc(UserBillEnum::IS_GZC['NOT_STARTED']['code']);
  386. return $this->createByEntity($userBillEntity);
  387. }
  388. /**
  389. * 写入 数据
  390. * @param UserBillEntity $userBillEntity
  391. * @return UserBillEntity
  392. */
  393. public function createByEntity(UserBillEntity $userBillEntity): UserBillEntity
  394. {
  395. $result = $this->dao->create($userBillEntity->toUnderlineArray())->toArray();
  396. /** @var UserBillEntity $entity */
  397. $entity = UserBillEntity::newInstance($result);
  398. return $entity;
  399. }
  400. /**
  401. * @param int $billId
  402. * @return UserBillEntity
  403. */
  404. public function getUserBillEntityByBillId(int $billId): UserBillEntity
  405. {
  406. /** @var UserBillEntity $entity */
  407. $entity = $this->dao->getUserBillEntityByBillId($billId);
  408. return $entity;
  409. }
  410. /**
  411. * 更新记录的状态
  412. * @param int $billId
  413. * @param int $status
  414. * @return int
  415. */
  416. public function updateStatusByBillId(int $billId, int $status): int
  417. {
  418. try {
  419. return $this->dao->update($billId, ['status' => $status]);
  420. } catch (DbException $e) {
  421. throw new ValidateException('更新记录状态失败');
  422. }
  423. }
  424. /**
  425. * 获取Bill实体集合 根据 订单类型 订单Id集合 待结算状态
  426. * @param string $typeShop
  427. * @param array $orderIdList
  428. * @return UserBillEntity[]
  429. */
  430. public function getUserBillEntityListByOrderIdListAndPending(string $typeShop, array $orderIdList): array
  431. {
  432. return $this->dao->getUserBillEntityListByOrderIdListAndPending($typeShop, $orderIdList);
  433. }
  434. }