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