Blame view

addons/antis/model/Daily.php 3.27 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  <?php
  
  namespace addons\antis\model;
  
  use think\Exception;
  use think\Model;
  use traits\model\SoftDelete;
  use addons\antis\model\DailyDraft;
  /**
   *工作报告
   */
  class Daily Extends Model {
  	use SoftDelete;
      // 表名,不含前缀
      protected $name = 'qingdong_daily';
  	// 开启自动写入时间戳字段
  	protected $autoWriteTimestamp = 'int';
  	// 定义时间戳字段名
  	protected $createTime = 'createtime';
  	protected $updateTime = 'updatetime';
  	protected $deleteTime = 'deletetime';
  
  	//获取创建时间
  	public function getCreatetimeAttr($value){
  		return date('Y-m-d H:i',$value);
  	}
      public function getOtherAttr($value){
          return json_decode($value,true);
      }
  	//销售
  	public function staff() {
  		return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,img,name,post');
  	}
  
  	//已读
  	public function read(){
  		return $this->hasMany(DailyRead::class, 'daily_id', 'id')->field('daily_id,createtime');
  	}
  	/**
  	 * @desc 备注
  	 * @update_date 2021/6/9 更新时间
  	 * @author 瑄客CRM
  	 */
  	public static function createDaily($params) {
  		//自定义字段
  		$other = [];
  		foreach ($params as $name => $val) {
  			if (strstr( $name,'other_') !== false || strstr( $name,'main_') !== false) {
                  if(is_array($val)){
                      $other[$name] = implode(',',$val);
                  }else{
                      $other[$name] = $val;
                  }
  				unset($params[$name]);
  			}
  		}
  		$params['other']=json_encode($other);
  		$model = new self;
  		// 调用当前模型对应的User验证器类进行数据验证
  		$result = $model->allowField(true)->save($params);
  		if (false === $result) {
  			// 验证失败 输出错误信息
  			throw new Exception($model->getError());
  		}
  		$lastId=$model->getLastInsID();
  
  		if (isset($params['reminds_id'])) {//发送通知
  			$staff_ids = explode(',', $params['reminds_id']);
  			foreach ($staff_ids as $staff_id) {
  				//发送通知
  				Message::addMessage(Message::DAILY_TYPE, $lastId, $staff_id, $params['create_staff_id']);
  			}
  		}
  		return $lastId;
  	}
  
      /**
       * 修改
       * @param $params
       * @return string
       * @throws Exception
       */
      public static function updateDaily($params) {
          //自定义字段
          $other = [];
          foreach ($params as $name => $val) {
              if (strstr( $name,'other_') !== false || strstr( $name,'main_') !== false) {
                  if(is_array($val)){
                      $other[$name] = implode(',',$val);
                  }else{
                      $other[$name] = $val;
                  }
                  unset($params[$name]);
              }
          }
          $params['other']=json_encode($other);
          if(isset($params['reminds_id'])){
              unset($params['reminds_id']);
          }
          $model = new self;
          // 调用当前模型对应的User验证器类进行数据验证
          $result = $model->allowField(true)->save($params,['id'=>$params['id']]);
          if (false === $result) {
              // 验证失败 输出错误信息
              throw new Exception($model->getError());
          }
          $lastId=$model->getLastInsID();
  
          $where['create_staff_id'] = $params['create_staff_id'];
          $where['type'] = $params['type'];
          DailyDraft::where($where)->update(array('deletetime'=>time()));
  
  
          return $lastId;
      }
  }