<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class CompanyComment extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = Db::name('company_comment');
    }

    /**
     * 列表
     * @return void
     */
    public function getList()
    {
        try {
            $companyId = $this->request->param('company_id',0);
            $companyId = !empty($companyId) ? $companyId : $this->auth->company_id;
            $cc = 'company_comment';
            $u = 'user';
            $field = $cc.'.id,'.$cc.'.user_id,info,images,'.$cc.'.createtime,'.$u.'.avatar,'.$u.'.nickname';
            $where[$cc.'.company_id'] = $companyId;

            $result = $this->model->alias($cc)->field($field)
                ->join($u,$u.'.id = '.$cc.'.user_id','LEFT')
                ->where($where)->order($cc.'.createtime desc')->autopage()->select();
            if (!empty($result)) {
                foreach ($result as $key => &$value) {
                    empty($value['info']) && $value['info'] = '此用户没有填写评价。';
                    $value['createtime'] = !empty($value['createtime']) ? date('m月d日',$value['createtime']) : '';
                    unset($value['user_id']);
                }
                $result = list_domain_image($result,['images','avatar']);
            }

            $this->success('获取成功',$result);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 保存
     * @return void
     */
    public function save()
    {
        try {
            //验证参数
            $id = $this->request->param('id',0);
            $companyId = $this->request->param('company_id',0);
            $companyId = !empty($companyId) ? $companyId : $this->auth->company_id;
            $info = $this->request->param('info','');
            $images = $this->request->param('images','');
            if (empty($info) && $images) {
                throw new Exception('参数有误');
            }
            $userId = $this->auth->id;
            $scene = !empty($id) ? 'edit' : 'add';
            $validate = validate('CompanyComment');
            if(!$validate->check($this->request->param(),[],$scene)){
                throw new Exception($validate->getError());
            }
            $time = time();
            $data = [
                'info' => $info,
                'images'  => $images,
            ];
            $limitCreatetime = $time - 60;
            $commentWhere['user_id'] = $userId;
            $commentWhere['company_id'] = $companyId;
            $commentWhere['createtime'] = ['gt',$limitCreatetime];
            $commentData = $this->model->where($commentWhere)->find();
            if (!empty($commentData) && empty($id)) {
                throw new Exception('请不要频繁操作');
            }
            if (empty($id)) {
                $data['company_id'] = $companyId;
                $data['user_id'] = $userId;
                $data['createtime'] = $time;
                $res = $this->model->insertGetId($data);
            } else {
                $where['id'] = $id;
                $where['user_id'] = $userId;
                $res = $this->model->where($where)->update($data);
            }
            if (!$res) {
                throw new Exception('操作失败');
            }
            $this->success('操作成功');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}