Follow.php 4.9 KB
<?php

namespace app\admin\controller\qywx;

use addons\qyexternal\model\Corp;
use addons\qyexternal\model\UserBehavior;
use think\Model;
use addons\qyexternal\model\FollowUser;
use app\common\controller\Backend;
use Weasy\Core\Sync;

/**
 * 企业成员管理
 *
 * @icon fa fa-circle-o
 */
class Follow extends Backend
{
    /**
     * @var null | Model
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new FollowUser();
    }

    /**
     * 发送信息
     */
    public function sendmsg($ids=null)
    {
        if ($this->request->isPost()) {
           $params = $this->request->post();
           $qy = new  \app\api\controller\Qywx();
           $qy->sendmsg($params['corp_id'],$params['app_id'],$params['userid'],$params['content']);
           $this->success('发送成功');
        }
        $user = $this->model::get($ids);
        
     
        $corp = \think\Db::name('qywx_corp')->where('corp_id',$user->corp_id)->find();
        $corp['user'] = $user->userid;
        $this->view->assign('corp',$corp);
        return $this->view->fetch();
    }
    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function syncContact($ids)
    {
        $follow = $this->model->get($ids);

        $corp = Corp::where("corp_id", $follow["corp_id"])->find();

        $sync = new Sync($corp);

        $sync->ContactsList($follow->userid);

        return $this->success("同步联系人成功...");
    }

    /**
     * 数据统计
     * @throws \Weasy\External\Utils\Error\ParameterError
     */
    public function chart($ids)
    {
        $follow = $this->model->get($ids);

        $today = strtotime(date("Y-m-d"));

        $end_time = $today-86400;
        $start_time = $end_time - 86400 * 29;

        // 是否在访问时同步
        if (get_addon_config("qyexternal")["sync"]) {
            $last = UserBehavior::where("corp_id", $follow->corp_id)
                ->where("userid", $follow->userid)
                ->order("date", "desc")
                ->find();

            // 数据同步,自动同步30天数据
            if ($last["date"] < $today) {
                $corp = \addons\qyexternal\model\Corp::where("corp_id", $follow->corp_id)->find();
                $sync = new Sync($corp);
                $sync->UserBehaviorData($follow->userid, $start_time, $end_time);
            }
        }

        $month = UserBehavior::where("corp_id", $follow->corp_id)
            ->where("userid", $follow->userid)
            ->whereBetween("date", [$start_time, $end_time])
            ->select();

        $data_month = [];
        $count = [
            "new_apply_cnt" => 0,
            "new_contact_cnt" => 0,
            "negative_feedback_cnt" => 0,
            "chat_cnt" => 0,
            "message_cnt" => 0,
            "reply_percentage" => 0,
            "avg_reply_time" => 0,
        ];
        $month_count = $count;
        foreach ($month as $item) {
            $data_month["x"][$item["date"]] = date("m-d", $item["date"]);
            foreach ($item->toArray() as $key=>$val) {
                $data_month[$key]["y"][] = $val;
                if (isset($month_count[$key])) {
                    $month_count[$key] += $val;
                }
            }
        }

        $data_month["x"] = array_values($data_month["x"]);
        $data_month["count"] = $month_count;

        if (!empty($data_month)) {
            $this->assignconfig('month', $data_month);
        }

        $end_time = time()-86400;
        $start_time = $end_time - 86400 * 7;

        $week = UserBehavior::where("corp_id", $follow->corp_id)
            ->where("userid", $follow->userid)
            ->whereBetween("date", [$start_time, $end_time])
            ->select();

        $data_week = [];
        $week_count = $count;
        foreach ($week as $item) {
            $data_week["x"][$item["date"]] = date("m-d", $item["date"]);
            foreach ($item->toArray() as $key=>$val) {
                $data_week[$key]["y"][] = $val;
                if (isset($week_count[$key])) {
                    $week_count[$key] += $val;
                }
            }
        }

        $data_week["x"] = array_values($data_week["x"]);
        $data_week["count"] = $week_count;

        if (!empty($data_week)) {
            $this->assignconfig('week', $data_week);
        }

        return $this->view->fetch();
    }
}