ExamineCategory.php 10.0KB

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