<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/3/22
 * Time: 下午5:58
 */

namespace App\Modules\Channel\Models;


use Illuminate\Database\Eloquent\Model;

class ChannelQuestionNaire extends Model
{

    protected $table = 'channel_question_naire';
    protected $fillable = ['id', 'channel_user_id', 'reply_grade', 'business_name', 'company_id', 'distribution_manages_id', 'created_at', 'updated_at'];

    /**
     * 添加问卷调查信息
     * @param $item
     * @return mixed
     */
    static function addQuestionNaire($item)
    {
        return self::create($item);
    }

    /**
     * 提交问卷调查
     * @param $id
     * @param $reply_grade
     */
    static function updateInfo($id, $reply_grade, $business_name)
    {
        return self::where('id', $id)->update(['reply_grade' => $reply_grade, 'business_name' => $business_name]);
    }

    /**
     * 获取问卷调查信息
     * @param $param 查询字段
     * @param bool $isAll 是否获取所有
     */
    static function getQuestionNaires($params, $isAll = false)
    {
        $search_obj = self::orderBy('reply_grade', 'desc')->groupBy('business_name');
        $search_obj->where('reply_grade', '<>', '')->where('business_name', '<>', '');

        if (isset($params['business_name'])) {
            $search_obj->where('business_name', $params['business_name']);
        }

        if ($isAll) {
            return $search_obj->get();
        } else {
            return $search_obj->paginate();
        }
    }

    /**
     * 查询是否已经显示 或者 提交
     * @param $channel_user_ids
     * @return mixed
     */
    static function checkQuestionNaire($company_id, $isReply = false)
    {
        $search_obj = self::where('company_id', $company_id);
        $start = date('Y-m-01');
        $end = date('Y-m-d H:i:s');
        if ($isReply) {
            $search_obj->where('reply_grade', '<>', '');
        }
        return $search_obj->whereBetween('updated_at', [$start, $end])->count();
    }

    /**
     * 查询本月这个公司是否已经显示
     * @param $channel_user_ids
     * @return mixed
     */
    static function geFirstQuestionNaire($company_id)
    {
        $search_obj = self::where('company_id', $company_id);
        $start = date('Y-m-01');
        $end = date('Y-m-d H:i:s');
        return $search_obj->whereBetween('updated_at', [$start, $end])->first();
    }

    /**
     * 获取商务名下已经参与的公司数
     * @param $distribution_manages_id
     * @return mixed
     */
    static function getNaireCompanyCount($distribution_manages_id, $isPreMonth = true)
    {
        $search_obj = self::select('company_id')->where('distribution_manages_id', $distribution_manages_id)->where('reply_grade', '<>', '');
        if ($isPreMonth) {
            $begin_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
            $end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
            $search_obj->whereBetween('updated_at', [$begin_time, $end_time]);
        }
        return $search_obj->distinct()->count();
    }

    /**
     * 商务名下的参与调查的公司获的总分数
     * @param $channel_user_ids
     * @return mixed
     */
    static function getTotalGrade($distribution_manages_id, $isPreMonth = true)
    {
        $search_obj = self::where('distribution_manages_id', $distribution_manages_id)->where('reply_grade', '<>', '');
        if ($isPreMonth) {
            $begin_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
            $end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
            $search_obj->whereBetween('updated_at', [$begin_time, $end_time]);
        }
        return $search_obj->sum('reply_grade');
    }
}