Daily.php 4.0 KB

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