ExamineFlow.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批流程
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use think\Request;
  12. use think\Validate;
  13. class ExamineFlow extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'admin_examine_flow';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. protected $typesArr = ['crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine'];
  24. /**
  25. * [getDataList 审批流程list]
  26. * @author Michael_xu
  27. * @param [string] $map [查询条件]
  28. * @param [number] $page [当前页数]
  29. * @param [number] $limit [每页数量]
  30. * @return [array] [description]
  31. */
  32. public function getDataList($request)
  33. {
  34. $userModel = new \app\admin\model\User();
  35. $structureModel = new \app\admin\model\Structure();
  36. $examineStepModel = new \app\admin\model\ExamineStep();
  37. $request = $this->fmtRequest( $request );
  38. $map = $request['map'] ? : [];
  39. if (isset($map['search'])) {
  40. //普通筛选
  41. $map['name'] = ['like', '%'.$map['search'].'%'];
  42. unset($map['search']);
  43. }
  44. $map['is_deleted'] = 0;
  45. $list_view = db('admin_examine_flow')
  46. ->where($map)
  47. ->alias('examine_flow')
  48. ->join('__ADMIN_USER__ user', 'user.id = examine_flow.update_user_id', 'LEFT');
  49. $list = $list_view
  50. ->page($request['page'], $request['limit'])
  51. ->field('examine_flow.*,user.realname,user.thumb_img')
  52. ->order('examine_flow.status desc,examine_flow.update_time desc')
  53. ->select();
  54. foreach ($list as $k=>$v) {
  55. $list[$k]['user_ids_info'] = $userModel->getListByStr($v['user_ids']);
  56. $list[$k]['structure_ids_info'] = $structureModel->getListByStr($v['structure_ids']);
  57. $stepList = [];
  58. $stepList = $examineStepModel->getDataList($v['flow_id']);
  59. $list[$k]['stepList'] = $stepList ? : [];
  60. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  61. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  62. }
  63. $dataCount = $list_view->where($map)->count('flow_id');
  64. $data = [];
  65. $data['list'] = $list;
  66. $data['dataCount'] = $dataCount ? : 0;
  67. return $data;
  68. }
  69. /**
  70. * 创建审批流程信息
  71. * @author Michael_xu
  72. * @param
  73. * @return
  74. */
  75. public function createData($param)
  76. {
  77. //验证
  78. if (!$param['name']) {
  79. $this->error = '请填写审批流名称1';
  80. return false;
  81. }
  82. if ($this->data($param)->allowField(true)->save()) {
  83. $data = [];
  84. $data['flow_id'] = $this->flow_id;
  85. return $data;
  86. } else {
  87. $this->error = '添加失败';
  88. return false;
  89. }
  90. }
  91. /**
  92. * 编辑审批流程信息
  93. * @author Michael_xu
  94. * @param
  95. * @return
  96. */
  97. public function updateDataById($param, $flow_id = '')
  98. {
  99. unset($param['flow_id']);
  100. $dataInfo = $this->get($flow_id);
  101. if (!$dataInfo) {
  102. $this->error = '数据不存在或已删除';
  103. return false;
  104. }
  105. //过滤不能修改的字段
  106. $unUpdateField = ['create_user_id','is_deleted','delete_time'];
  107. foreach ($unUpdateField as $v) {
  108. unset($param[$v]);
  109. }
  110. //验证
  111. if (!$param['name']) {
  112. $this->error = '请填写审批流名称';
  113. return false;
  114. }
  115. // $param['flow_id'] = $flow_id;
  116. if ($this->allowField(true)->save($param)) {
  117. $data = [];
  118. $data['flow_id'] = $flow_id;
  119. return $data;
  120. } else {
  121. $this->error = '编辑失败,请重试';
  122. return false;
  123. }
  124. }
  125. /**
  126. * 审批流程详情
  127. * @author Michael_xu
  128. * @param
  129. * @return
  130. */
  131. public function getDataById($flow_id = '')
  132. {
  133. $userModel = new \app\admin\model\User();
  134. $dataInfo = $this->get($flow_id);
  135. if (!$dataInfo) {
  136. $this->error = '数据不存在或已删除';
  137. return false;
  138. }
  139. //审批步骤
  140. $stepList = db('admin_examine_step')->where(['flow_id' => $flow_id])->select();
  141. foreach ($stepList as $k=>$v) {
  142. $examine_user_id_arr = [];
  143. switch ($v['status']) {
  144. case 2 :
  145. case 3 : $examine_user_id_arr = stringToArray($v['user_id']); break;
  146. default : $examine_user_id_arr = []; break;
  147. }
  148. $stepList[$k]['user_id_info'] = $userModel->getUserByIdArr($examine_user_id_arr);
  149. }
  150. $dataInfo['stepList'] = $stepList ? : [];
  151. return $dataInfo;
  152. }
  153. /**
  154. * 审批流程(根据对象获取需要执行的审批流程)
  155. * @param types 审批对象
  156. * @param types_id 审批对象ID(如OA审批类型ID)
  157. */
  158. public function getFlowByTypes($user_id, $types, $types_id = 0)
  159. {
  160. $userModel = new \app\admin\model\User();
  161. if (!in_array($types, $this->typesArr)) {
  162. $this->error = '参数错误';
  163. return false;
  164. }
  165. $map['types'] = $types;
  166. $map['status'] = 1;
  167. $map['is_deleted'] = 0;
  168. if ($types !== 'oa_examine') {
  169. $types_id = 0;
  170. }
  171. $map['types_id'] = $types_id;
  172. //判断用户使用哪个流程(优先级:所属部门 > 全部)
  173. $userData = $userModel->getUserById($user_id);
  174. $userData['map'] = $map;
  175. $flowInfo = db('admin_examine_flow')
  176. ->where(function ($query) use ($userData) {
  177. $userData = $userData;
  178. $query->where(['config' => 1])
  179. ->where($userData['map'])
  180. ->where(function ($query) use ($userData) {
  181. $query->where('structure_ids','like','%,'.$userData['structure_id'].',%')
  182. ->whereOr('user_ids','like','%,'.$userData['id'].',%');
  183. });
  184. })->whereOr(function ($query) use ($userData) {
  185. $query->where(['config' => 1])
  186. ->where($userData['map'])
  187. ->where('structure_ids','eq','')
  188. ->where('user_ids','eq','');
  189. })->whereOr(function ($query) use ($userData) {
  190. $query->where(['config' => 0])
  191. ->where($userData['map']);
  192. })->order('update_time desc')->find();
  193. return $flowInfo ? : [];
  194. }
  195. /**
  196. * 审批流程权限(创建操作使用)
  197. * @param types 审批对象
  198. * @param user_id 审批对象申请人ID
  199. * @param category_id 审批类型ID,或其他类型ID
  200. */
  201. public function checkExamine($user_id, $types, $category_id = 0)
  202. {
  203. $examineStepModel = new \app\admin\model\ExamineStep();
  204. //符合条件的审批流
  205. $resFlow = $this->getFlowByTypes($user_id, $types, $category_id);
  206. if (!$resFlow) {
  207. return false;
  208. }
  209. if ($resFlow['config'] == 1) {
  210. //审批流是否为空
  211. $stepList = $examineStepModel->getStepList($resFlow['flow_id'], $user_id, $types);
  212. if (!$stepList) {
  213. return false;
  214. }
  215. }
  216. return $resFlow;
  217. }
  218. /**
  219. * 审批流程下所有审批人ID
  220. * @param
  221. * @return
  222. */
  223. public function getUserByFlow($flow_id, $user_id, $check_user_id = '')
  224. {
  225. $flowInfo = db('admin_examine_flow')->where(['flow_id' => $flow_id])->find();
  226. $userIds = [];
  227. if ($flowInfo['config'] == 1) {
  228. $stepList = db('admin_examine_step')->where(['flow_id' => $flow_id])->select();
  229. foreach ($stepList as $k=>$v) {
  230. if ($v['status'] == 1) {
  231. $userInfo = db('admin_user')->where(['id' => $user_id])->find();
  232. if ($userInfo['parent_id']) {
  233. $userIds[] = $userInfo['parent_id'];
  234. } else {
  235. $userIds[] = 1;
  236. }
  237. }
  238. if (stringToArray($v['user_id'])) $userIds = $userIds ? array_merge($userIds, stringToArray($v['user_id'])) : stringToArray($v['user_id']);
  239. }
  240. } else {
  241. $userIds = [];
  242. $check_user_id = stringToArray($check_user_id);
  243. //查询已审批人ID(未失效的)
  244. $is_check_user_id = db('admin_examine_record')->where(['flow_id' => $flow_id,'is_end' => 0])->column('check_user_id');
  245. if ($check_user_id && $is_check_user_id) {
  246. $userIds = array_merge($check_user_id, $is_check_user_id);
  247. } elseif ($check_user_id) {
  248. $userIds = $check_user_id;
  249. } else {
  250. $userIds = $is_check_user_id;
  251. }
  252. }
  253. return $userIds ? : [];
  254. }
  255. }