123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
-
- namespace app\admin\model;
-
- use think\Model;
-
- class Collection extends Model
- {
-
- //收款记录
- public function getAll($param, $page, $pageSize)
- {
- unset($param['page']);
- unset($param['pageSize']);
- foreach ($param as $k => $v) {
- if ($param[$k] === '' || $param[$k] === null) {
- unset($param[$k]);
- }
- }
- $where = [];
- if (isset($param['type'])) {
- $where[] = ['c.type', '=', $param['type']];
- }
- if (isset($param['demand'])) {
- $where[] = ['c.demand', 'like', '%' . $param['demand'] . '%'];
- }
- if (isset($param['adcode'])) {
- $where[] = ['t.adcode', '=', $param['adcode']];
- }
- if (isset($param['hospital_id'])) {
- $where[] = ['a.hospital_id', '=', $param['hospital_id']];
- }
- if (isset($param['user_id'])) {
- $where[] = ['t.user_id', '=', $param['user_id']];
- }
- if (isset($param['customer_id'])) {
- $where[] = ['c.customer_id', '=', $param['customer_id']];
- }
- if (isset($param['city_id'])) {
- $where[] = ['t.city_id', '=', $param['city_id']];
- }
- if (isset($param['keyword'])) {
- $where[] = ['t.name|t.phone', 'like', '%' . $param['keyword'] . '%'];
- }
- if (isset($param['source'])) {
- $where[] = ['t.source', '=', $param['source']];
- }
- if (isset($param['signup_time_start'])) {
- $where[] = ['t.signup_time', 'between', [$param['signup_time_start'], $param['signup_time_end']]];
- }
- if (isset($param['collection_time_start'])) {
- $where[] = ['c.collection_time', 'between', [$param['collection_time_start'], $param['collection_time_end']]];
- }
- if (isset($param['appoint_time_start'])) {
- $where[] = ['a.appoint_time', 'between', [$param['appoint_time_start'], $param['appoint_time_end']]];
- }
- $Appoint_model = new \app\admin\model\Appoint();
- $Customer_model = new \app\admin\model\Customer();
- $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,c.type,a.hospital_id,c.deal_money,c.newdeal_money,c.collection_money,FROM_UNIXTIME(c.collection_time) as collection_time,c.refunddeal_money,c.refundcollection_money,c.feedback,c.remark,t.user_id,c.id')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData', 'getExamineOneData'])->where($where)->select()->toArray();
- $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->count();
- foreach ($result as $k => $v) {
- $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
- $result[$k]['collection_money'] = $v['collection_money'] / 100;
- $result[$k]['deal_money'] = $v['deal_money'] / 100;
- //0=全款,1=收尾款,2=新增成交,3=退款
- if ($v['type'] == 2) {
- $result[$k]['deal_money'] = $v['newdeal_money'] / 100;
- }
- if ($v['type'] == 3) {
- $result[$k]['collection_money'] = $v['refundcollection_money'] / 100;
- $result[$k]['deal_money'] = $v['refunddeal_money'] / 100;
- }
- $result[$k]['examine_collection_money'] = $v['examine_collection_money'] / 100;
- $result[$k]['examine_deal_money'] = $v['examine_deal_money'] / 100;
- $result[$k]['examine_status'] = (string)$v['examine_status'];
- }
- return ['rows' => $result, 'total' => $total];
- }
-
- //成交记录
- public function getDealAll($param, $page, $pageSize)
- {
- unset($param['page']);
- unset($param['pageSize']);
- foreach ($param as $k => $v) {
- if ($param[$k] === '' || $param[$k] === null) {
- unset($param[$k]);
- }
- }
- $where = [];
- if (isset($param['collection'])) {
- $where[] = ['a.collection', '=', $param['collection']];
- }
- if (isset($param['demand'])) {
- $where[] = ['c.demand', 'like', '%' . $param['demand'] . '%'];
- }
- if (isset($param['adcode'])) {
- $where[] = ['t.adcode', '=', $param['adcode']];
- }
- if (isset($param['hospital_id'])) {
- $where[] = ['a.hospital_id', '=', $param['hospital_id']];
- }
- if (isset($param['user_id'])) {
- $where[] = ['t.user_id', '=', $param['user_id']];
- }
- if (isset($param['city_id'])) {
- $where[] = ['t.city_id', '=', $param['city_id']];
- }
- if (isset($param['keyword'])) {
- $where[] = ['t.name|t.phone', 'like', '%' . $param['keyword'] . '%'];
- }
- if (isset($param['source'])) {
- $where[] = ['t.source', '=', $param['source']];
- }
- if (isset($param['signup_time_start'])) {
- $where[] = ['t.signup_time', 'between', [$param['signup_time_start'], $param['signup_time_end']]];
- }
- if (isset($param['collection_time_start'])) {
- $where[] = ['c.collection_time', 'between', [$param['collection_time_start'], $param['collection_time_end']]];
- }
- if (isset($param['appoint_time_start'])) {
- $where[] = ['a.appoint_time', 'between', [$param['appoint_time_start'], $param['appoint_time_end']]];
- }
- $Appoint_model = new \app\admin\model\Appoint();
- $Customer_model = new \app\admin\model\Customer();
- $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,c.type,a.hospital_id,a.collection,FROM_UNIXTIME(c.collection_time) as collection_time,c.feedback,c.remark,t.user_id,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData'])->group('c.appoint_id')->where($where)->select()->toArray();
- $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->group('c.appoint_id')->count();
- foreach ($result as $k => $v) {
- $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
- $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
- $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
- }
- return ['rows' => $result, 'total' => $total];
- }
-
- //成交信息
- public function getDealDetail($param, $page, $pageSize)
- {
- unset($param['page']);
- unset($param['pageSize']);
- foreach ($param as $k => $v) {
- if ($param[$k] === '' || $param[$k] === null) {
- unset($param[$k]);
- }
- }
- $Appoint_model = new \app\admin\model\Appoint();
- $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.customer_id = ' . $param['customer_id'] . ' and a.is_deal = 1')->field('c.appoint_id,a.city_id,a.collection,a.hospital_id,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money,FROM_UNIXTIME(c.collection_time) as collection_time,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money')->group('c.appoint_id')->limit($page, $pageSize)->with(['getHospitalOneData', 'getCollectionManyData', 'getDistrictOneData'])->select()->toArray();
- $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.customer_id = ' . $param['customer_id'] . ' and a.is_deal = 1')->group('c.appoint_id')->count();
- foreach ($result as $k => $v) {
- $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
- $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
- $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
- if (isset($v['get_collection_many_data']) && !empty($v['get_collection_many_data'])) {
- foreach ($v['get_collection_many_data'] as $kk => $vv) {
- $result[$k]['get_collection_many_data'][$kk]['collection_time'] = date('Y-m-d H:i:s', $vv['collection_time']);
- //0=全款,1=收尾款,2=新增成交,3=退款
- if ($vv['type'] == 2) {
- $result[$k]['get_collection_many_data'][$kk]['deal_money'] = $vv['newdeal_money'] / 100;
- }
- if ($vv['type'] == 3) {
- $result[$k]['get_collection_many_data'][$kk]['collection_money'] = $vv['refundcollection_money'] / 100;
- $result[$k]['get_collection_many_data'][$kk]['deal_money'] = $vv['refunddeal_money'] / 100;
- }
- }
- }
- }
- return ['rows' => $result, 'total' => $total];
- }
-
- //业绩核算
- public function getAchievementAccounting($param, $page, $pageSize)
- {
- unset($param['page']);
- unset($param['pageSize']);
- foreach ($param as $k => $v) {
- if ($param[$k] === '' || $param[$k] === null) {
- unset($param[$k]);
- }
- }
- $where = [];
- if (isset($param['user_id'])) {
- $where[] = ['t.user_id', '=', $param['user_id']];
- }
- if (isset($param['collection_time'])) {
- $month_end = strtotime(date('Y-m-d', strtotime('last day of', $param['collection_time'])) . ' 23:59:59');
- $where[] = ['c.collection_time', 'between', [$param['collection_time'], $month_end]];
- } else {
- $month_start = strtotime(date('Y-01-01 00:00:00'));
- $month_end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));//当月最后一天
- $where[] = ['c.collection_time', 'between', [$month_start, $month_end]];
- }
- $Appoint_model = new \app\admin\model\Appoint();
- $Customer_model = new \app\admin\model\Customer();
- $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,c.appoint_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,a.hospital_id,FROM_UNIXTIME(c.collection_time) as collection_time,t.user_id,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData', 'getCollectionManyData2'])->group('c.appoint_id')->where($where)->select()->toArray();
- $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->group('c.appoint_id')->count();
- $months = [];
- foreach ($result as $k => $v) {
- if (isset($v['get_collection_many_data2']) && !empty($v['get_collection_many_data2'])) {
- foreach ($v['get_collection_many_data2'] as $kk => $vv) {
- $months[$k][$kk] = $vv['months'];
- $result[$k][$vv['months']] = $vv['all_collection_money'] / 100;
- // $result[$k]['months'][$kk] = $vv['months'];
- }
- $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
- $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
- }
- }
- //月份降维
- $months2 = [];
- foreach ($months as $k => $v) {
- $months2 = array_merge($months2, $v);//降维
- }
- $months2 = array_values(array_keys(array_flip($months2)));
- return ['rows' => $result, 'total' => $total, 'months' => $months2];
- }
-
- public function getCollectionManyData2()
- {
- return $this->hasMany(\app\admin\model\Collection::class, 'appoint_id', 'appoint_id')->field('(sum(collection_money)-sum(refundcollection_money)) as all_collection_money,(sum(deal_money)+sum(newdeal_money)-sum(refunddeal_money)) as all_deal_money,month(from_unixtime(collection_time)) as months,appoint_id')->group('appoint_id,months');
- }
-
- public function getDistrictOneData()
- {
- return $this->hasOne(\app\admin\model\District::class, 'adcode', 'city_id')->bind(['city_name' => 'name', 'province_id' => 'parent_id']);
- }
-
- public function getExamineOneData()
- {
- return $this->hasOne(\app\admin\model\Examine::class, 'collection_id', 'id')->bind(['examine_deal_money' => 'deal_money', 'examine_collection_money' => 'collection_money', 'examine_status' => 'status', 'examine_reason' => 'reason']);
- }
-
- public function getUserOneData()
- {
- return $this->hasOne(\app\admin\model\User::class, 'id', 'user_id')->bind(['username' => 'username']);
- }
-
- public function getHospitalOneData()
- {
- return $this->hasOne(\app\admin\model\Hospital::class, 'id', 'hospital_id')->bind(['hospital_name' => 'name']);
- }
-
- public function getCollectionManyData()
- {
- return $this->hasMany(\app\admin\model\Collection::class, 'appoint_id', 'appoint_id');
- }
-
- public function getCustomerOneData()
- {
- return $this->hasOne(\app\admin\model\Customer::class, 'id', 'customer_id')->bind(['customer_name' => 'name']);
- }
-
- public function getAppointOneData()
- {
- return $this->hasOne(\app\admin\model\Appoint::class, 'id', 'appoint_id')->bind(['hospital_id' => 'hospital_id', 'appoint_time' => 'appoint_time']);
- }
-
- // public function read($param)
- // {
- // $result = $this->where($this->getPk(), $param[$this->getPk()])->find();
- // if (isset($result[$this->getPk()])) {
- // return $result->toArray();
- // } else {
- // return [];
- // }
- // }
-
- }
|