123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2019/9/25
- * Time: 17:53
- */
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Log;
- use think\Request;
- class FlushBookCollectSum extends Command
- {
- protected function configure()
- {
- $this->setName('FlushBookCollectSum')->setDescription('更正BookCollect数据汇总');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- model("AuthGroupAccess")->where('group_id', 'in', ['3', '4'])->chunk(100, function ($rows) use($output) {
- if (count($rows) > 0) {
- $time = strtotime(date("2019-09-25"));
- foreach ($rows as $row) {
- $admin_id = $row['uid'];
- $maps = [
- 'admin_id' => ['=', $admin_id],
- 'state' => ['=', '1'],
- 'deduct' => ['=', 0],
- 'book_id' => ['>', 0],
- 'finishtime' => ['<=', $time],
- ];
- $bookRechargeRows = model("Orders")->field("book_id, sum(money) as recharge_money, count(1) as recharge_num")->where($maps)->group('book_id')->select();
- if ($bookRechargeRows) {
- foreach ($bookRechargeRows as $bookRechargeRow) {
- //存在更新 不存在插入?
- $bookId = $bookRechargeRow['book_id'];
- $rechargeMoney = $bookRechargeRow['recharge_money'];
- $rechargeNum = $bookRechargeRow['recharge_num'];
- $collectRow = model("BookCollect")->where('admin_id', 'eq', $admin_id)->where('type', 'eq', '3')->where('book_id', 'eq', $bookId)->find();
- if ($collectRow) {
- //更新
- model('BookCollect')->update(['recharge_num' => $rechargeNum, 'recharge_money' => $rechargeMoney], ['id' => $collectRow['id']]);
- $output->info('BookCollect update sql success');
- } else {
- //插入
- $insert_data = [
- 'type' => '3',
- 'admin_id' => $admin_id,
- 'book_id' => $bookId,
- 'createdate' => '20180101',
- 'recharge_money' => $rechargeMoney,
- 'recharge_num' => $rechargeNum,
- 'createtime' => time(),
- 'updatetime' => time()
- ];
- if ($is_insert = model('BookCollect')->insert($insert_data)) {
- $output->info('BookCollect insert sql success');
- } else {
- $output->error('BookCollect insert sql failed');
- }
- }
- }
- }
- $output->write("admin_id:{$admin_id} 完事了");
- }
- } else {
- return false;
- }
- }, 'uid');
- $output->write("脚本执行结束");
- }
- }
|