<?php

namespace App\Console\Commands;

use App\Model\SystemBulletinModel;
use App\Model\SystemUpdateModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

//TODO 上线之后去除注释这个notice ,5。0版本的缺陷 https://blog.csdn.net/weixin_44251615/article/details/93710829
ini_set("error_reporting","E_ALL & ~E_NOTICE");
class CreateNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'notice:create_notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成每日六点的公告';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //查找出今日的所有更新
        $date = date('Y-m-d');

        $where['begin_time'] = $date;
        $where['end_time'] = date('Y-m-d H:i:s');

        $systemUpdateModel = new SystemUpdateModel();

        //韦伯系统是所有的,通知所有人
        $allUpdate = $systemUpdateModel->getWhereObj($where)->get()->toArray();
        if (!empty($allUpdate)){
            // 查找所有通知用户邮箱和ID
            $sendUser = DB::table('t_user_perm')->where('begDate','<=',$date)
                ->where('endDate','>=',$date)
                ->select('username','userId')->get();

            $this->createNoticeSaveData($allUpdate,$date,'韦伯系统',json_encode($sendUser));
        }

        //查找所有需要更新的子系统,通知对应系统的人
        $allGroup = $systemUpdateModel->getWhereObj($where)->groupBy('system_id')->get()->toArray();
        foreach ($allGroup as $value){

            // 查找所有通知ID
            $sendUser = DB::table('t_user_perm')->where('begDate','<=',$date)
                ->where('endDate','>=',$date)
                ->where('bid',$value['system_id'])
                ->select('username','userId')->get();

            $allUpdate = $systemUpdateModel->getWhereObj($where)->where('system_id',$value['system_id'])->get()->toArray();

            $this->createNoticeSaveData($allUpdate,$date,$value['system_name'],json_encode($sendUser));
        }
    }

    public function createNoticeSaveData($allUpdate,$date,$systemName,$sendUser)
    {

        $todayTime = strtotime($date);
        $saveData['title'] = $date.'系统不停机维护公告';
        $saveData['is_send'] = '未发送';
        $saveData['create_time'] = $todayTime;
        $saveData['send_user'] = $sendUser;
        $saveData['system_name'] = $systemName;


        $tbodyContent = '';
        foreach ($allUpdate as $value){
            $tbodyContent .=
                '<tr>'.
                '<td>'.$date.'</td>'.
                '<td>'.$value['system_name'].'</td>'.
                '<td>'.$value['update_type'].'</td>'.
                '<td>'.$value['update_title'].'</td>'.
                '<td>'.$value['update_content'].'</td>'.
                '<td>'.$value['product_user'].'</td>'.
                '<td>'.$value['version_num'].'</td>'.
                '</tr>';

        }


        $saveData['content'] = '
<h2>猎芯的同事们好</h2>
<h2>以下内容为'.$systemName.'今天全部的更新,请查收</h2>
<table border="1" cellspacing="0" style="width: 1300px;line-height: 50px;table-layout: fixed">
<thead style="background:grey;font-size: 30px;">
<tr>
<td></td>
<td>系统</td>
<td>更新类型</td>
<td>更新标题</td>
<td width="350">更新内容简介</td>
<td>产品负责人</td>
<td>版本号</td>
</tr>
</thead>
<tbody>
'.$tbodyContent.'
</tbody>
</table>
';
        $systemBulletinModel = new SystemBulletinModel();
        if($smbnId = $systemBulletinModel->where('create_time',$todayTime)->where('system_name',$systemName)->value('smbn_id')){
            $systemBulletinModel->where('smbn_id',$smbnId)->update($saveData);
        }else{
            $systemBulletinModel->insertGetId($saveData);
        }
    }
}