ExamineCategory.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批类型
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\oa\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use think\Request;
  11. use think\Validate;
  12. class ExamineCategory extends Common
  13. {
  14. /**
  15. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  16. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  17. */
  18. protected $name = 'oa_examine_category';
  19. protected $createTime = 'create_time';
  20. protected $updateTime = 'update_time';
  21. protected $autoWriteTimestamp = true;
  22. /**
  23. * [getDataList 审批类型list]
  24. * @param [string] $map [查询条件]
  25. * @param [number] $page [当前页数]
  26. * @param [number] $limit [每页数量]
  27. * @return [array] [description]
  28. * @author Michael_xu
  29. */
  30. public function getDataList($request)
  31. {
  32. $userModel = new \app\admin\model\User();
  33. $structureModel = new \app\admin\model\Structure();
  34. $examineFlowModel = new \app\admin\model\ExamineFlow();
  35. $examineStepModel = new \app\admin\model\ExamineStep();
  36. $request = $this->fmtRequest($request);
  37. $map = $request['map'] ?: [];
  38. if (isset($map['search'])) {
  39. //普通筛选
  40. $map['title'] = ['like', '%' . $map['search'] . '%'];
  41. unset($map['search']);
  42. }
  43. $map['is_deleted'] = 0;
  44. $list = $this
  45. ->where($map)
  46. ->page($request['page'], $request['limit'])
  47. ->select();
  48. foreach ($list as $k => $v) {
  49. $flowInfo = [];
  50. $flowInfo = $examineFlowModel->getDataById($v['flow_id']);
  51. $list[$k]['config'] = !empty($flowInfo['config']) ? 1 : 0;
  52. $stepList = [];
  53. $stepList = $examineStepModel->getDataList($v['flow_id']);
  54. $list[$k]['stepList'] = $stepList ?: [];
  55. $list[$k]['user_ids_info'] = $userModel->getListByStr($v['user_ids']);
  56. $list[$k]['structure_ids_info'] = $structureModel->getListByStr($v['structure_ids']);
  57. }
  58. $dataCount = $this->where($map)->count('category_id');
  59. $data = [];
  60. $data['list'] = $list;
  61. $data['dataCount'] = $dataCount ?: 0;
  62. return $data;
  63. }
  64. /**
  65. * 创建审批类型信息
  66. * @param
  67. * @return
  68. * @author Michael_xu
  69. */
  70. public function createData($param)
  71. {
  72. $fieldModel = new \app\admin\model\Field();
  73. //验证
  74. $validate = validate($this->name);
  75. if (!$validate->check($param)) {
  76. $this->error = $validate->getError();
  77. return false;
  78. }
  79. $param['is_sys'] = 0;
  80. $param['user_ids'] = $param['user_ids'] ? arrayToString($param['user_ids']) : ''; //处理user_id
  81. $param['structure_ids'] = $param['structure_ids'] ? arrayToString($param['structure_ids']) : ''; //处理structure_id
  82. $examineStep = $param['step']; //审批步骤
  83. $config = $param['config'] ? 1 : 0; //审批流程类型 1固定审批0授权审批
  84. if ($this->data($param)->allowField(true)->save()) {
  85. //添加基础自定义字段
  86. $fieldData = [];
  87. $fieldData[0]['types'] = 'oa_examine';
  88. $fieldData[0]['types_id'] = $this->category_id;
  89. $fieldData[0]['field'] = 'content';
  90. $fieldData[0]['name'] = '审批事由';
  91. $fieldData[0]['form_type'] = 'textarea';
  92. $fieldData[0]['is_null'] = '1';
  93. $fieldData[0]['order_id'] = '1';
  94. $fieldData[0]['operating'] = '1';
  95. $fieldData[0]['create_time'] = time();
  96. $fieldData[0]['update_time'] = time();
  97. $fieldData[1]['types'] = 'oa_examine';
  98. $fieldData[1]['types_id'] = $this->category_id;
  99. $fieldData[1]['field'] = 'remark';
  100. $fieldData[1]['name'] = '备注';
  101. $fieldData[1]['form_type'] = 'textarea';
  102. $fieldData[1]['is_null'] = '0';
  103. $fieldData[1]['order_id'] = '1';
  104. $fieldData[1]['operating'] = '1';
  105. $fieldData[1]['create_time'] = time();
  106. $fieldData[1]['update_time'] = time();
  107. if (!$fieldModel->createData('oa_examine', $fieldData)) {
  108. db('oa_examine_category')->where(['category_id' => $this->category_id])->delete();
  109. $this->error = '程序出错,请重试';
  110. return false;
  111. }
  112. $data = [];
  113. $data['category_id'] = $this->category_id;
  114. //创建审批流
  115. if (is_array($examineStep) && $examineStep) {
  116. $examineFlowModel = new \app\admin\model\ExamineFlow();
  117. $examineStepModel = new \app\admin\model\ExamineStep();
  118. $examineFlow = [];
  119. $examineFlow['name'] = $param['title'] . '流程';
  120. $examineFlow['config'] = $config;
  121. $examineFlow['types'] = 'oa_examine';
  122. $examineFlow['types_id'] = $this->category_id;
  123. $examineFlow['user_ids'] = arrayToString($param['user_ids']);
  124. $examineFlow['structure_ids'] = arrayToString($param['structure_ids']);
  125. $examineFlow['update_user_id'] = $param['create_user_id'];
  126. $examineFlow['status'] = 1;
  127. $res = $examineFlowModel->createData($examineFlow);
  128. if ($res) {
  129. if ((int)$config == 1) {
  130. $resUpdate = db('oa_examine_category')->where(['category_id' => $this->category_id])->update(['flow_id' => $res['flow_id']]);
  131. //固定审批流
  132. $resStep = $examineStepModel->createStepData($examineStep, $res['flow_id']);
  133. if ($resStep) {
  134. return $data;
  135. } else {
  136. db('admin_examine_flow')->where(['flow_id' => $res['flow_id']])->delete();
  137. $this->error = $examineStepModel->getError();
  138. return false;
  139. }
  140. } else {
  141. return $data;
  142. }
  143. } else {
  144. $this->error = $examineFlowModel->getError();
  145. return false;
  146. }
  147. } else {
  148. $this->error = '请添加审批步骤';
  149. return false;
  150. }
  151. } else {
  152. $this->error = '添加失败';
  153. return false;
  154. }
  155. }
  156. /**
  157. * 编辑审批类型信息
  158. * @param
  159. * @return
  160. * @author Michael_xu
  161. */
  162. public function updateDataById($param, $category_id = '')
  163. {
  164. $category_id = intval($category_id);
  165. unset($param['id']);
  166. //过滤不能修改的字段
  167. $unUpdateField = ['create_user_id', 'is_deleted', 'delete_user_id', 'delete_time', 'is_sys'];
  168. foreach ($unUpdateField as $v) {
  169. unset($param[$v]);
  170. }
  171. //验证
  172. $validate = validate($this->name);
  173. if (!$validate->check($param)) {
  174. $this->error = $validate->getError();
  175. return false;
  176. }
  177. $param['user_ids'] = is_array($param['user_ids']) ? arrayToString($param['user_ids']) : $param['user_ids']; //处理user_id
  178. $param['structure_ids'] = is_array($param['structure_ids']) ? arrayToString($param['structure_ids']) : $param['structure_ids']; //处理structure_id
  179. $examineStep = $param['step']; //审批步骤
  180. $config = $param['config'] ? 1 : 0; //审批流程类型 1固定审批0授权审批
  181. if ($this->allowField(true)->save($param, ['category_id' => $category_id])) {
  182. $data = [];
  183. $data['category_id'] = $category_id;
  184. return $data;
  185. } else {
  186. $this->error = '编辑失败';
  187. return false;
  188. }
  189. }
  190. /**
  191. * 审批类型数据
  192. * @param $id 审批ID
  193. * @return
  194. */
  195. public function getDataById($id = '')
  196. {
  197. $map['category_id'] = $id;
  198. $dataInfo = db('oa_examine_category')->where($map)->find();
  199. return $dataInfo ?: [];
  200. }
  201. /**
  202. * 逻辑删除,将数据标记为删除状态
  203. * @author Michael_xu
  204. */
  205. public function signDelById($id, $user_id)
  206. {
  207. if (!$id) {
  208. $this->error = '删除失败';
  209. return false;
  210. }
  211. $info = $this->get($id);
  212. if ($info['is_sys'] == 1) {
  213. $this->error = '系统类型,不能删除';
  214. return false;
  215. }
  216. //是否被使用
  217. $resCategory = db('oa_examine')->where(['category_id' => $info['category_id']])->find();
  218. if ($resCategory) {
  219. $this->error = '已有审批,不能删除';
  220. return false;
  221. }
  222. $this->startTrans();
  223. try {
  224. $data['is_deleted'] = 1;
  225. $data['delete_time'] = time();
  226. $data['delete_user_id'] = $user_id;
  227. $this->allowField(true)->save($data, ['category_id' => $id]);
  228. $this->commit();
  229. return true;
  230. } catch (\Exception $e) {
  231. $this->error = '删除失败';
  232. $this->rollback();
  233. return false;
  234. }
  235. }
  236. }