| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- /**
- * Created by PhpStorm.
- * User: dingran
- * Date: 2018/8/8
- * Time: 下午6:52
- */
- namespace app\backend\controllers;
- use app\common\components\BaseController;
- use app\common\models\Income;
- use app\common\models\Order;
- use app\common\services\member\MemberRelation;
- use Illuminate\Foundation\Bus\DispatchesJobs;
- use Illuminate\Support\Facades\DB;
- use Yunshop\Commission\models\Agents;
- use Yunshop\Commission\models\Commission;
- use Yunshop\Commission\models\CommissionOrder;
- use Yunshop\Commission\services\CommissionOrderService;
- use Yunshop\Mryt\job\UpgradeByRegisterJob;
- use Yunshop\Mryt\listeners\MemberRelationEventListener;
- use Yunshop\Mryt\services\UpgradeService;
- use Yunshop\TeamDividend\models\TeamDividendModel;
- class FixController extends BaseController
- {
- use DispatchesJobs;
- public function getAgentUniacid()
- {
- $agents = DB::select('select ya.* FROM ims_yz_agents as ya LEFT JOIN ims_yz_agent_level as al on al.id = ya.agent_level_id where ya.uniacid <> al.uniacid');
- echo '该系统有'. count($agents).'条错误数据。';
- }
- public function agentUniacid()
- {
- $agents = DB::select('select ya.* FROM ims_yz_agents as ya LEFT JOIN ims_yz_agent_level as al on al.id = ya.agent_level_id where ya.uniacid <> al.uniacid');
- $success = 0;
- $error = 0;
- foreach ($agents as $agent) {
- $log = DB::table('yz_commission_log')
- ->where('uid',$agent['member_id'])
- ->where('after_level_id',$agent['agent_level_id'])
- ->orderBy('id','desc')
- ->first();
- $this->fixAgentUniacid($agent, $log['before_level_id']);
- $success ++;
- }
- echo '成功:'. $success.'。失败:'.$error;
- }
- public function fixAgentUniacid($agent, $level_id)
- {
- if ($level_id == 0) {
- DB::table('yz_agents')->where('member_id', $agent['member_id'])->update(['agent_level_id' => 0]);
- } else {
- $level = DB::table('yz_agent_level')->where('id',$level_id)->first();
- if ($agent['uniacid'] == $level['uniacid']) {
- DB::table('yz_agents')->where('member_id', $agent['member_id'])->update(['agent_level_id' => $level_id,'updated_at' => time()]);
- } else {
- $log = DB::table('yz_commission_log')
- ->where('uid',$agent['member_id'])
- ->where('after_level_id',$level_id)
- ->orderBy('id','desc')
- ->first();
- $this->fixAgentUniacid($agent, $log['before_level_id']);
- }
- }
- }
- public function errorDividendData()
- {
- $a = DB::table('yz_team_dividend')->select(['yz_team_dividend.uniacid as tuniacid','mc_members.uniacid as uniacid','yz_team_dividend.id' , 'yz_order.id as orderid', 'yz_order.uid', 'yz_team_dividend.order_sn', 'yz_team_dividend.member_id', 'yz_team_dividend.status'])
- ->join('yz_order', 'yz_order.order_sn', '=', 'yz_team_dividend.order_sn')
- ->join('mc_members', 'mc_members.uid', '=', 'yz_team_dividend.member_id')
- ->where(DB::raw('ims_mc_members.uniacid != ims_yz_team_dividend.uniacid'))
- ->orderBy('yz_team_dividend.id', 'asc')
- ->get();
- dump($a);
- }
- public function handleCommissionOrder()
- {
- $handle = 0;
- $success = 0;
- $waitCommissionOrder = CommissionOrder::uniacid()->whereStatus(0)->get();
- $status = \Setting::get('plugin.commission')['settlement_event'] ? 1 : 3;
- if (!$waitCommissionOrder->isEmpty()) {
- foreach ($waitCommissionOrder as $key => $commissionOrder) {
- $orderModel = Order::uniacid()->whereId($commissionOrder->ordertable_id)->first();
- if ($orderModel->status >= $status) {
- $handle += 1;
- $commissionOrder->status = 1;
- if ($status == 1) {
- $commissionOrder->recrive_at = strtotime($orderModel->pay_time);
- } else {
- $commissionOrder->recrive_at = strtotime($orderModel->finish_time);
- }
- if ($commissionOrder->save()) {
- $success += 1;
- }
- }
- unset($orderModel);
- }
- }
- echo "分销订单未结算总数:{$waitCommissionOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
- }
- public function fixTeam()
- {
- $search_date = strtotime('2018-10-25 12:00:00');
- $error = [];
- $tmp = [];
- $pos = [];
- $res = DB::table('yz_team_dividend as t')
- ->select(['t.id' , 'o.id as orderid', 'o.uid', 't.order_sn', 't.member_id', 't.status'])
- ->join('yz_order as o', 'o.order_sn', '=', 't.order_sn')
- ->where('t.created_at', '>', $search_date)
- ->orderBy('t.id', 'asc')
- ->get();
- if (!$res->isEmpty()) {
- foreach ($res as $key => $rows) {
- if (!$tmp[$rows['orderid']]) {
- // $pos = [$rows->member_id => $key];
- $tmp[$rows['orderid']] = [
- 'id' => $rows['id'],
- 'order_id' => $rows['orderid'],
- 'uid' => $rows['uid'],
- 'order_sn' => $rows['order_sn'],
- 'parent_id' => $rows['member_id'],
- 'status' => $rows['status'],
- ];
- file_put_contents(storage_path('logs/team_fix.log'), print_r($tmp, 1), FILE_APPEND);
- } else {
- // $k = $pos[$rows->member_id];
- // $tmp[$k]['member_id'][] = $rows->member_id;
- }
- }
- }
- //订单会员->关系链 不匹配
- foreach ($tmp as $k => $v) {
- $total = DB::table('yz_member')
- ->where('member_id', '=', $v['uid'])
- ->where('parent_id', '=', $v['parent_id'])
- ->count();
- if (0 == $total) {
- $error[] = $v;
- file_put_contents(storage_path('logs/team_fix_error.log'), print_r($v, 1), FILE_APPEND);
- }
- }
- collect($error)->each(function ($item) {
- if (0 == $item['status']) {
- $model = Order::find($item['order_id']);
- if (!is_null($model)) {
- DB::transaction(function () use ($item, $model) {
- DB::table('yz_team_dividend')
- ->where('order_sn', '=', $item['order_sn'])
- ->delete();
- DB::table('yz_order_plugin_bonus')
- ->where('order_id', '=', $item['order_id'])
- ->where('table_name', '=', 'yz_team_dividend')
- ->delete();
- (new \Yunshop\TeamDividend\Listener\OrderCreatedListener)->fixOrder($model);
- file_put_contents(storage_path('logs/team_fix_del.log'), print_r($item, 1), FILE_APPEND);
- });
- }
- }
- });
- echo '数据修复ok';
- }
- public function fixArea()
- {
- $search_date = strtotime('2018-10-25 12:00:00');
- $error = [];
- $tmp = [];
- $res = DB::table('yz_area_dividend as t')
- ->select(['t.id' , 'o.id as orderid', 'o.uid', 't.order_sn', 't.member_id', 't.status'])
- ->join('yz_order as o', 'o.order_sn', '=', 't.order_sn')
- ->where('t.created_at', '>', $search_date)
- ->orderBy('t.id', 'asc')
- ->get();
- if (!$res->isEmpty()) {
- foreach ($res as $key => $rows) {
- if (!$tmp[$rows['orderid']]) {
- // $pos = [$rows->member_id => $key];
- $tmp[$rows['orderid']] = [
- 'id' => $rows['id'],
- 'order_id' => $rows['orderid'],
- 'uid' => $rows['uid'],
- 'order_sn' => $rows['order_sn'],
- 'parent_id' => $rows['member_id'],
- 'status' => $rows['status'],
- ];
- file_put_contents(storage_path('logs/area_fix.log'), print_r($tmp, 1), FILE_APPEND);
- }
- }
- }
- // //订单会员->关系链 不匹配
- // foreach ($tmp as $k => $v) {
- // $total = DB::table('yz_member')
- // ->where('member_id', '=', $v['uid'])
- // ->where('parent_id', '=', $v['parent_id'])
- // ->count();
- //
- // if (0 == $total) {
- // $error[] = $v;
- //
- // file_put_contents(storage_path('logs/area_fix_error.log'), print_r($v, 1), FILE_APPEND);
- // }
- // }
- collect($tmp)->each(function ($item) {
- if (0 == $item['status']) {
- $model = Order::find($item['order_id']);
- DB::transaction(function () use ($item, $model) {
- DB::table('yz_area_dividend')
- ->where('order_sn', '=', $item['order_sn'])
- ->delete();
- DB::table('yz_order_plugin_bonus')
- ->where('order_id', '=', $item['order_id'])
- ->where('table_name', '=', 'yz_area_dividend')
- ->delete();
- (new \Yunshop\AreaDividend\Listener\OrderCreatedListener)->fixOrder($model);
- file_put_contents(storage_path('logs/area_fix_del.log'), print_r($item, 1), FILE_APPEND);
- });
- }
- });
- echo '数据修复ok';
- }
- public function fixIncome()
- {
- $count = 0;
- $income = Income::whereBetween('created_at', [1539792000,1541433600])->get();
- foreach ($income as $value) {
- $pattern1 = '/\\\u[\d|\w]{4}/';
- preg_match($pattern1, $value->detail, $exists);
- if (empty($exists)) {
- $pattern2 = '/(u[\d|\w]{4})/';
- $value->detail = preg_replace($pattern2, '\\\$1', $value->detail);
- $value->save();
- $count++;
- }
- }
- echo "修复了{$count}条";
- }
- public function mr()
- {
- $m = new MemberRelationEventListener();
- $parent_id = 6080;
- $m->fixRelation($parent_id);
- }
- public function ma()
- {
- $m = new MemberRelationEventListener();
- $parent_id = 6080;
- $m->fixAward($parent_id);
- }
- public function changeField()
- {
- $sql1 = 'ALTER TABLE `' . DB::getTablePrefix() . 'users_permission` MODIFY `modules` text NULL';
- $sql2 = 'ALTER TABLE `' . DB::getTablePrefix() . 'users_permission` MODIFY `templates` text NULL';
- try {
- DB::select($sql1);
- DB::select($sql2);
- echo '数据已修复';
- } catch (\Exception $e) {
- echo $e->getMessage();
- }
- }
- public function handleTeamOrder()
- {
- $handle = 0;
- $success = 0;
- $status = \Setting::get('plugin.team_dividend')['settlement_event'] ? 1 : 3;
- $waitTeamOrder = \Yunshop\TeamDividend\models\TeamDividendModel::uniacid()->whereStatus(0)->get();
- if (!$waitTeamOrder->isEmpty()) {
- foreach ($waitTeamOrder as $key => $teamOrder) {
- $orderModel = \app\common\models\Order::uniacid()->where('order_sn',$teamOrder->order_sn)->first();
- if ($orderModel->status >= $status) {
- $handle += 1;
- $teamOrder->recrive_at = strtotime($orderModel->pay_time);
- $teamOrder->is_pay = 1;
- if ($teamOrder->save()) {
- $success += 1;
- }
- }
- unset($orderModel);
- }
- }
- echo "经销商订单未结算总数:{$waitTeamOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
- }
- public function handleAreaOrder()
- {
- $handle = 0;
- $success = 0;
- $waitTeamOrder = \Yunshop\AreaDividend\models\AreaDividend::uniacid()->whereStatus(0)->get();
- if (!$waitTeamOrder->isEmpty()) {
- foreach ($waitTeamOrder as $key => $teamOrder) {
- $orderModel = \app\common\models\Order::uniacid()->where('order_sn',$teamOrder->order_sn)->first();
- if ($orderModel->status == 3) {
- $handle += 1;
- $teamOrder->recrive_at = strtotime($orderModel->finish_time);
- if ($teamOrder->save()) {
- $success += 1;
- }
- }
- unset($orderModel);
- }
- }
- echo "区域分红订单未结算总数:{$waitTeamOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
- }
- public function fixCommissionAmount()
- {
- $CommissionOrder = CommissionOrder::uniacid()->whereNull('commission_amount')->get();
- $set = \Setting::get('plugin.commission');
- $count = 0;
- foreach ($CommissionOrder as $commission) {
- $orderModel = Order::find($commission->ordertable_id);
- $orderGoods = $orderModel->hasManyOrderGoods;
- $commissionAmount = 0;
- $formula = '';
- foreach ($orderGoods as $key => $og) {
- //获取商品分销设置信息
- $commissionGoods = Commission::getGoodsById($og->goods_id)->first();
- if (!$commissionGoods->is_commission) {
- continue;
- }
- if ($commissionGoods) {
- if ($commissionGoods['has_commission'] == '1') {
- //商品独立佣金
- $commissionAmount += $og['payment_amount']; //分佣计算金额
- $formula .= "+商品独立佣金";//分佣计算方式
- } else {
- $countAmount = CommissionOrderService::getCountAmount($orderModel, $og, '', $set);
- $commissionAmount += $countAmount['amount'];
- $formula = $countAmount['method'];
- }
- }
- }
- $commission->commission_amount = $commissionAmount;
- $commission->formula = $formula;
- $commission->save();
- $count++;
- }
- echo '修改了'.$count.'条信息';
- }
- public function fixNotCommission()
- {
- $order_sn = \YunShop::request()->order_sn;
- $order = Order::uniacid()->where('order_sn', $order_sn)->first();
- $commission_order = CommissionOrder::uniacid()->where('ordertable_id', $order->id)->first();
- if ($commission_order) {
- echo '已有这条分红';
- } else {
- $result = (new \Yunshop\Commission\Listener\OrderCreatedListener())->handler($order);
- $commission_order = CommissionOrder::uniacid()->where('ordertable_id', $order->id)->first();
- if ($commission_order) {
- echo '成功';
- } else {
- echo '不成功,请检查设置是否正确,一定绝对必须要检查清楚!!!!!!如果正确?!那就服务器有问题,非常难受';
- }
- }
- }
- public function fixNotTeam()
- {
- $order_sn = \YunShop::request()->order_sn;
- $order = Order::uniacid()->where('order_sn', $order_sn)->first();
- $team_order = TeamDividendModel::uniacid()->where('order_sn', $order_sn)->first();
- if ($team_order) {
- echo '已有这条分红';
- } else {
- (new \Yunshop\TeamDividend\Listener\OrderCreatedListener())->handle($order);
- $team_order = TeamDividendModel::uniacid()->where('order_sn', $order_sn)->first();
- if ($team_order) {
- echo '成功';
- } else {
- echo '不成功,请检查设置是否正确,一定绝对必须要检查清楚!!!!!!如果正确?!那就服务器有问题,非常难受';
- }
- }
- }
- public function fixChangeMemberRelation()
- {
- $member_relation = new MemberRelation();
- $a = [
- [2186, 66]
- ];
- foreach ($a as $item) {
- $member_relation->build($item[0], $item[1]);
- }
- echo 'ok';
- }
- }
|