Approval.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. * 审批管理
  8. */
  9. class Approval Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdongams_approval';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'show_staff_data',
  22. ];
  23. public function getShowStaffDataAttr($value,$data)
  24. {
  25. if (!isset($data['show_staff_id'])) {
  26. return '';
  27. }
  28. $ids=explode(',',$data['show_staff_id']);
  29. return Staff::where(['id'=>['in',$ids]])->field('id,name,img,post,mobile')->select();
  30. }
  31. /**
  32. * @desc 备注
  33. * @update_date 2021/7/10 更新时间
  34. * @author zhangwei
  35. */
  36. public static function createApproval($params) {
  37. $data = $params['data'];
  38. $formapproval_id = $params['formapproval_id'];
  39. $relation_type = $params['relation_type']??'';
  40. $relation_id = $params['relation_id']??0;
  41. $flow_staff_id = $params['flow_staff_ids']??'';
  42. $file_ids = $params['file_ids']??'';
  43. $add = [
  44. 'content' => json_encode($data),
  45. 'formapproval_id' => $formapproval_id,
  46. 'relation_id' => $relation_id,
  47. 'flow_staff_ids' => $flow_staff_id,
  48. 'file_ids' => $file_ids,
  49. 'check_status' => 1,
  50. ];
  51. if(isset($params['reminds_id'])){
  52. $add['reminds_id']=$params['reminds_id'];
  53. }
  54. $staff = Staff::info();
  55. if ($staff) {
  56. $add['create_staff_id'] = $staff->id;
  57. }
  58. $add['show_staff_id']=$add['create_staff_id'];
  59. $flow = Flow::getsteplist(Flow::APPROVAL_STATUS.'_'.$formapproval_id);
  60. $add['flow_id'] = $flow['flow_id'];
  61. $add['order_id'] = $flow['order_id'];
  62. if ($flow['status'] == 0) {//发起人自选
  63. if (empty($add['flow_staff_ids'])) {
  64. throw new Exception('审批人必须选择');
  65. }
  66. $add['flow_staff_ids'] = trim($params['flow_staff_ids']);
  67. } else {
  68. $add['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  69. }
  70. $model = new self;
  71. // 调用当前模型对应的User验证器类进行数据验证
  72. $result = $model->allowField(true)->save($add);
  73. if (false === $result) {
  74. // 验证失败 输出错误信息
  75. throw new Exception($model->getError());
  76. }
  77. $lastId = $model->getLastInsID();
  78. if ($flow['status'] == 1) {//固定审批
  79. //发送审批通知
  80. Flow::sendStepRecord($flow,Flow::APPROVAL_STATUS, $lastId);
  81. } else {//发起人自选 依次审批
  82. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  83. if ($staff_id) {
  84. ExamineRecord::addExaminse(ExamineRecord::APPROVAL_TYPE, $lastId, $staff_id);
  85. }
  86. }
  87. //创建日志
  88. OperationLog::createLog(OperationLog::APPROVAL_TYPE, $lastId, '创建');
  89. return $lastId;
  90. }
  91. //修改
  92. public static function updateApproval($params) {
  93. $data = $params['data'];
  94. $formapproval_id = $params['formapproval_id'];
  95. $relation_type = $params['relation_type'];
  96. $relation_id = $params['relation_id']??0;
  97. $flow_staff_id = $params['flow_staff_ids']??'';
  98. $file_ids = $params['file_ids']??'';
  99. $update = [
  100. 'content' => json_encode($data),
  101. 'file_ids' => $file_ids,
  102. 'formapproval_id' => $formapproval_id,
  103. 'relation_type' => $relation_type,
  104. 'relation_id' => $relation_id,
  105. 'flow_staff_ids' => $flow_staff_id,
  106. 'check_status' => 1,
  107. 'next_staff_id' => explode(',', $flow_staff_id)[0]
  108. ];
  109. $flow = Flow::getsteplist(Flow::APPROVAL_STATUS.'_'.$formapproval_id);
  110. $update['flow_id'] = $flow['flow_id'];
  111. $update['order_id'] = $flow['order_id'];
  112. if ($flow['status'] == 0) {//发起人自选
  113. if (empty($update['flow_staff_ids'])) {
  114. throw new Exception('审批人必须选择');
  115. }
  116. $update['flow_staff_ids'] = trim($params['flow_staff_ids']);
  117. } else {
  118. $update['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  119. }
  120. $model = new self;
  121. // 调用当前模型对应的User验证器类进行数据验证
  122. $result = $model->allowField(true)->save($update,['id'=>$params['id']]);
  123. if (false === $result) {
  124. // 验证失败 输出错误信息
  125. throw new Exception($model->getError());
  126. }
  127. if ($flow['status'] == 1) {//固定审批
  128. //发送审批通知
  129. Flow::sendStepRecord($flow,Flow::APPROVAL_STATUS, $params['id']);
  130. } else {//发起人自选 依次审批
  131. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  132. if ($staff_id) {
  133. ExamineRecord::addExaminse(ExamineRecord::APPROVAL_TYPE, $params['id'], $staff_id);
  134. }
  135. }
  136. //创建日志
  137. OperationLog::createLog(OperationLog::APPROVAL_TYPE, $params['id'], '修改');
  138. return true;
  139. }
  140. //
  141. public function getFileidsAttr($value) {
  142. $files = explode(',', $value);
  143. $result = [];
  144. foreach ($files as $fid) {
  145. if ($fid) {
  146. $result[] = cdnurl(File::getUrl($fid), true);
  147. }
  148. }
  149. return $result;
  150. }
  151. //获取内容
  152. public function getContent($value){
  153. return json_decode($value,true);
  154. }
  155. //创建人
  156. public function createStaff() {
  157. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
  158. }
  159. //
  160. public function formapproval() {
  161. return $this->hasOne(FormApproval::class, 'id', 'formapproval_id')->field('id,name,form_id');
  162. }
  163. //获取时间
  164. public function getCreatetimeAttr($value){
  165. return date('Y-m-d H:i',$value);
  166. }
  167. //获取内容
  168. public function getContentAttr($value){
  169. return json_decode($value,true);
  170. }
  171. }