Approval.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Approval as ApprovalModel;
  4. use addons\qingdongams\model\ExamineRecord;
  5. use addons\qingdongams\model\FormApproval;
  6. use addons\qingdongams\model\Form;
  7. use addons\qingdongams\model\FormField;
  8. use addons\qingdongams\model\Message;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 工作审批
  13. */
  14. class Approval extends StaffApi {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = [];
  17. //审批数量
  18. public function examineApprovalNumber()
  19. {
  20. $whereFlow = [];
  21. $where = [];
  22. $where['check_status'] = ['in', [0, 1]];
  23. $whereFlow['next_staff_id'] = $this->auth->id;
  24. $woshenhe = ApprovalModel::where($where)->where($whereFlow)->count();
  25. $where['create_staff_id'] = $this->auth->id;
  26. //我发起的
  27. $wofaqi = ApprovalModel::where($where)->count();
  28. $this->success('请求成功', ['wofaqi' => $wofaqi, 'woshenhe' => $woshenhe]);
  29. }
  30. //审核审核记录
  31. public function examineApprovalList(){
  32. $check_status=input('check_status');//1审核中、2审核通过、3审核未通过
  33. $type=input('type',1);
  34. $whereFlow=$followWhere = [];
  35. $where=[];
  36. if ($check_status) {
  37. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  38. if ($check_status == 1) {
  39. $where['check_status'] = ['in', [0, 1]];
  40. $followWhere['next_staff_id'] = $this->auth->id;
  41. } elseif ($check_status == 2) {
  42. $where['check_status'] = 2;
  43. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id. ',check_staff_ids)')];
  44. } elseif ($check_status == 3) {
  45. $where['check_status'] = 3;
  46. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id. ',check_staff_ids)')];
  47. } elseif ($check_status == 4) {
  48. $where['check_status'] = 4;
  49. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id. ',check_staff_ids)')];
  50. } elseif ($check_status == 9) {
  51. $where['check_status'] = 9;
  52. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id. ',check_staff_ids)')];
  53. }
  54. }
  55. if($type == 1){//我发起的
  56. $where['create_staff_id']=$this->auth->id;
  57. }elseif($type == 2){//我审批的
  58. $whereFlow = $followWhere;
  59. }
  60. $approvals= ApprovalModel::where($where)->where($whereFlow)->with(['createStaff','formapproval'])->order('id desc')->paginate();
  61. $this->success('请求成功',$approvals);
  62. }
  63. /**
  64. * 获取工作列表
  65. */
  66. public function getList() {
  67. $list = FormApproval::where([])->field('id,name,img')->select();
  68. $this->success('请求成功', $list);
  69. }
  70. /**
  71. * 获取form表单
  72. */
  73. public function getFormapproval() {
  74. $id = input('id');
  75. $form = FormApproval::where(['id' => $id])->find();
  76. $formData = Form::where(array('id'=>$form['form_id']))->find();
  77. $staff = \addons\qingdongams\model\Staff::where(['id' => ['in',explode(',', $form['flow_staff_ids'])],'status'=>1])->field('id,name,img')->select();
  78. $data=json_decode($formData['data'], true)['data']??[];
  79. $this->success('请求成功',['data'=>$data,'staff'=>$staff] );
  80. }
  81. /**
  82. * 添加审批
  83. */
  84. public function addApproval() {
  85. $params = $this->request->post();
  86. if(empty( $params['data'])){
  87. $this->error('数据不能为空');
  88. }
  89. if(empty( $params['formapproval_id'])){
  90. $this->error('表单id不能为空');
  91. }
  92. if(empty( $params['relation_type'])){
  93. $this->error('关联类型不能为空');
  94. }
  95. $approval=FormApproval::where(['id'=>$params['formapproval_id']])->with(['form'])->find();
  96. $result = FormField::checkFields($approval['form']['type'], $params['data']);
  97. if ($result !== true) {
  98. $this->error($result);
  99. }
  100. ApprovalModel::createApproval($params);
  101. $this->success('请求成功');
  102. }
  103. /**
  104. * 获取审批详情
  105. */
  106. public function getDetail(){
  107. $id = input('id');
  108. if(empty($id)){
  109. $this->error('参数不能为空');
  110. }
  111. $approval=ApprovalModel::where(['id'=>$id])->find()->toArray();
  112. if ($approval['check_status'] == 0 || $approval['check_status'] == 1) {
  113. $approval['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::APPROVAL_TYPE, $id);
  114. } else {
  115. $approval['is_examine'] = 0;
  116. }
  117. $approval['is_operation']=0;
  118. if($approval['create_staff_id'] == $this->auth->id){
  119. //是否可以操作
  120. $approval['is_operation']=1;
  121. }
  122. $form=FormApproval::where(['id'=>$approval['formapproval_id']])->with(['form'])->find();
  123. $approval['content'] = Form::getDataDetail($form['form']['type'], $approval['content']);
  124. Message::setRead(Message::APPROVAL_TYPE,$id,$this->auth->id);
  125. $this->success('请求成功',$approval);
  126. }
  127. /**
  128. * 修改审批
  129. */
  130. public function editApproval(){
  131. $id = input('id');
  132. $params = $this->request->post();
  133. $row = ApprovalModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
  134. if (empty($row)) {
  135. $this->error('审批信息不存在');
  136. }
  137. if(empty( $params['data'])){
  138. $this->error('数据不能为空');
  139. }
  140. if(empty( $params['formapproval_id'])){
  141. $this->error('表单id不能为空');
  142. }
  143. if(empty( $params['relation_type'])){
  144. $this->error('关联类型不能为空');
  145. }
  146. $approval=FormApproval::where(['id'=>$params['formapproval_id']])->with(['form'])->find();
  147. $result = FormField::checkFields($approval['form']['type'], $params['data']);
  148. if ($result !== true) {
  149. $this->error($result);
  150. }
  151. Db::startTrans();
  152. try {
  153. $result = ApprovalModel::updateApproval($params);
  154. Db::commit();
  155. } catch (Exception $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. }
  159. $this->success('修改信息成功');
  160. }
  161. /**
  162. * 撤回审批
  163. */
  164. public function cancel(){
  165. $id = input('id');
  166. $customer = ApprovalModel::where([
  167. 'id' => $id,
  168. 'check_status' => ['in', [0, 1]],
  169. 'create_staff_id'=>$this->auth->id])->find();
  170. if (empty($customer)) {
  171. $this->error('信息不存在');
  172. }
  173. Db::startTrans();
  174. try {
  175. ApprovalModel::where(['id' => $id])->update(['check_status' => 4]);
  176. ExamineRecord::cancelExaminse(ExamineRecord::APPROVAL_TYPE,$id);
  177. Db::commit();
  178. } catch (Exception $e) {
  179. Db::rollback();;
  180. $this->error($e->getMessage());
  181. }
  182. $this->success('撤回成功');
  183. }
  184. /**
  185. * 终止审批
  186. */
  187. public function revokeApproval()
  188. {
  189. $id = input('id');
  190. if(empty($id)){
  191. $this->error('id不能为空');
  192. }
  193. Db::startTrans();
  194. try {
  195. ApprovalModel::where(['id'=>$id])->update(['check_status'=>9]);
  196. ExamineRecord::cancelExaminse(ExamineRecord::APPROVAL_TYPE,$id);
  197. Db::commit();
  198. } catch (Exception $e) {
  199. Db::rollback();
  200. $this->error($e->getMessage());
  201. }
  202. $this->success('撤销成功');
  203. }
  204. }