From 18687d8ab188cae30b0c2ecabc5ec0d1ea37b646 Mon Sep 17 00:00:00 2001 From: wanyongkang <937888580@qq.com> Date: Sun, 22 Nov 2020 11:09:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E5=AE=A2=E6=88=B7=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/manager/controller/SellInfo.php | 217 ++++++++++++++++++++++++++++ app/manager/model/ProductOrder.php | 42 +++++- app/manager/model/User.php | 2 +- enum/order/ProductOrder.php | 6 +- 4 files changed, 261 insertions(+), 6 deletions(-) create mode 100644 app/manager/controller/SellInfo.php diff --git a/app/manager/controller/SellInfo.php b/app/manager/controller/SellInfo.php new file mode 100644 index 0000000..81e2623 --- /dev/null +++ b/app/manager/controller/SellInfo.php @@ -0,0 +1,217 @@ +getOne(['id' => $this->userinfo['OperaterID']]); + $where = []; + if (!$manager_info['IsRoot']) { + $where['ManagerId'] = $this->userinfo['OperaterID']; + } + + $user_id_list = []; + $register_id_list = []; + $today_register_id_list = []; + $data = []; + $time_where1 = ''; + $time_where2 = ''; + $time_where_today = ''; + $time_where2_today = ''; + + + $date1 = date("Y-m-01", time()); + $date2 = date("Y-m-d H:i:s", time()); + $flag = date("d", time()) == date("t", time()); + $date3 = date("Y-m-01", strtotime("-1 month")); + $date4 = $flag ? date("Y-m-t H:i:s", strtotime("-1 month")) : date("Y-m-d H:i:s", strtotime("-1 month")); + + if(isset($_GET['sell_month'])){ + $date1 = date('Y-m-01', strtotime($_GET['sell_month'])); + $date2 = date("Y-m-t 23:59:59", strtotime($_GET['sell_month'])); + $date3 = date("Y-m-01", strtotime($_GET['sell_month']." -1 month")); + $date4 = date("Y-m-t 23:59:59", strtotime($_GET['sell_month']." -1 month")); + } + + + //本月 + $time_where1 .= " UpdateTime>='" . $date1 . "' and UpdateTime<='" . $date2 . "' "; + $time_where2 .= " UpdateTime>='" . $date3 . "' and UpdateTime<='" . $date4 . "' "; + $time_where_today .= " UpdateTime>='" . date("Y-m-d") . "' and UpdateTime<='" . $date2 . "' "; + + //获取用户列表 + $user_list = $user_model->getAllManagerUser($where); + foreach ($user_list as $info) { + $user_id_list[] = $info['Id']; + //本月注册用户 + if (strtotime($info['CreateTime']) >= strtotime($date1) && strtotime($info['CreateTime']) <= strtotime($date2)) { + $register_id_list[] = $info['Id']; + } + if (strtotime($info['CreateTime']) >= strtotime(date("Y-m-d")) && strtotime($info['CreateTime']) <= strtotime($date2)) { + $today_register_id_list[] = $info['Id']; + } + } + $user_where = ['UserId' => ['in', $user_id_list]]; + $register_where = ['UserId' => ['in', $register_id_list]]; + //获取本月所有消费额 + $all_cost_data = $product_order_model->getAllMoney($time_where1); + //获取本月该客户经理所有的消费额 + $user_cost_data = $product_order_model->getAllMoney($time_where1, $user_where); + //获取上月该客户经理所有的消费额 + $user_last_month_cost_data = $product_order_model->getAllMoney($time_where2, $user_where); + //获取今天该客户经理所有的消费额 + $user_today_cost_data = $product_order_model->getAllMoney($time_where2_today, ['UserId' => ['in', $today_register_id_list]]); + //获取本月该客户经理所有新注册的消费额 + $register_cost_data = $product_order_model->getAllMoney($time_where1, $register_where); + //获取本月总退款 + $refund_cost_data = $product_order_model->getRefund($time_where1, $user_where); + //总消费营业额 + $all_cost = $all_cost_data['money']??0; + $user_cost = $user_cost_data['money']??0; + $all_cost_num = $all_cost_data['num']??0; + $user_cost_num = $user_cost_data['num']??0; + //上月 + $user_last_month_cost = $user_last_month_cost_data['money']??0; + //注册 + $user_register_count = count($register_id_list); + $user_register_cost_count = $register_cost_data?$register_cost_data['num']:0; + + $sell_percent = ($all_cost?round($user_cost / $all_cost * 100, 2):0); + $cost_percent = ($all_cost_num?round($user_cost_num / $all_cost_num * 100, 2):0); + $cost_register = ($user_register_count?round($user_register_cost_count / $user_register_count * 100, 2):0); + $cost_old_user = (($all_cost_num - $user_register_count)?round(($user_cost_num - $user_register_cost_count) / ($all_cost_num - $user_register_count) * 100, 2):0); + $com_cost = ($user_cost - $user_last_month_cost); + $com_percent = ($user_last_month_cost?round(($user_cost - $user_last_month_cost) / $user_last_month_cost * 100, 2):0); + $today_new_user = ($user_today_cost_data?$user_today_cost_data['num']:0)."/".count($today_register_id_list); + $refund = ($refund_cost_data?$refund_cost_data['money']:0); + + $data['sell_percent'] = "我的营业额($user_cost)/总营业额($all_cost)=" . $sell_percent . '%'; + $data['cost_percent'] = "消费客户数($user_cost_num)/所有客户($all_cost_num)=" . $cost_percent . '%'; + $data['cost_register'] = "注册且购买过的客户($user_register_cost_count)/当月新注册客户总数($user_register_count)=" . $cost_register . '%'; + $data['cost_old_user'] = "老客户在本月再次购买(" . ($user_cost_num - $user_register_cost_count) . ")/所有老客户(" . $cost_old_user . '%'; + $data['com_cost'] = "同比增加金额=" . $com_cost; + $data['com_percent'] = "同比增加百分比=" . $com_percent.'%'; + $data['today_new_user'] = "今日新赠客户".$today_new_user; + $data['refund'] = "总退款金额".$refund; + + \result($data); + } + + /** + * @description: 获取所有客户销售信息 + * @param {*} + * @return {*} + */ + public function getAll() + { + $manager_model = new Manager; + $manager_list = $manager_model->getAllList(); + $list = []; + foreach($manager_list as $info){ + $temp = $this->getInfo($info['id']); + $temp['name'] = $info['RealName']; + $list[] = $temp; + } + \result($list); + } + + private function getInfo($manager_id) + { + $user_model = new User; + $product_order_model = new ProductOrder; + $where['ManagerId'] = $manager_id; + + $user_id_list = []; + $register_id_list = []; + $today_register_id_list = []; + $data = []; + $time_where1 = ''; + $time_where2 = ''; + $time_where_today = ''; + $time_where2_today = ''; + $data = []; + + + $date1 = date("Y-m-01", time()); + $date2 = date("Y-m-d H:i:s", time()); + $flag = date("d", time()) == date("t", time()); + $date3 = date("Y-m-01", strtotime("-1 month")); + $date4 = $flag ? date("Y-m-t H:i:s", strtotime("-1 month")) : date("Y-m-d H:i:s", strtotime("-1 month")); + + if(isset($_GET['sell_month'])){ + $date1 = date('Y-m-01', strtotime($_GET['sell_month'])); + $date2 = date("Y-m-t 23:59:59", strtotime($_GET['sell_month'])); + $date3 = date("Y-m-01", strtotime($_GET['sell_month']." -1 month")); + $date4 = date("Y-m-t 23:59:59", strtotime($_GET['sell_month']." -1 month")); + } + + + //本月 + $time_where1 .= " UpdateTime>='" . $date1 . "' and UpdateTime<='" . $date2 . "' "; + $time_where2 .= " UpdateTime>='" . $date3 . "' and UpdateTime<='" . $date4 . "' "; + $time_where_today .= " UpdateTime>='" . date("Y-m-d") . "' and UpdateTime<='" . $date2 . "' "; + + //获取用户列表 + $user_list = $user_model->getAllManagerUser($where); + foreach ($user_list as $info) { + $user_id_list[] = $info['Id']; + //本月注册用户 + if (strtotime($info['CreateTime']) >= strtotime($date1) && strtotime($info['CreateTime']) <= strtotime($date2)) { + $register_id_list[] = $info['Id']; + } + if (strtotime($info['CreateTime']) >= strtotime(date("Y-m-d")) && strtotime($info['CreateTime']) <= strtotime($date2)) { + $today_register_id_list[] = $info['Id']; + } + } + $user_where = ['UserId' => ['in', $user_id_list]]; + $register_where = ['UserId' => ['in', $register_id_list]]; + //获取本月所有消费额 + $all_cost_data = $product_order_model->getAllMoney($time_where1); + //获取本月该客户经理所有的消费额 + $user_cost_data = $product_order_model->getAllMoney($time_where1, $user_where); + //获取上月该客户经理所有的消费额 + $user_last_month_cost_data = $product_order_model->getAllMoney($time_where2, $user_where); + //获取今天该客户经理所有的消费额 + $user_today_cost_data = $product_order_model->getAllMoney($time_where2_today, ['UserId' => ['in', $today_register_id_list]]); + //获取本月该客户经理所有新注册的消费额 + $register_cost_data = $product_order_model->getAllMoney($time_where1, $register_where); + //获取本月总退款 + $refund_cost_data = $product_order_model->getRefund($time_where1, $user_where); + //总消费营业额 + $all_cost = $all_cost_data['money']??0; + $user_cost = $user_cost_data['money']??0; + $all_cost_num = $all_cost_data['num']??0; + $user_cost_num = $user_cost_data['num']??0; + //上月 + $user_last_month_cost = $user_last_month_cost_data['money']??0; + //注册 + $user_register_count = count($register_id_list); + $user_register_cost_count = $register_cost_data?$register_cost_data['num']:0; + + $data['sell_percent'] = ($all_cost?round($user_cost / $all_cost * 100, 2):0).'%'; + $data['cost_percent'] = ($all_cost_num?round($user_cost_num / $all_cost_num * 100, 2):0).'%'; + $data['cost_register'] = ($user_register_count?round($user_register_cost_count / $user_register_count * 100, 2):0); + $data['cost_old_user'] = (($all_cost_num - $user_register_count)?round(($user_cost_num - $user_register_cost_count) / ($all_cost_num - $user_register_count) * 100, 2):0); + $data['com_cost'] = ($user_cost - $user_last_month_cost); + $data['com_percent'] = ($user_last_month_cost?round(($user_cost - $user_last_month_cost) / $user_last_month_cost * 100, 2):0).'%'; + $data['today_new_user'] = ($user_today_cost_data?$user_today_cost_data['num']:0)."/".count($today_register_id_list); + $data['refund'] = ($refund_cost_data?$refund_cost_data['money']:0); + + return $data; + } +} diff --git a/app/manager/model/ProductOrder.php b/app/manager/model/ProductOrder.php index 8bcc922..c0aeff2 100644 --- a/app/manager/model/ProductOrder.php +++ b/app/manager/model/ProductOrder.php @@ -4,8 +4,8 @@ * @version: * @Author: kangkang * @Date: 2020-10-16 14:44:02 - * @LastEditors: kangkang - * @LastEditTime: 2020-11-19 18:49:23 + * @LastEditors: Please set LastEditors + * @LastEditTime: 2020-11-22 10:31:50 */ namespace app\manager\model; @@ -31,6 +31,44 @@ class ProductOrder extends Model ->fetchAll(); } + /** + * @description: 获取支付总金额 + * @param {*} + * @return {*} + */ + public function getAllMoney($where = '', $where2 = []) + { + $data = $this->field('sum(PaymentAmount) as money,UserId') + ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]]) + ->where($where) + ->where($where2) + ->group(['UserId']) + ->fetchAll(); + $result = [ + 'money' => 0, + 'num' => 0 + ]; + foreach($data as $info){ + $result['money'] += $info['money']; + $result['num'] ++; + } + return $result; + } + + /** + * @description: 获取退款总金额 + * @param {*} + * @return {*} + */ + public function getRefund($where = '', $where2 = []) + { + return $this->field('sum(PaymentAmount) as money') + ->where(['OrderState' => ['in', ProductOrderEnum::$Refunds]]) + ->where($where) + ->where($where2) + ->fetch(); + } + /** * @description: 获取所有支付成功用户近一个月的支付总金额 * @param {*} diff --git a/app/manager/model/User.php b/app/manager/model/User.php index 559d155..038104f 100644 --- a/app/manager/model/User.php +++ b/app/manager/model/User.php @@ -5,7 +5,7 @@ * @Author: kangkang * @Date: 2020-10-16 14:44:02 * @LastEditors: Please set LastEditors - * @LastEditTime: 2020-11-20 15:59:59 + * @LastEditTime: 2020-11-20 16:51:47 */ namespace app\manager\model; diff --git a/enum/order/ProductOrder.php b/enum/order/ProductOrder.php index 7c832dc..a5739c2 100644 --- a/enum/order/ProductOrder.php +++ b/enum/order/ProductOrder.php @@ -4,8 +4,8 @@ * @version: * @Author: kangkang * @Date: 2020-10-22 15:35:49 - * @LastEditors: kangkang - * @LastEditTime: 2020-10-28 17:43:57 + * @LastEditors: Please set LastEditors + * @LastEditTime: 2020-11-20 17:58:33 */ namespace enum\order; @@ -43,7 +43,7 @@ class ProductOrder //付款完成 public static $PayComplete = [20,90]; - //账单付款和退款 + //账单退款 public static $Refunds = [30,40,50]; //统计分析用到的状态 public static $fenxi = [20,40,50,90];