Event.php 6.72 KB
<?php

namespace addons\antis\controller;

use addons\antis\model\Event as EventModel;
use addons\antis\model\File;
use addons\antis\model\Form;
use addons\antis\model\StaffSignIn;
use addons\antis\model\Message;
use addons\antis\model\Staff;
use think\Db;
use think\Exception;

/**
 * 日程接口
 */
class Event extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    //获取日程
    public function getEvent()
    {
        //开始时间
        $start_day = input('start_day');
        //结束时间
        $end_day = input('end_day');;
        if (empty($start_day) || empty($end_day)) {
            $this->error('开始时间或结束时间不能为空');
        }
        if ($end_day < $start_day) {
            $this->error('开始时间不能小于结束时间');
        }
        $this->success('请求成功', EventModel::getTimeList($start_day, $end_day));
    }


    //添加日程
    public function addEvent()
    {
        $params = $this->request->post();
        // 表单验证
        if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        Db::startTrans();
        try {
            $params['type'] = 1;//日程
            $result = EventModel::createEvent($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('添加日程成功');
        }
    }


    //变更状态
    public function changeStatus()
    {
        $id = input('id');
        $status = input('status', 0);
        if (empty($id)) {
            $this->error('参数错误');
        }
        Db::startTrans();
        try {
            EventModel::changeStatus($id, $status);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('变更成功');
    }


    //
    public function getList()
    {
        $relation_type = input('relation_type', '', 'intval');// 1客户 2联系人 3合同 4线索
        $relation_id = input('relation_id', '', 'intval');
        $limit = input("limit/d", 10);
        $where = [];
        $where['relation_type'] = $relation_type;
        $where['relation_id'] = $relation_id;
        $where['staff_id'] = $this->auth->id;
        $records = EventModel::where($where)->with(['staff'])->field('id,staff_id,type,title,start_time,end_time,status,auto_end,level,remindtype,remark,color,createtime')->order('id desc')->paginate($limit);
        $this->success('请求成功', $records);
    }


    //添加签到
    public function addSign()
    {
        $params = $this->request->post();
        $location = input('location');
        $lng = input('lng');
        $lat = input('lat');
        $files = input('file_ids');
        $customer_id = input('customer_id');
        $content = input('content');
        if (empty($lng) || empty($lat)) {
            $this->error('地理位置不能为空');
        }

        //自定义字段
        $other = [];
        foreach ($params as $name => $val) {
            if (strstr( $name,'other_') !== false) {
                if(is_array($val)){
                    $other[$name] = implode(',',$val);
                }else{
                    $other[$name] = $val;
                }
                unset($params[$name]);
            }
        }

        $data = [
            'location' => $location,
            'lng' => $lng,
            'lat' => $lat,
            'file_ids' => $files,
            'customer_id' => $customer_id,
            'staff_id' => $this->auth->id,
            'other' => json_encode($other, JSON_UNESCAPED_UNICODE)
        ];
        $model = new StaffSignIn();
        if ($result = $model->save($data)) {
            $lastId = $model->getLastInsID();
            //通知上级
            Message::addMessage(Message::SIGN_TYPE, $lastId, $this->auth->parent_id, $this->auth->id);
            $this->success('签到成功');
        }
        $this->error('签到失败');
    }


    //获取签到信息
    public function getSign()
    {
        $customer_id = input('customer_id', 0, 'intval');
        $limit = input("limit/d", 10);
        $where = [];
        $type       = input('type', 0);// 0 全部 1 我创建 2 下属创建

        if ($type == 1) {//我的客户
            $where['staff_id'] = $this->auth->id;
        } elseif ($type == 2) {//下属负责的客户
            $where['staff_id'] = ['in', Staff::getLowerStaffId()];
        }else{
            $where['staff_id'] = ['in', Staff::getMyStaffIds()];
        }
        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        $staffSign = StaffSignIn::where($where)->with(['staff', 'customer'])
            ->order('id desc')->paginate($limit);
        //标记通知已读
        Message::setRead(Message::SIGN_TYPE,0, $this->auth->id);
        $this->success('请求成功', $staffSign);
    }

    /**
     * 获取签到详情
     */
    public function getSignDetail(){
        $id = input('id', 0, 'intval');

        $staffSign = StaffSignIn::where(['id'=>$id])->with(['staff', 'customer'])->find();
        if(empty($staffSign)){
            $this->error('数据不存在');
        }
        if($staffSign['other']){
            $other = json_decode($staffSign['other'],true);
            $form = Form::getDataValue('signin');
            foreach($form as $k=>$v){
                if($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile'){
                    $other[$v['id'].'_str'] = '';
                    if(key_exists($v['id'],$other)){
                        if(isset($other[$v['id']]) && $other[$v['id']]){
                            $whereT['id'] = array('in',$other[$v['id']]);
                            $fileinfo = File::where($whereT)->field('id,name,file_path,save_name')->select();
                            if($fileinfo){
                                $other[$v['id']] = $fileinfo;
                                $fileinfodata = '';
                                foreach($fileinfo as $kss=>$vss){
                                    $fileinfodata = $vss['save_name'].','.$fileinfodata;
                                }
                                $other[$v['id'].'_str'] = rtrim($fileinfodata,',');
                            }
                        }
                    }
                }
            }
            $staffSign['other'] = $other;
        }
        $this->success('请求成功',$staffSign);
    }
}