ci = &get_instance(); $this->init($params); $class_name = lcfirst(get_class($this));//调用类的名称,子类或者当前类名称 $this->log_file = "libraris_{$class_name}.log"; } public function init($params) { if ($params['app_id']) { $this->app_id = $params['app_id']; $this->load->model('app/App_model', 'mdApp'); $this->app_config = $this->ci->mdApp->appConfig()[$this->app_id]; } } /** * Notes:增加分销记录 * @param $oid int 订单id * @return bool */ public function deal_log($oid){ $this->load->model('receiver/order/receiver_orders_model'); $this->load->model('receiver/receiver_clues_model'); $this->load->model('auto/auto_cars_model'); $this->load->model($this->app_config['model'], 'mdUser'); $this->load->model('app/Deal_log_model', 'mdDealLog'); $order = $this->ci->receiver_orders_model->get(['id'=>$oid]); $clue = $this->ci->receiver_clues_model->get(['id'=>$order['clue_id']]); if(!$clue){ return array('code' => 0, 'msg' => '参数错误'); } //该线索是否存在分销 if($this->ci->mdDealLog->get(['ex_id'=>$clue['id'],'status>='=>0])){ return array('code' => 0, 'msg' => '该线索已存在分销记录'); } $where = [ 'brand_id' => $order['brand_id'], 's_id' => $order['s_id'], 'v_id' => $order['v_id'], 'status' => 1 ]; $car = $this->ci->auto_cars_model->get($where); if(!$car['brokerage_1'] && !$car['brokerage_2']){ return array('code' => 0, 'msg' => '该车辆未设置分销佣金'); } $app_uid = $clue['recommend_id']; $reU = $this->ci->mdUser->get(array('id' => $app_uid), 'id,dealer,up_uid'); if (!$reU) { return array('code' => 0, 'msg' => '用户不存在'); } if (!$reU['dealer']) { return array('code' => 0, 'msg' => '不是分销用户'); } $res = array(); $brokerage_1 = $car['brokerage_1']; $brokerage_2 = $car['brokerage_2']; $brokerage = $brokerage_1 + $brokerage_2; $reU2 = $this->ci->mdUser->get(array('id' => $reU['up_uid']), 'dealer'); if ($reU['up_uid'] && $reU2['dealer']) {//存在上一级并且是分销人员 $this->set_deal_log(array(1, $reU['id'], 0, $brokerage_2, $clue['id'], $order['sid'], $clue['app_id'])); $this->set_deal_log(array(1, $reU['up_uid'], $reU['id'], $brokerage_1, $clue['id'], $order['sid'], $clue['app_id'])); }else{ $this->set_deal_log(array(0, $reU['id'], 0, $brokerage, $clue['id'], $order['sid'], $clue['app_id'])); } if ($this->data) { $res2 = $this->ci->mdDealLog->add_batch($this->data); if (!$res2) { debug_log("[error] " . __FUNCTION__ . ":err_sql:" . $this->ci->mdDealLog->db->last_query(), $this->log_file); } } } private function set_deal_log($params) { if ($params[3]) { $this->data[] = array('type' => $params[0], 'app_uid' => $params[1], 't_uid' => $params[2], 'money' => $params[3] , 'ex_id' => $params[4], 'cf_sid' => $params[5], 'app_id' => $params[6], 'status' => 0, 'c_time' => time()); } } /** * 确认交车 * @param $oid int 订单id * @return bool */ public function deal_log_finish($oid){ $this->load->model('app/User_account_model', 'mdUserAccount'); $this->load->model('app/User_accountlog_model', 'mdUserAccountLog'); $this->load->model('app/Deal_log_model', 'mdDealLog'); $this->load->model('receiver/order/receiver_orders_model'); $order = $this->ci->receiver_orders_model->get(['id'=>$oid]); if(!$order){ return array('code' => 0, 'msg' => '订单不存在'); } $where = array("app_id" => $this->app_id, "cf_sid" => $order['sid'], "status" => 0); $res = $this->ci->mdDealLog->select($where, 'id DESC', 0, 0, 'id,app_uid,money'); if ($res) { foreach ($res as $key => $value) { $reUa = $this->ci->mdUserAccount->get(array('app_id' => $this->app_id, 'app_uid' => $value['app_uid'])); if ($reUa['id']) { $aid = $reUa['id']; $money_total = $reUa['money_total'] + $value['money']; $money_left = $reUa['money_left'] + $value['money']; } else { $money_total = $value['money']; $money_left = $value['money']; $addData = array('app_id' => $this->app_id, 'app_uid' => $value['app_uid'], 'status' => 1, 'c_time' => time()); $aid = $this->ci->mdUserAccount->add($addData); } if ($aid) { $this->ci->mdUserAccountLog->db->trans_begin(); $this->ci->mdUserAccount->db->trans_begin(); $this->ci->mdDealLog->db->trans_begin(); //增加资金流水表 $this->ci->mdUserAccountLog->add(array('account_id' => $aid, 'trade_type' => 1, 'money_in' => $value['money'] , 'money_left' => $money_left, 'status' => 1, 'c_time' => time())); //更新账户表 $this->ci->mdUserAccount->update(array('money_total' => $money_total, 'money_left' => $money_left), array('id' => $aid)); //更新分销记录表 $this->ci->mdDealLog->update(array('status' => 1), array('id' => $value['id'])); if ($this->ci->mdUserAccountLog->db->trans_status() && $this->ci->mdUserAccount->db->trans_status() && $this->ci->mdDealLog->db->trans_status()) { $this->ci->mdUserAccountLog->db->trans_commit(); $this->ci->mdUserAccount->db->trans_commit(); $this->ci->mdDealLog->db->trans_commit(); debug_log("[success] " . __FUNCTION__ . ":操作成功,account_id={$aid}", $this->log_file); } else { $this->ci->mdUserAccountLog->db->trans_rollback(); $this->ci->mdUserAccount->db->trans_rollback(); $this->ci->mdDealLog->db->trans_rollback(); debug_log("[finish] " . __FUNCTION__ . ":执行失败回滚,account_id={$aid}", $this->log_file); } } } } } /** * Notes:核销成功/确认收货后加佣金 * Created on: 2020/7/9 16:30 * Created by: dengbw * @param $params * @return bool */ public function deal_log_code($params) { if (!$params['sid']) { return array('code' => 0, 'msg' => '参数错误'); } $this->load->model('appitem/items_model', 'mdItems'); $this->load->model('order/order_purchase_model', 'mdOrderPurchase'); $order = $this->ci->mdOrderPurchase->get(array('sid' => $params['sid'], 'app_id' => $this->app_id)); $item = $this->ci->mdItems->get(array('id' => $order['item_id'], 'app_id' => $this->app_id)); if (!$order) { return array('code' => 0, 'msg' => '订单不存在'); } if (!$item) { return array('code' => 0, 'msg' => '商品不存在'); } if (!$item['is_deal']) { return array('code' => 0, 'msg' => '不是分销商品'); } $where = array("app_id" => $this->app_id, "cf_sid" => $params['sid'], "status" => 0); if ($item['type'] == 1) {//实物 if ($order['status_detail'] != 27) { return array('code' => 0, 'msg' => '未收货'); } } else if ($item['type'] == 2) {//虚似商品 if ($item['code_num'] == 1 && $params['ex_id']) { $this->load->model('apporder/order_exchange_model', 'mdOrderExchange'); $re_exchange = $this->ci->mdOrderExchange->get(array('id' => $params['ex_id'])); if ($re_exchange['status'] != 1) { return array('code' => 0, 'msg' => '未核销'); } $where['ex_id'] = $params['ex_id']; //虚似商品且一个核销码,根据核销码加佣金 } } $this->load->model('app/User_account_model', 'mdUserAccount'); $this->load->model('app/User_accountlog_model', 'mdUserAccountLog'); $this->load->model('app/Deal_log_model', 'mdDealLog'); $res = $this->ci->mdDealLog->select($where, 'id DESC', 0, 0, 'id,app_uid,money'); if ($res) { foreach ($res as $key => $value) { $reUa = $this->ci->mdUserAccount->get(array('app_id' => $this->app_id, 'app_uid' => $value['app_uid'])); if ($reUa['id']) { $aid = $reUa['id']; $money_total = $reUa['money_total'] + $value['money']; $money_left = $reUa['money_left'] + $value['money']; } else { $money_total = $value['money']; $money_left = $value['money']; $addData = array('app_id' => $this->app_id, 'app_uid' => $value['app_uid'], 'status' => 1, 'c_time' => time()); $aid = $this->ci->mdUserAccount->add($addData); } if ($aid) { $this->ci->mdUserAccountLog->db->trans_begin(); $this->ci->mdUserAccount->db->trans_begin(); $this->ci->mdDealLog->db->trans_begin(); //增加资金流水表 $this->ci->mdUserAccountLog->add(array('account_id' => $aid, 'trade_type' => 1, 'money_in' => $value['money'] , 'money_left' => $money_left, 'status' => 1, 'c_time' => time())); //更新账户表 $this->ci->mdUserAccount->update(array('money_total' => $money_total, 'money_left' => $money_left), array('id' => $aid)); //更新分销记录表 $this->ci->mdDealLog->update(array('status' => 1), array('id' => $value['id'])); if ($this->ci->mdUserAccountLog->db->trans_status() && $this->ci->mdUserAccount->db->trans_status() && $this->ci->mdDealLog->db->trans_status()) { $this->ci->mdUserAccountLog->db->trans_commit(); $this->ci->mdUserAccount->db->trans_commit(); $this->ci->mdDealLog->db->trans_commit(); debug_log("[success] " . __FUNCTION__ . ":操作成功,account_id={$aid}", $this->log_file); } else { $this->ci->mdUserAccountLog->db->trans_rollback(); $this->ci->mdUserAccount->db->trans_rollback(); $this->ci->mdDealLog->db->trans_rollback(); debug_log("[finish] " . __FUNCTION__ . ":执行失败回滚,account_id={$aid}", $this->log_file); } } } } } /** * Notes:更新分销记录状态 -1无效 * Created on: 2020/7/13 15:37 * Created by: dengbw * @param $params * @return array */ public function deal_log_status($params) { $this->load->model('appdistribution/Deal_log_model', 'mdDealLog'); $this->load->model('appitem/items_model', 'mdItems'); $this->load->model('order/order_purchase_model', 'mdOrderPurchase'); if (!$params['id']) { return array('code' => 0, 'msg' => '参数错误'); } $order = $this->ci->mdOrderPurchase->get(array('id' => $params['id'])); if (!$order) { return array('code' => 0, 'msg' => '订单不存在'); } $item = $this->ci->mdItems->get(array('id' => $order['item_id'], 'app_id' => $this->app_id)); if (!$item) { return array('code' => 0, 'msg' => '商品不存在'); } if (!$item['is_deal']) { return array('code' => 0, 'msg' => '不是分销商品'); } if (($order['status'] == 3 && $order['status_detail'] == 33) //申请退款 已退款 || ($order['status'] == 2 && $order['status_detail'] == 23) //支付后环节 核销码过期未退款 || ($order['status'] == 2 && $order['status_detail'] == 24) //支付后环节 核销码过期已退款 ) { //更新分销状态失效 $this->ci->mdDealLog->update(array('status' => -1), array('app_id' => $order['app_id'], 'cf_sid' => $order['sid'])); debug_log("[success] " . __FUNCTION__ . ":操作成功,sid={$order['sid']}", $this->log_file); } else { debug_log("[error] " . __FUNCTION__ . ":{$order['sid']}_更新分销记录状态失败", $this->log_file); } } public function __get($name) { if ('_model' === substr($name, -6)) { return $this->ci->$name; } elseif ('load' == $name) { return $this->ci->load; } return null; } }