Files
liche/common/libraries/entity/Deal_entity.php
T
2021-08-03 19:13:43 +08:00

309 lines
15 KiB
PHP
Executable File

<?php
/**
* Notes:分销商品
* Created on: 2020/7/9 14:44
* Created by: dengbw
*/
class Deal_entity
{
private $ci;
private $app_id;
private $app_config;
private $log_file;//日志文件
private $data = array();
public function __construct($params = array())
{
$this->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:增加分销记录
* Created on: 2020/7/8 15:21
* Created by: dengbw
* @param $params
* @return bool
*/
public function deal_log($params)
{
if (!$params['sid']) {
return array('code' => 0, 'msg' => '参数错误');
}
$this->load->model('order/order_purchase_model', 'mdOrderPurchase');
$this->load->model($this->app_config['model'], 'mdUser');
$this->load->model('app/Deal_log_model', 'mdDealLog');
$this->load->model('apporder/order_exchange_model', 'mdOrderExchange');
$order = $this->ci->mdOrderPurchase->get(array('sid' => $params['sid'], 'app_id' => $this->app_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 ($item['type'] == 1) {//实物
if ($order['status_detail'] != 25) {
return array('code' => 0, 'msg' => '不是待发货');
}
} else if ($item['type'] == 2) {//虚似商品
if ($order['status_detail'] != 21) {
return array('code' => 0, 'msg' => '不是待核销');
}
}
if ($order['sku_id']) {
$reIs = $this->ci->mdItemStock->get(array('id' => $order['sku_id']), 'brokerage_1,brokerage_2');
} else {
$reIs = $this->ci->mdItemStock->get(array('item_id' => $order['item_id'], 'sku_ids' => 0), 'brokerage_1,brokerage_2');
}
$app = $this->ci->mdApp->get(array('id' => $this->app_id));
$json = $app['jsondata'] ? json_decode($app['jsondata'], true) : array();
$lock_fans = $json['lock_fans'] ? $json['lock_fans'] : 0; //1锁粉
$app_uid = $order['app_uid'];
if ($lock_fans == 0) {//没锁粉操作
if ($order['cf_uid']) {
$app_uid = $order['cf_uid'];
} else {
$order['cf_uid'] = $app_uid;
}
}
$reU = $this->ci->mdUser->get(array('id' => $app_uid), 'dealer,up_uid');
if (!$reU) {
//debug_log("[error] " . __FUNCTION__ . ":{$order['app_uid']}_用户不存在", $this->log_file);
return array('code' => 0, 'msg' => '用户不存在');
}
if ($lock_fans == 0 && $reU['dealer'] == 0) {//没锁粉操作
return array('code' => 0, 'msg' => '不是分销用户');
}
$res = array();
$brokerage_1 = $reIs['brokerage_1'];
$brokerage_2 = $reIs['brokerage_2'];
$brokerage = $reIs['brokerage_1'] + $reIs['brokerage_2']; //直销
if ($item['type'] == 2 && $item['code_num'] == 1) {//虚似商品且一个核销码
$where = array('status' => 0, 'app_id' => $this->app_id, 'sid' => $params['sid'], 'order_type' => 'purchase');
$res = $this->ci->mdOrderExchange->select($where, 'id DESC', 0, 0, 'id');///未核销
} else {
//商品多个核销码数量或实物商品,只算订算数
for ($x = 0; $x < $order['item_num']; $x++) {
$res[] = array("id" => 0);
}
}
//type 0自己购买 1团员购买 2粉丝购买 3团员粉丝购买
if ($res) {
foreach ($res as $key => $value) {
if ($lock_fans == 1) {//1锁粉
if ($reU['dealer'] == 1) {
if ($reU['up_uid'] == 0) {//团长自己购买直销
$this->set_deal_log(array(0, $order['app_uid'], 0, $brokerage, $value['id'], $order['sid'], $order['app_id']));
} else {//团员自己购买
$this->set_deal_log(array(0, $order['app_uid'], 0, $brokerage_1, $value['id'], $order['sid'], $order['app_id']));
}
if ($reU['up_uid']) {//团员购买团长2级
$reU3 = $this->ci->mdUser->get(array('id' => $reU['up_uid']), 'dealer');//团长
if ($reU3['dealer'] == 1 && $reU3['up_uid'] == 0) {//判断是否团长
$this->set_deal_log(array(1, $reU['up_uid'], $order['app_uid'], $brokerage_2, $value['id'], $order['sid'], $order['app_id']));
}
}
} else if ($reU['dealer'] == 0 && $reU['up_uid']) {
//粉丝购买直销
$this->set_deal_log(array(2, $reU['up_uid'], $order['app_uid'], $brokerage, $value['id'], $order['sid'], $order['app_id']));
$reU2 = $this->ci->mdUser->get(array('id' => $reU['up_uid']), 'up_uid');//团员
$reU3 = $this->ci->mdUser->get(array('id' => $reU2['up_uid']), 'dealer');//团长
if ($reU3['dealer'] == 1 && $reU3['up_uid'] == 0) {//团员粉丝购买团长2级
$this->set_deal_log(array(3, $reU2['up_uid'], $reU['up_uid'], $brokerage_2, $value['id'], $order['sid'], $order['app_id']));
}
}
} else if ($lock_fans == 0) {
if ($order['cf_uid'] == $order['app_uid']) {//自己购买
if ($reU['up_uid'] == 0) {//团长自己购买直销
$this->set_deal_log(array(0, $order['app_uid'], 0, $brokerage, $value['id'], $order['sid'], $order['app_id']));
} else {//团员自己购买1级
$this->set_deal_log(array(0, $order['app_uid'], 0, $brokerage_1, $value['id'], $order['sid'], $order['app_id']));
}
if ($reU['up_uid']) {//团员购买团长2级
$reU3 = $this->ci->mdUser->get(array('id' => $reU['up_uid']), 'dealer');
if ($reU3['dealer'] == 1 && $reU3['up_uid'] == 0) {//判断是否团长
$this->set_deal_log(array(1, $reU['up_uid'], $order['app_uid'], $brokerage_2, $value['id'], $order['sid'], $order['app_id']));
}
}
} else {
if ($reU['up_uid'] == 0) {//团长自己分享出去直销
$this->set_deal_log(array(2, $order['cf_uid'], $order['app_uid'], $brokerage, $value['id'], $order['sid'], $order['app_id']));
} else {//团员分享出去
//团员1级
$this->set_deal_log(array(2, $order['cf_uid'], $order['app_uid'], $brokerage_1, $value['id'], $order['sid'], $order['app_id']));
//团长2级
$reU3 = $this->ci->mdUser->get(array('id' => $reU['up_uid']), 'dealer');
if ($reU3['dealer'] == 1 && $reU3['up_uid'] == 0) {//判断是否团长
$this->set_deal_log(array(3, $reU['up_uid'], $order['cf_uid'], $brokerage_2, $value['id'], $order['sid'], $order['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());
}
}
/**
* 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;
}
}