<?php
namespace app\cron;

use app\common\util\AppMsg;
use think\Db;
use yunwuxin\cron\Task;

class MateReport extends Task
{
    public function configure()
    {
        $this->monthlyOn(25); // 每月25号0:00执行一次
    }

    /**
     * 执行任务
     * @return mixed
     */
    protected function execute()
    {
        try {
            $this->remind();
        } catch (\Exception $e) {
            trace($e->getMessage());
        }
    }


    public function remind(){
        $sday = date('Y-m', strtotime('last month')).'-25 ';
        $starttime = $sday." 00:00:00";
        $eday = date('Y-m').'-25 ';
        $endtime = $eday."00:00:00";
        $orgs = Db::name("org")->where('type',2)->where('enable',1)->where('del',0)->select();
        foreach($orgs as $k=>$v){
            $goods = Db::name('mate_goods')
                ->where('org_id',$v['id'])
                ->where('del',0)
                ->where('enable',1)
                ->field('id,nums')
                ->select();
            if(!$goods){ // 无商品不生成记录
                continue;
            }
            $innums = 0;
            $outnums = 0;
            $usenums = 0;
            $cznums = 0;
            $nums = 0;

            $reportGoods = [];
            foreach ($goods as $kk=>$vv){
                $innums1 = Db::name('mate_apply_goods')
                    ->alias('a')
                    ->join('mate_apply b','a.apply_id = b.id')
                    ->where('b.del',0)
                    ->where('b.type',1)
                    ->where('b.org_id',$v['id'])
                    ->where('a.goods_id',$vv['id'])
                    ->where('b.create_time','>=',$starttime)
                    ->where('b.create_time','<',$endtime)
                    ->sum('a.nums');
                $innums += $innums1;
                $outnums1 = Db::name('mate_apply_goods')
                    ->alias('a')
                    ->join('mate_apply b','a.apply_id = b.id')
                    ->where('b.del',0)
                    ->where('b.type',2)
                    ->where('b.org_id',$v['id'])
                    ->where('a.goods_id',$vv['id'])
                    ->where('b.create_time','>=',$starttime)
                    ->where('b.create_time','<',$endtime)
                    ->sum('a.nums');
                $outnums += $outnums1;

                $usenums1 = Db::name('todo_mate_item')
                    ->alias('a')
                    ->join('todo_mate b','a.todo_mate_id = b.id')
                    ->where('b.org_id',$v['id'])
                    ->where('a.items_id',$vv['id'])
                    ->where('b.create_time','>=',$starttime)
                    ->where('b.create_time','<',$endtime)
                    ->sum('a.total');
                $usenums += $usenums1;

                $cznums1 = Db::name('mate_goods_log')
                    ->where('type',1)
                    ->where('org_id',$v['id'])
                    ->where('goods_id',$vv['id'])
                    ->where('create_time','>=',$starttime)
                    ->where('create_time','<',$endtime)
                    ->sum('nums');
                $cznums += $cznums1;

                $nums += $vv['nums'];

                $reportGoods[] = [
                    'org_id' => $v['id'],
                    'goods_id' => $vv['id'],
                    'in_nums' => $innums1,
                    'out_nums' => $outnums1,
                    'use_nums' => $usenums1,
                    'cz_nums' => $cznums1,
                    'nums' => $vv['nums'],
                ];
            }

            Db::startTrans();
            try{
                $reportId = Db::name("mate_report")->insertGetId([
                    'org_id' => $v['id'],
                    'start_time' => $sday,
                    'end_time' => $eday,
                    'in_nums' => $innums,
                    'out_nums' => $outnums,
                    'use_nums' => $usenums,
                    'cz_nums' => $cznums,
                    'nums' => $nums,
                    'create_time' => date("Y-m-d H:i:s")
                ]);

                $arr = [];
                foreach ($reportGoods as $kk=>$vv){
                    $vv['report_id'] = $reportId;
                    $arr[] = $vv;
                    if(count($arr) == 200){
                        $res = Db::name("mate_report_goods")->insertAll($arr);
                        if($res != count($arr)){
                            exception("操作失败");
                        }
                        $arr = [];
                    }
                }
                if(count($arr) > 0){
                    $res = Db::name("mate_report_goods")->insertAll($arr);
                    if($res != count($arr)){
                        exception("操作失败");
                    }
                }

                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
            }
        }
    }



}