FixController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2018/8/8
  6. * Time: 下午6:52
  7. */
  8. namespace app\backend\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\models\Income;
  11. use app\common\models\Order;
  12. use app\common\services\member\MemberRelation;
  13. use Illuminate\Foundation\Bus\DispatchesJobs;
  14. use Illuminate\Support\Facades\DB;
  15. use Yunshop\Commission\models\Agents;
  16. use Yunshop\Commission\models\Commission;
  17. use Yunshop\Commission\models\CommissionOrder;
  18. use Yunshop\Commission\services\CommissionOrderService;
  19. use Yunshop\Mryt\job\UpgradeByRegisterJob;
  20. use Yunshop\Mryt\listeners\MemberRelationEventListener;
  21. use Yunshop\Mryt\services\UpgradeService;
  22. use Yunshop\TeamDividend\models\TeamDividendModel;
  23. class FixController extends BaseController
  24. {
  25. use DispatchesJobs;
  26. public function getAgentUniacid()
  27. {
  28. $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');
  29. echo '该系统有'. count($agents).'条错误数据。';
  30. }
  31. public function agentUniacid()
  32. {
  33. $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');
  34. $success = 0;
  35. $error = 0;
  36. foreach ($agents as $agent) {
  37. $log = DB::table('yz_commission_log')
  38. ->where('uid',$agent['member_id'])
  39. ->where('after_level_id',$agent['agent_level_id'])
  40. ->orderBy('id','desc')
  41. ->first();
  42. $this->fixAgentUniacid($agent, $log['before_level_id']);
  43. $success ++;
  44. }
  45. echo '成功:'. $success.'。失败:'.$error;
  46. }
  47. public function fixAgentUniacid($agent, $level_id)
  48. {
  49. if ($level_id == 0) {
  50. DB::table('yz_agents')->where('member_id', $agent['member_id'])->update(['agent_level_id' => 0]);
  51. } else {
  52. $level = DB::table('yz_agent_level')->where('id',$level_id)->first();
  53. if ($agent['uniacid'] == $level['uniacid']) {
  54. DB::table('yz_agents')->where('member_id', $agent['member_id'])->update(['agent_level_id' => $level_id,'updated_at' => time()]);
  55. } else {
  56. $log = DB::table('yz_commission_log')
  57. ->where('uid',$agent['member_id'])
  58. ->where('after_level_id',$level_id)
  59. ->orderBy('id','desc')
  60. ->first();
  61. $this->fixAgentUniacid($agent, $log['before_level_id']);
  62. }
  63. }
  64. }
  65. public function errorDividendData()
  66. {
  67. $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'])
  68. ->join('yz_order', 'yz_order.order_sn', '=', 'yz_team_dividend.order_sn')
  69. ->join('mc_members', 'mc_members.uid', '=', 'yz_team_dividend.member_id')
  70. ->where(DB::raw('ims_mc_members.uniacid != ims_yz_team_dividend.uniacid'))
  71. ->orderBy('yz_team_dividend.id', 'asc')
  72. ->get();
  73. dump($a);
  74. }
  75. public function handleCommissionOrder()
  76. {
  77. $handle = 0;
  78. $success = 0;
  79. $waitCommissionOrder = CommissionOrder::uniacid()->whereStatus(0)->get();
  80. $status = \Setting::get('plugin.commission')['settlement_event'] ? 1 : 3;
  81. if (!$waitCommissionOrder->isEmpty()) {
  82. foreach ($waitCommissionOrder as $key => $commissionOrder) {
  83. $orderModel = Order::uniacid()->whereId($commissionOrder->ordertable_id)->first();
  84. if ($orderModel->status >= $status) {
  85. $handle += 1;
  86. $commissionOrder->status = 1;
  87. if ($status == 1) {
  88. $commissionOrder->recrive_at = strtotime($orderModel->pay_time);
  89. } else {
  90. $commissionOrder->recrive_at = strtotime($orderModel->finish_time);
  91. }
  92. if ($commissionOrder->save()) {
  93. $success += 1;
  94. }
  95. }
  96. unset($orderModel);
  97. }
  98. }
  99. echo "分销订单未结算总数:{$waitCommissionOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
  100. }
  101. public function fixTeam()
  102. {
  103. $search_date = strtotime('2018-10-25 12:00:00');
  104. $error = [];
  105. $tmp = [];
  106. $pos = [];
  107. $res = DB::table('yz_team_dividend as t')
  108. ->select(['t.id' , 'o.id as orderid', 'o.uid', 't.order_sn', 't.member_id', 't.status'])
  109. ->join('yz_order as o', 'o.order_sn', '=', 't.order_sn')
  110. ->where('t.created_at', '>', $search_date)
  111. ->orderBy('t.id', 'asc')
  112. ->get();
  113. if (!$res->isEmpty()) {
  114. foreach ($res as $key => $rows) {
  115. if (!$tmp[$rows['orderid']]) {
  116. // $pos = [$rows->member_id => $key];
  117. $tmp[$rows['orderid']] = [
  118. 'id' => $rows['id'],
  119. 'order_id' => $rows['orderid'],
  120. 'uid' => $rows['uid'],
  121. 'order_sn' => $rows['order_sn'],
  122. 'parent_id' => $rows['member_id'],
  123. 'status' => $rows['status'],
  124. ];
  125. file_put_contents(storage_path('logs/team_fix.log'), print_r($tmp, 1), FILE_APPEND);
  126. } else {
  127. // $k = $pos[$rows->member_id];
  128. // $tmp[$k]['member_id'][] = $rows->member_id;
  129. }
  130. }
  131. }
  132. //订单会员->关系链 不匹配
  133. foreach ($tmp as $k => $v) {
  134. $total = DB::table('yz_member')
  135. ->where('member_id', '=', $v['uid'])
  136. ->where('parent_id', '=', $v['parent_id'])
  137. ->count();
  138. if (0 == $total) {
  139. $error[] = $v;
  140. file_put_contents(storage_path('logs/team_fix_error.log'), print_r($v, 1), FILE_APPEND);
  141. }
  142. }
  143. collect($error)->each(function ($item) {
  144. if (0 == $item['status']) {
  145. $model = Order::find($item['order_id']);
  146. if (!is_null($model)) {
  147. DB::transaction(function () use ($item, $model) {
  148. DB::table('yz_team_dividend')
  149. ->where('order_sn', '=', $item['order_sn'])
  150. ->delete();
  151. DB::table('yz_order_plugin_bonus')
  152. ->where('order_id', '=', $item['order_id'])
  153. ->where('table_name', '=', 'yz_team_dividend')
  154. ->delete();
  155. (new \Yunshop\TeamDividend\Listener\OrderCreatedListener)->fixOrder($model);
  156. file_put_contents(storage_path('logs/team_fix_del.log'), print_r($item, 1), FILE_APPEND);
  157. });
  158. }
  159. }
  160. });
  161. echo '数据修复ok';
  162. }
  163. public function fixArea()
  164. {
  165. $search_date = strtotime('2018-10-25 12:00:00');
  166. $error = [];
  167. $tmp = [];
  168. $res = DB::table('yz_area_dividend as t')
  169. ->select(['t.id' , 'o.id as orderid', 'o.uid', 't.order_sn', 't.member_id', 't.status'])
  170. ->join('yz_order as o', 'o.order_sn', '=', 't.order_sn')
  171. ->where('t.created_at', '>', $search_date)
  172. ->orderBy('t.id', 'asc')
  173. ->get();
  174. if (!$res->isEmpty()) {
  175. foreach ($res as $key => $rows) {
  176. if (!$tmp[$rows['orderid']]) {
  177. // $pos = [$rows->member_id => $key];
  178. $tmp[$rows['orderid']] = [
  179. 'id' => $rows['id'],
  180. 'order_id' => $rows['orderid'],
  181. 'uid' => $rows['uid'],
  182. 'order_sn' => $rows['order_sn'],
  183. 'parent_id' => $rows['member_id'],
  184. 'status' => $rows['status'],
  185. ];
  186. file_put_contents(storage_path('logs/area_fix.log'), print_r($tmp, 1), FILE_APPEND);
  187. }
  188. }
  189. }
  190. // //订单会员->关系链 不匹配
  191. // foreach ($tmp as $k => $v) {
  192. // $total = DB::table('yz_member')
  193. // ->where('member_id', '=', $v['uid'])
  194. // ->where('parent_id', '=', $v['parent_id'])
  195. // ->count();
  196. //
  197. // if (0 == $total) {
  198. // $error[] = $v;
  199. //
  200. // file_put_contents(storage_path('logs/area_fix_error.log'), print_r($v, 1), FILE_APPEND);
  201. // }
  202. // }
  203. collect($tmp)->each(function ($item) {
  204. if (0 == $item['status']) {
  205. $model = Order::find($item['order_id']);
  206. DB::transaction(function () use ($item, $model) {
  207. DB::table('yz_area_dividend')
  208. ->where('order_sn', '=', $item['order_sn'])
  209. ->delete();
  210. DB::table('yz_order_plugin_bonus')
  211. ->where('order_id', '=', $item['order_id'])
  212. ->where('table_name', '=', 'yz_area_dividend')
  213. ->delete();
  214. (new \Yunshop\AreaDividend\Listener\OrderCreatedListener)->fixOrder($model);
  215. file_put_contents(storage_path('logs/area_fix_del.log'), print_r($item, 1), FILE_APPEND);
  216. });
  217. }
  218. });
  219. echo '数据修复ok';
  220. }
  221. public function fixIncome()
  222. {
  223. $count = 0;
  224. $income = Income::whereBetween('created_at', [1539792000,1541433600])->get();
  225. foreach ($income as $value) {
  226. $pattern1 = '/\\\u[\d|\w]{4}/';
  227. preg_match($pattern1, $value->detail, $exists);
  228. if (empty($exists)) {
  229. $pattern2 = '/(u[\d|\w]{4})/';
  230. $value->detail = preg_replace($pattern2, '\\\$1', $value->detail);
  231. $value->save();
  232. $count++;
  233. }
  234. }
  235. echo "修复了{$count}条";
  236. }
  237. public function mr()
  238. {
  239. $m = new MemberRelationEventListener();
  240. $parent_id = 6080;
  241. $m->fixRelation($parent_id);
  242. }
  243. public function ma()
  244. {
  245. $m = new MemberRelationEventListener();
  246. $parent_id = 6080;
  247. $m->fixAward($parent_id);
  248. }
  249. public function changeField()
  250. {
  251. $sql1 = 'ALTER TABLE `' . DB::getTablePrefix() . 'users_permission` MODIFY `modules` text NULL';
  252. $sql2 = 'ALTER TABLE `' . DB::getTablePrefix() . 'users_permission` MODIFY `templates` text NULL';
  253. try {
  254. DB::select($sql1);
  255. DB::select($sql2);
  256. echo '数据已修复';
  257. } catch (\Exception $e) {
  258. echo $e->getMessage();
  259. }
  260. }
  261. public function handleTeamOrder()
  262. {
  263. $handle = 0;
  264. $success = 0;
  265. $status = \Setting::get('plugin.team_dividend')['settlement_event'] ? 1 : 3;
  266. $waitTeamOrder = \Yunshop\TeamDividend\models\TeamDividendModel::uniacid()->whereStatus(0)->get();
  267. if (!$waitTeamOrder->isEmpty()) {
  268. foreach ($waitTeamOrder as $key => $teamOrder) {
  269. $orderModel = \app\common\models\Order::uniacid()->where('order_sn',$teamOrder->order_sn)->first();
  270. if ($orderModel->status >= $status) {
  271. $handle += 1;
  272. $teamOrder->recrive_at = strtotime($orderModel->pay_time);
  273. $teamOrder->is_pay = 1;
  274. if ($teamOrder->save()) {
  275. $success += 1;
  276. }
  277. }
  278. unset($orderModel);
  279. }
  280. }
  281. echo "经销商订单未结算总数:{$waitTeamOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
  282. }
  283. public function handleAreaOrder()
  284. {
  285. $handle = 0;
  286. $success = 0;
  287. $waitTeamOrder = \Yunshop\AreaDividend\models\AreaDividend::uniacid()->whereStatus(0)->get();
  288. if (!$waitTeamOrder->isEmpty()) {
  289. foreach ($waitTeamOrder as $key => $teamOrder) {
  290. $orderModel = \app\common\models\Order::uniacid()->where('order_sn',$teamOrder->order_sn)->first();
  291. if ($orderModel->status == 3) {
  292. $handle += 1;
  293. $teamOrder->recrive_at = strtotime($orderModel->finish_time);
  294. if ($teamOrder->save()) {
  295. $success += 1;
  296. }
  297. }
  298. unset($orderModel);
  299. }
  300. }
  301. echo "区域分红订单未结算总数:{$waitTeamOrder->count()},已完成订单数:{$handle}, 执行成功数:{$success}";
  302. }
  303. public function fixCommissionAmount()
  304. {
  305. $CommissionOrder = CommissionOrder::uniacid()->whereNull('commission_amount')->get();
  306. $set = \Setting::get('plugin.commission');
  307. $count = 0;
  308. foreach ($CommissionOrder as $commission) {
  309. $orderModel = Order::find($commission->ordertable_id);
  310. $orderGoods = $orderModel->hasManyOrderGoods;
  311. $commissionAmount = 0;
  312. $formula = '';
  313. foreach ($orderGoods as $key => $og) {
  314. //获取商品分销设置信息
  315. $commissionGoods = Commission::getGoodsById($og->goods_id)->first();
  316. if (!$commissionGoods->is_commission) {
  317. continue;
  318. }
  319. if ($commissionGoods) {
  320. if ($commissionGoods['has_commission'] == '1') {
  321. //商品独立佣金
  322. $commissionAmount += $og['payment_amount']; //分佣计算金额
  323. $formula .= "+商品独立佣金";//分佣计算方式
  324. } else {
  325. $countAmount = CommissionOrderService::getCountAmount($orderModel, $og, '', $set);
  326. $commissionAmount += $countAmount['amount'];
  327. $formula = $countAmount['method'];
  328. }
  329. }
  330. }
  331. $commission->commission_amount = $commissionAmount;
  332. $commission->formula = $formula;
  333. $commission->save();
  334. $count++;
  335. }
  336. echo '修改了'.$count.'条信息';
  337. }
  338. public function fixNotCommission()
  339. {
  340. $order_sn = \YunShop::request()->order_sn;
  341. $order = Order::uniacid()->where('order_sn', $order_sn)->first();
  342. $commission_order = CommissionOrder::uniacid()->where('ordertable_id', $order->id)->first();
  343. if ($commission_order) {
  344. echo '已有这条分红';
  345. } else {
  346. $result = (new \Yunshop\Commission\Listener\OrderCreatedListener())->handler($order);
  347. $commission_order = CommissionOrder::uniacid()->where('ordertable_id', $order->id)->first();
  348. if ($commission_order) {
  349. echo '成功';
  350. } else {
  351. echo '不成功,请检查设置是否正确,一定绝对必须要检查清楚!!!!!!如果正确?!那就服务器有问题,非常难受';
  352. }
  353. }
  354. }
  355. public function fixNotTeam()
  356. {
  357. $order_sn = \YunShop::request()->order_sn;
  358. $order = Order::uniacid()->where('order_sn', $order_sn)->first();
  359. $team_order = TeamDividendModel::uniacid()->where('order_sn', $order_sn)->first();
  360. if ($team_order) {
  361. echo '已有这条分红';
  362. } else {
  363. (new \Yunshop\TeamDividend\Listener\OrderCreatedListener())->handle($order);
  364. $team_order = TeamDividendModel::uniacid()->where('order_sn', $order_sn)->first();
  365. if ($team_order) {
  366. echo '成功';
  367. } else {
  368. echo '不成功,请检查设置是否正确,一定绝对必须要检查清楚!!!!!!如果正确?!那就服务器有问题,非常难受';
  369. }
  370. }
  371. }
  372. public function fixChangeMemberRelation()
  373. {
  374. $member_relation = new MemberRelation();
  375. $a = [
  376. [2186, 66]
  377. ];
  378. foreach ($a as $item) {
  379. $member_relation->build($item[0], $item[1]);
  380. }
  381. echo 'ok';
  382. }
  383. }