From 417d813f30dcb6121a78e69cf72566bbe333fcd7 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Mon, 29 Nov 2021 13:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E4=B8=BA=E5=85=85=E5=80=BC=E8=B0=83?= =?UTF-8?q?=E6=95=B4+=E6=B5=81=E6=B0=B4=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/Recharge.php | 12 +- app/controller/manager/Bill.php | 27 +++-- app/controller/manager/Business.php | 22 ++-- app/controller/manager/Login.php | 7 +- app/model/CouponBill.php | 15 +++ app/repository/BillRepository.php | 53 ++++++-- app/repository/BusinessRepository.php | 12 +- app/repository/RechargeRepository.php | 26 +++- public/static/manager/js/bill.js | 48 +++++++- public/static/manager/js/business/business.js | 16 +-- view/manager/bill/index.html | 113 ++++++++++++++++-- view/manager/business/index.html | 14 +-- 12 files changed, 296 insertions(+), 69 deletions(-) diff --git a/app/controller/api/Recharge.php b/app/controller/api/Recharge.php index 469027d..11fa329 100644 --- a/app/controller/api/Recharge.php +++ b/app/controller/api/Recharge.php @@ -50,11 +50,15 @@ class Recharge extends Base //这里确定支付成功 $total_fee = $res['total_fee'] / 100; //加余额 - $business->save(["balance" => ($business["balance"] + $total_fee)]); + $business->save([ + "balance" => ($business["balance"] + $total_fee), + "total_recharge" => ($business["total_recharge"] + $total_fee) + ]); //修改支付状态 $recharge->save([ "money" => $total_fee, "state" => RechargeModel::state_on, + "open_id" => $res['openid'], "update_time" => date("Y-m-d H:i:s"), "balance" => $business->balance ]); @@ -109,11 +113,15 @@ class Recharge extends Base //这里确定支付成功 $total_fee = $message['total_fee'] / 100; //加余额 - $business->save(["balance" => ($business["balance"] + $total_fee)]); + $business->inc()->save([ + "balance" => ($business["balance"] + $total_fee), + "total_recharge" => ($business["total_recharge"] + $total_fee)] + ); //修改支付状态 $recharge->save([ "money" => $total_fee, "state" => RechargeModel::state_on, + "open_id" => $message['openid'], "update_time" => date("Y-m-d H:i:s"), "balance" => $business->balance ]); diff --git a/app/controller/manager/Bill.php b/app/controller/manager/Bill.php index 8de5928..11dd844 100644 --- a/app/controller/manager/Bill.php +++ b/app/controller/manager/Bill.php @@ -5,6 +5,7 @@ namespace app\controller\manager; /*流水查看*/ +use app\model\CouponBill; use app\repository\BillRepository; class Bill extends Base @@ -17,20 +18,28 @@ class Bill extends Base */ public function index() { - + $startTime = input("start_time/s", date("Y-m-d H:i:s", strtotime(date("Y-m-d") . " -1 month"))); + $endTime = input("end_time/s", date("Y-m-d 00:00:00")); if ($this->request->isPost()) { $repo = BillRepository::getInstance(); - $keyword = $this->request->param('keyword/s', ''); - $page = $this->request->param('page/d', 1); - $size = $this->request->param('size/d', 30); - $orders = ['sort'=>'asc']; - - - $list = $repo->billList($keyword, $page, $size, $orders); - + $keyword = $this->request->param('keyword/s', ''); + $page = $this->request->param('page/d', 1); + $size = $this->request->param('size/d', 30); + $orders = ['id' => 'desc']; + $list = $repo->billList($page, $size, $keyword, $startTime, $endTime, $orders); + $list['agency_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::agency_money, $startTime, $endTime); + $list['admin_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::admin_money, $startTime, $endTime); + $list['consumer_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::consumer_money, $startTime, $endTime); return $this->json(0, 'success', $list); } + $this->data["agency_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::agency_money); + $this->data["admin_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::admin_money); + $this->data["consumer_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::consumer_money); + + $this->data["startTime"] = $startTime; + $this->data["endTime"] = $endTime; + return $this->view(); } diff --git a/app/controller/manager/Business.php b/app/controller/manager/Business.php index c808d54..a21b027 100644 --- a/app/controller/manager/Business.php +++ b/app/controller/manager/Business.php @@ -37,10 +37,10 @@ class Business extends Base $page = $this->request->param('page/d', 1); $size = $this->request->param('size/d', 30); - $whereMap = [["business.state", "=", BusinessModel::state_on]]; - $orders = ['business.id' => 'desc']; + $whereMap = [["c.state", "=", BusinessModel::state_on]]; + $orders = ['a.id' => 'desc']; if (!empty($keyword)) { - $whereMap[] = ['account.nick_name|business.business_name', 'like', "%$keyword%"]; + $whereMap[] = ['b.nick_name|c.business_name', 'like', "%$keyword%"]; } $list = $repo->businessList($whereMap, $page, $size, $orders); @@ -48,10 +48,10 @@ class Business extends Base //得到当前商家的所有优惠券 $date = date("Y-m-d H:i:s"); //优惠券总数 - $item->coupon_total_count = CouponMain::where(["business_code" => $item->business->code])->sum("count"); + $item->coupon_total_count = CouponMain::where(["business_code" => $item->business_code])->sum("count"); //进行中优惠券总数 - $item->coupon_doing_count = CouponMain::where(["business_code" => $item->business->code]) + $item->coupon_doing_count = CouponMain::where(["business_code" => $item->business_code]) ->whereTime("start_time", "<", $date) ->whereTime("end_time", ">", $date) ->where("status", CouponMain::status_on) @@ -59,21 +59,21 @@ class Business extends Base ->sum("count"); //进行中优惠券总数 - $item->coupon_receive_count = CouponMain::where(["business_code" => $item->business->code])->sum("received_count"); + $item->coupon_receive_count = CouponMain::where(["business_code" => $item->business_code])->sum("received_count"); //过期未使用优惠券总数 - $item->coupon_be_overdue_count = Coupon::where(["business_code" => $item->business->code]) + $item->coupon_be_overdue_count = Coupon::where(["business_code" => $item->business_code]) ->where("is_verificated", "=", Coupon::is_verificated_off) ->whereTime("end_time", "<", $date) ->count(); //已使用优惠券总数 - $item->coupon_used_count = Coupon::where(["business_code" => $item->business->code]) + $item->coupon_used_count = Coupon::where(["business_code" => $item->business_code]) ->where("is_verificated", "=", Coupon::is_verificated_on) ->count(); //商家充值总额 - $item->recharge_total_money = $item->business->total_recharge; + $item->recharge_total_money = $item->total_recharge; }); return $this->json(0, 'success', $list); @@ -320,7 +320,7 @@ class Business extends Base Db::startTrans(); try { //创建充值订单 - if (!$order = RechargeRepository::getInstance()->createOrder($businessCode, $money)) { + if (!$order = RechargeRepository::getInstance()->adminCreateOrder($businessCode, $money)) { throw new RepositoryException('订单创建失败,请稍后重试'); } //生成支付 @@ -344,7 +344,7 @@ class Business extends Base return $this->json("5001", $e->getMessage()); } catch (Exception $e) { Db::rollback(); - return $this->json("5001", $e->getMessage()); + return $this->json("5002", $e->getMessage()); } } diff --git a/app/controller/manager/Login.php b/app/controller/manager/Login.php index 4d080c5..7525433 100644 --- a/app/controller/manager/Login.php +++ b/app/controller/manager/Login.php @@ -43,9 +43,10 @@ class Login extends BaseController } $userInfo = [ - 'user_id' => $member['id'], - 'username' => $member['username'], - 'nickname' => $member['nickname'], + 'user_id' => $member['id'], + 'username' => $member['username'], + 'nickname' => $member['nickname'], + 'business_code' => $member['business_code'], ]; $jwtToken = Jwt::generate($userInfo, env('app.expire', 7200)); diff --git a/app/model/CouponBill.php b/app/model/CouponBill.php index 4be805b..660514b 100644 --- a/app/model/CouponBill.php +++ b/app/model/CouponBill.php @@ -14,5 +14,20 @@ use think\db\exception\ModelNotFoundException; */ class CouponBill extends Base { + public const agency_money = "agency_money"; + public const admin_money = "admin_money"; + public const consumer_money = "consumer_money"; + public function couponMain() + { + return $this->hasOne(CouponMain::class, 'id',"coupon_main_id"); + } + public function business() + { + return $this->hasOne(Business::class, 'code',"business_code"); + } + public function account() + { + return $this->hasOne(Account::class, 'user_code',"user_code"); + } } \ No newline at end of file diff --git a/app/repository/BillRepository.php b/app/repository/BillRepository.php index 4bbad0a..c319c98 100644 --- a/app/repository/BillRepository.php +++ b/app/repository/BillRepository.php @@ -25,12 +25,18 @@ class BillRepository extends Repository { /** * 流水列表 - * @param $keyword * @param $page * @param $size - * @param $orders + * @param $keyword + * @param null $startTime + * @param null $endTime + * @param array $orders + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ - public function billList($keyword, $page, $size, $orders = ["a.id" => "desc"]) + public function billList($page, $size, $keyword = null, $startTime = null, $endTime = null, $orders = ["id" => "desc"]) { $failData = [ 'total' => 0, @@ -38,14 +44,28 @@ class BillRepository extends Repository 'size' => $size, 'list' => new Collection(), ]; - $rep = CouponBill::alias("a") - ->join("business b", "a.business_code = b.code") - ->wher(!empty($keyword), function ($q) use ($keyword) { - $q->where("b.business_name", "like", "%$keyword%"); + $rep = CouponBill::with([ + "business" => function ($query) use ($keyword) { + $query->field(["code", "business_name"])->when(empty($keyword), function ($q) use ($keyword) { + $q->where("business_name", "like", "%$keyword%"); + }); + }, + "account" => function ($q) { + $q->field(["user_code", "nick_name"]); + }, + "couponMain" => function ($q) { + $q->field(["id", "name"]); + }, + ]) + ->when(!empty($startTime), function ($q) use ($startTime) { + $q->whereTime("create_time", ">=", $startTime); + }) + ->when(!empty($endTime), function ($q) use ($endTime) { + $q->whereTime("create_time", "<=", $endTime); }); $failData ['total'] = $rep->count(); - $failData ['list'] = $rep->field("a.*,b.business_name") + $failData ['list'] = $rep ->page($page, $size) ->order($orders) ->select(); @@ -53,4 +73,21 @@ class BillRepository extends Repository } + /** + * 总收益 + * @param $field + * @param null $start_time + * @param null $end_time + * @return float + */ + public function getAgencyMoneySum($field, $start_time = null, $end_time = null) + { + return CouponBill::when(!empty($start_time), function ($q) use ($start_time) { + $q->whereTime("create_time", ">=", $start_time); + })->when(!empty($end_time), function ($q) use ($end_time) { + $q->whereTime("create_time", "<=", $end_time); + }) + ->sum($field); + } + } \ No newline at end of file diff --git a/app/repository/BusinessRepository.php b/app/repository/BusinessRepository.php index 93674c7..33b94e0 100644 --- a/app/repository/BusinessRepository.php +++ b/app/repository/BusinessRepository.php @@ -33,7 +33,10 @@ class BusinessRepository extends Repository */ public function businessList(array $where = [], int $page = 1, int $limit = 0, array $order = []) { - $q = $this->model->alias("a")->field("id")->withJoin(["account" => ["avatar_url", "nick_name"], "business"]); + $q = $this->model->alias("a") + ->join("account b","a.user_code = b.user_code") + ->join("business c","a.business_code = c.code") + ->field(["c.code as business_code","b.avatar_url", "b.nick_name as account_nick_name","c.business_name","c.total_recharge","c.id as basiness_id","c.balance"]); $data = [ 'total' => 0, 'current' => $page, @@ -113,7 +116,12 @@ class BusinessRepository extends Repository /* 获取所有的商家*/ public function getBusinessAll() { - return Business:: where("state", Business::state_on)->select(); + return BusinessFlow::alias("a") + ->join("account b","a.user_code = b.user_code") + ->join("business c","a.business_code = c.code") + ->field("c.code , c.business_name ") + ->order("a.id desc") + ->select(); } /** diff --git a/app/repository/RechargeRepository.php b/app/repository/RechargeRepository.php index daac0b4..60ad429 100644 --- a/app/repository/RechargeRepository.php +++ b/app/repository/RechargeRepository.php @@ -19,9 +19,32 @@ use think\Model; */ class RechargeRepository extends Repository { + /** + * 后台创建一个支付订单 + * @param $businessCode + * @param $money + * @return Recharge|Model + */ + public function adminCreateOrder($businessCode, $money) + { + + $data = [ + "order_num" => createUuid(), + "agency_code" => session('auth')['business_code']??'', + "business_code" => $businessCode, + "money" => $money, + "state" => Recharge::state_off, + "create_time" => date("Y-m-d H:i:s"), + "balance" => 0, + ]; + return Recharge::create($data); + } /** * 创建一个支付订单 - * */ + * @param $businessCode + * @param $money + * @return Recharge|Model + */ public function createOrder($businessCode, $money) { $data = [ @@ -34,5 +57,4 @@ class RechargeRepository extends Repository ]; return Recharge::create($data); } - } \ No newline at end of file diff --git a/public/static/manager/js/bill.js b/public/static/manager/js/bill.js index 09e7c66..4104672 100644 --- a/public/static/manager/js/bill.js +++ b/public/static/manager/js/bill.js @@ -1,10 +1,11 @@ -layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function () { +layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect',"laydate"], function () { let $ = layui.jquery, form = layui.form, table = layui.table, layer = layui.layer, xmSelect = layui.xmSelect, - miniTab = layui.miniTab; + miniTab = layui.miniTab, + laydate = layui.laydate ; /**** index begin ***/ //index页面 @@ -30,6 +31,10 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function limitName: 'size', }, parseData: function (res) { + //console.log(res) + $("#new_agency_money_sum").text(res.data.agency_money_sum?res.data.agency_money_sum:0) + $("#new_admin_money_sum").text(res.data.admin_money_sum?res.data.admin_money_sum:0) + $("#new_consumer_money_sum").text(res.data.consumer_money_sum?res.data.consumer_money_sum:0) return { "code": res.code, //解析接口状态 "msg": res.msg, //解析提示文本 @@ -40,11 +45,30 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function page: true, cols: [[ {type: 'checkbox'}, - {field: 'id', width: 80, title: 'ID'}, - {field: 'title', minWidth: 200, title: '标题'}, - + {templet:function(d){ + if( d.account != undefined &&d.account){ + return d.account.nick_name + } + return ''; + }, title: '用户昵称'}, + {templet:function(d){ + if( d.business != undefined &&d.business){ + return d.business.business_name + } + return ''; + }, title: '商家名称'}, + {templet:function(d){ + if( d.couponMain != undefined &&d.couponMain){ + return d.couponMain.name + } + return ''; + }, title: '优惠券名称'}, + {templet:"#row-commission", title: '持有比例(渠道商:平台:消费者)'}, + {field: 'money', minWidth: 200, title: '金额'}, + {field: 'create_time', minWidth: 200, title: '时间'}, ]], - done: function () { + done: function (res, curr, count) { + Tools.setInsTb(insTb); } }); @@ -60,6 +84,18 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function return false; }); + //日期时间选择器 + laydate.render({ + elem: '#start_time' + ,type: 'datetime' + }); + + //日期时间选择器 + laydate.render({ + elem: '#end_time' + ,type: 'datetime' + }); + diff --git a/public/static/manager/js/business/business.js b/public/static/manager/js/business/business.js index e4dce02..5791c7b 100644 --- a/public/static/manager/js/business/business.js +++ b/public/static/manager/js/business/business.js @@ -40,25 +40,17 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function page: true, cols: [[ // {type: 'checkbox'}, - {templet:function(d){ - return d.business.id - }, width: 80, title: 'ID'}, + {field:"basiness_id", width: 80, title: 'ID'}, {templet: '#row-cover', title: '头像', style: 'height: 90px;'}, - {templet:function(d){ - return d.account.nick_name - }, title: '昵称'}, - {templet:function(d){ - return d.business.business_name - } ,title: '商家名称'}, + {field:"account_nick_name", title: '昵称'}, + {field: "business_name" ,title: '商家名称'}, {field: 'coupon_total_count', title: '优惠券总数量'}, {field: 'coupon_doing_count', title: '进行中优惠券数量'}, {field: 'coupon_receive_count', title: '已领取优惠券数量'}, {field: 'coupon_be_overdue_count', title: '过期未使用优惠券数量'}, {field: 'coupon_used_count', title: '已使用优惠数量'}, - {templet:function(d){ - return d.business.balance - },field: 'balance',title: '商家余额'}, + {field: 'balance',title: '商家余额'}, {field: 'recharge_total_money', title: '充值总额'}, {templet: '#row-operate', minWidth: 350, field: 'right', align: 'center', title: '操作', fixed: 'right'} ]], diff --git a/view/manager/bill/index.html b/view/manager/bill/index.html index c0c8fab..19465fc 100644 --- a/view/manager/bill/index.html +++ b/view/manager/bill/index.html @@ -15,14 +15,99 @@ vertical-align: middle; border: 0; } + .l_font{ + font-size: 26px;height: 25px + }