UserBillRepository.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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\model\store\order\StoreOrder;
  14. use app\common\model\user\UserBill;
  15. use app\common\repositories\BaseRepository;
  16. use crmeb\jobs\SendTemplateMessageJob;
  17. use think\facade\Cache;
  18. use function Symfony\Component\String\s;
  19. use think\facade\Db;
  20. use think\facade\Queue;
  21. use think\Model;
  22. /**
  23. * Class UserBillRepository
  24. * @package app\common\repositories\user
  25. * @author xaboy
  26. * @day 2020-05-07
  27. * @mixin UserBillDao
  28. */
  29. class UserBillRepository extends BaseRepository
  30. {
  31. /**
  32. * @var UserBillDao
  33. */
  34. protected $dao;
  35. /**
  36. * UserBillRepository constructor.
  37. * @param UserBillDao $dao
  38. */
  39. public function __construct(UserBillDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. public function userList($where, $uid, $page, $limit)
  44. {
  45. $where['uid'] = $uid;
  46. $query = $this->dao->search($where)->order('create_time DESC')
  47. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  48. $query->whereLike('status', $where['status']);
  49. });
  50. $count = $query->count();
  51. $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status,order_sn,mer_id')->page($page, $limit)->select();
  52. return compact('count', 'list');
  53. }
  54. public function getList($where, $page, $limit)
  55. {
  56. if ($where["city"]){
  57. if ($where["city"] == '直辖区') {
  58. $city_id = Db::name("user_city")->where("city_name", $where["province"])->value("city_id");
  59. } else {
  60. $city_id = Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
  61. }
  62. $where['city_id'] = $city_id;
  63. unset($where["city"]);
  64. }
  65. $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
  66. $count = $query->count();
  67. $list = $query->page($page, $limit)->select();
  68. $order_type = 1;
  69. // dump($list);
  70. // return 1;
  71. $souxufee = systemConfig('yanglao');
  72. foreach ($list as &$v){
  73. if ($v["source"]==1) {
  74. $vorder_merchant=Db::name("order_merchant")->where(["out_trade_no"=>$v["order_sn"]])->find();
  75. $v["mer_id"]=$vorder_merchant["mer_id"];
  76. //消费金额
  77. $v["total_price"]=$vorder_merchant["money"];
  78. //可分佣金总额
  79. $v["extension_one"]=$vorder_merchant["available"];
  80. //分账给服务商的钱
  81. $v["service_money"]=$vorder_merchant["service_money"];
  82. //养老金
  83. $v["pension"]=$vorder_merchant["pension"];
  84. //刷卡手续费
  85. $v["swipe_fee"]=$vorder_merchant["swipe_fee"];
  86. $v["uid"]=$vorder_merchant["uid"];
  87. if ($vorder_merchant["uid"] > 0) {
  88. $users=Db::name("user")->where("uid",$vorder_merchant["uid"])->field("nickname,phone")->find();
  89. $v["nickname"]=$users["nickname"];
  90. $v["phone"]=$users["phone"];
  91. } else {
  92. $v["nickname"]=$vorder_merchant["nickname"]."【".($vorder_merchant["trade_type"]=="WX"?"微信用户":"支付宝用户")."】";
  93. $v["phone"]=$vorder_merchant["pay_user_id"];
  94. }
  95. } else {
  96. if(strpos($v['order_sn'],'channel') !== false || strpos($v['order_sn'],'sIC') !== false ){
  97. //渠道商
  98. $channel_order = Db::name('channel_order') -> where('order_no',$v['order_sn']) -> find();
  99. $v["mer_id"]='';
  100. $v["total_price"]=$channel_order["pay_price"];
  101. $v["extension_one"]=0;
  102. $users=Db::name("user")->where("uid",$channel_order["uid"])->field("nickname,phone")->find();
  103. $v["nickname"]=$users["nickname"];
  104. $v["phone"]=$users["phone"];
  105. $v["uid"]=$channel_order["uid"];
  106. $v['mer_name'] = '开通渠道商';
  107. $order_type = 2;
  108. }elseif (strpos($v['order_sn'],'annual') !== false || strpos($v['order_sn'],'sIM') !== false ){
  109. //商户
  110. $annual_cost_order=Db::name('merchant_annual_cost_order')-> where('order_no',$v['order_sn'])->find();
  111. $v["mer_id"]='';
  112. $v["total_price"]=$annual_cost_order["pay_price"];
  113. $v["extension_one"]=0;
  114. $users=Db::name("user")->where("uid",$annual_cost_order["uid"])->field("nickname,phone")->find();
  115. $v["nickname"]=$users["nickname"];
  116. $v["phone"]=$users["phone"];
  117. $v["uid"]=$annual_cost_order["uid"];
  118. $v['mer_name'] = '开通商户';
  119. $order_type = 2;
  120. }else{
  121. if(strpos($v['order_sn'],'sIO') !== false){
  122. $v["mer_id"]='';
  123. $v["total_price"]=strpos($v['mark'],'金卡') !== false ? '999' : '399';
  124. $order_type = 2;
  125. }else{
  126. $store_order=Db::name("store_order")->where("order_sn",$v["order_sn"])->find();
  127. $v["mer_id"]=$store_order["mer_id"];
  128. $v["total_price"]=$store_order["total_price"];
  129. $v["extension_one"]=$store_order["extension_one"];
  130. $users=Db::name("user")->where("uid",$store_order["uid"])->field("nickname,phone")->find();
  131. $v["nickname"]=$users["nickname"];
  132. $v["phone"]=$users["phone"];
  133. $v["uid"]=$store_order["uid"];
  134. }
  135. }
  136. $v["pension"]=Db::name('user_bill')
  137. ->where('commission_type',5)
  138. ->where("order_sn",$v["order_sn"])
  139. ->value('number');
  140. $v["swipe_fee"]=Db::name('admin_bill')
  141. ->where('type',1)
  142. ->where('status',1)
  143. ->where("order_sn",$v["order_sn"])
  144. ->value('number');
  145. //服务商分账的钱
  146. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  147. // $service_money=bcmul($v["total_price"],0.05,2);
  148. $v["service_money"]=$service_money;
  149. }
  150. $user_bill=Db::name("user_bill")
  151. ->alias('a')
  152. ->join('User b', 'a.uid = b.uid')
  153. ->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')
  154. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  155. ->select();
  156. $admin_bill=Db::name("admin_bill")->where("order_sn",$v["order_sn"])->select();
  157. foreach ($admin_bill as $q){
  158. $q["nickname"]="平台";
  159. $q["phone"]="---------";
  160. $user_bill[]=$q;
  161. }
  162. if ($v["uid"]==0){
  163. $user_bill[]=[
  164. "nickname"=>"注:该用户未绑定app,未给到养老金",
  165. "mark"=>$v["pension"]
  166. ];
  167. }
  168. $v["datalist"]=$user_bill;
  169. $mer =Db::name("merchant")->where("mer_id",$v["mer_id"])->find();
  170. $v["mer_name"]=$mer["mer_name"];
  171. if($order_type == 1){
  172. $mer_mory=bcsub(bcsub($v["total_price"],$v["extension_one"],2),$v["swipe_fee"],2);
  173. $user_bill[]=[
  174. "identity"=>4,
  175. "nickname"=>$mer["mer_name"],
  176. "phone"=>$mer["mer_phone"],
  177. "number"=>$mer_mory,
  178. "mark"=>"用户到店支付成功消费".$v["total_price"]."元,商家货款".$mer_mory
  179. ];
  180. }
  181. if($order_type == 1){
  182. $countcommission=Db::name("user_bill")
  183. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  184. ->where("commission_type","<>",5)
  185. ->sum("number");
  186. $jieyu=bcsub($v["service_money"],$countcommission,2);
  187. $user_bill[]=[
  188. "nickname"=>"平台",
  189. "phone"=>"----------",
  190. "number"=>$jieyu,
  191. "mark"=>"分账给服务商的钱".$v["service_money"]."元,结余".$jieyu
  192. ];
  193. $v['pingtai'] = $jieyu;
  194. }else{
  195. $countcommission=Db::name("user_bill")
  196. ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
  197. ->sum("number");
  198. $v["extension_one"] = $countcommission;
  199. $jieyu=bcsub($v["total_price"],$countcommission,2);
  200. $user_bill[]=[
  201. "nickname"=>"平台",
  202. "phone"=>"----------",
  203. "number"=>$jieyu,
  204. "mark"=>"总佣金+养老金:".$countcommission."元,结余".$jieyu
  205. ];
  206. $v['pingtai'] = $jieyu;
  207. //服务商分账的钱
  208. $service_money = bcsub($v["extension_one"], $v["pension"], 2);
  209. $v["service_money"]=$service_money;
  210. }
  211. }
  212. unset($v);
  213. $c = UserBill::getDB() -> count();
  214. $userBill = json_decode(Cache::get('userBill'),true);
  215. // dump($userBill);
  216. if(isset($userBill['count']) && $userBill['count'] == $c){
  217. $total1_query = [];
  218. $cardLists = $userBill['query'];
  219. }else{
  220. $total1_query = UserBill::getDB()
  221. ->alias('a')
  222. ->join('order_merchant b', 'a.order_sn = b.out_trade_no')
  223. ->join('merchant_intention c', 'a.mer_id = c.mer_id')
  224. ->field("b.money,b.available,b.service_money,b.pension,b.swipe_fee")
  225. ->where('a.source', 1)
  226. ->where("a.status",1)
  227. ->where("a.type_shop",0)
  228. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  229. $query->where('a.type', $where['type']);
  230. })
  231. ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
  232. getModelTime($query, $where['user_time'], 'a.create_time');
  233. })
  234. ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
  235. $query->where('c.city_id', $where['city_id']);
  236. })
  237. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  238. $query->whereLike('c.mer_name', "%{$where['keyword']}%");
  239. })
  240. ->group("a.order_sn")
  241. ->cursor();
  242. $total1_price = 0;
  243. $total1_extension_one = 0;
  244. $total1_service_money = 0;
  245. $total1_pension = 0;
  246. $total1_swipe_fee = 0;
  247. foreach ($total1_query as $total1) {
  248. $total1_price += $total1['money'];
  249. $total1_extension_one += $total1['available'];
  250. $total1_service_money += $total1['service_money'];
  251. $total1_pension += $total1['pension'];
  252. $total1_swipe_fee += $total1['swipe_fee'];
  253. }
  254. $cardLists=[
  255. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_price, 0, 2), 'field'=>'元', 'name'=> '消费总额' ],
  256. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_extension_one, 0, 2), 'field'=>'元', 'name'=> '可分佣总额' ],
  257. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_service_money, 0, 2), 'field'=>'元', 'name'=> '分给服务商的金额' ],
  258. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_pension, 0, 2), 'field'=>'元', 'name'=> '养老金' ],
  259. ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_swipe_fee, 0, 2), 'field'=>'元', 'name'=> '刷卡手续费' ],
  260. ];
  261. $userBill1['count'] = $c;
  262. $userBill1['query'] = $cardLists;
  263. Cache::set('userBill',json_encode($userBill1));
  264. }
  265. return compact('count', 'list', 'cardLists', 'total1_query');
  266. }
  267. /*平台收益记录*/
  268. public function getAdminList($where, $page, $limit)
  269. {
  270. $query = Db::name("admin_bill")->alias('a')->join('User b', 'a.link_id = b.uid')
  271. ->field('a.*,b.nickname,b.account')
  272. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  273. $query->where('a.type', $where['type']);
  274. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  275. getModelTime($query, $where['date'], 'a.create_time');
  276. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  277. $query->whereLike('a.order_sn|b.nickname|b.account', "%{$where['keyword']}%");
  278. });
  279. $query = $query->order('a.create_time DESC');
  280. $count = $query->count();
  281. $list = $query->page($page, $limit)->select();
  282. $cardLists=[];
  283. // $cardLists=[
  284. // ['className'=> 'el-icon-s-goods', 'count'=>$count1, 'field'=>'个', 'name'=> '平台收入' ],
  285. // ['className'=> 'el-icon-s-goods', 'count'=>$count2, 'field'=>'个', 'name'=> '审核中' ],
  286. // ['className'=> 'el-icon-s-goods', 'count'=>$count3, 'field'=>'个', 'name'=> '未通过' ],
  287. // ];
  288. return compact('count', 'list','cardLists');
  289. }
  290. /**
  291. * @param int $uid
  292. * @param string $category
  293. * @param string $type
  294. * @param int $pm
  295. * @param array $data
  296. * @return BaseDao|Model
  297. * @author xaboy
  298. * @day 2020-05-07
  299. */
  300. public function bill(int $uid, string $category, string $type, int $pm, array $data)
  301. {
  302. $data['category'] = $category;
  303. $data['type'] = $type;
  304. $data['uid'] = $uid;
  305. $data['pm'] = $pm;
  306. $bill = $this->dao->create($data);
  307. // if($category == 'now_money'){
  308. // $tempData = ['tempCode' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id];
  309. // Queue::push(SendTemplateMessageJob::class,$tempData);
  310. // }
  311. return $bill;
  312. }
  313. /**
  314. * @param int $uid
  315. * @param string $category
  316. * @param string $type
  317. * @param array $data
  318. * @return BaseDao|Model
  319. * @author xaboy
  320. * @day 2020-05-07
  321. */
  322. public function incBill(int $uid, string $category, string $type, array $data)
  323. {
  324. return $this->bill($uid, $category, $type, 1, $data);
  325. }
  326. /**
  327. * @param int $uid
  328. * @param string $category
  329. * @param string $type
  330. * @param array $data
  331. * @return BaseDao|Model
  332. * @author xaboy
  333. * @day 2020-05-07
  334. */
  335. public function decBill(int $uid, string $category, string $type, array $data)
  336. {
  337. return $this->bill($uid, $category, $type, 0, $data);
  338. }
  339. }