0
0

FCleanTask.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. class FCleanTask extends Base {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'f_clean_task';
  10. protected $validateName = 'FCleanTask';
  11. public function updates() {
  12. $data = request()->post();
  13. if (!isset($data['type']) || empty($data['type'])) {
  14. $this->error = '请选择任务项';
  15. return false;
  16. }
  17. if (!isset($data['user']) || empty($data['user'])) {
  18. $this->error = '请选择人员';
  19. return false;
  20. }
  21. $data['type_id'] = (new FCleanForm())->check_form_type(explode(',', $data['type']));
  22. if ($data['type_id'] <= 0) {
  23. $this->error = '请选择请选择同一分类下的任务项';
  24. return false;
  25. }
  26. if ($data['start_time'] >= $data['end_time']) {
  27. $this->error = '开始时间要小于结束时间';
  28. return false;
  29. }
  30. $data['org_id'] = cur_org_id();
  31. $result = validate($this->validateName)->check($data, [], '');
  32. if (true !== $result) {
  33. $this->error = validate($this->validateName)->getError();
  34. return false;
  35. }
  36. $id = $data['id'];
  37. unset($data['id']);
  38. $clean_task_form = $data['type'];
  39. $user = $data['user'];
  40. unset($data['type'], $data['user']);
  41. $this->startTrans();
  42. try {
  43. if ($id > 0) {
  44. $data['update_time'] = date('Y-m-d H:i:s');
  45. $ret = $this->allowField(true)->save($data, ['id' => $id]);
  46. $res = Db::name('f_clean_task_form')
  47. ->where('task_id', $id)->delete();
  48. if (!$res) {
  49. throw new Exception('重置任务内容失败');
  50. }
  51. $res = Db::name('f_clean_task_user')
  52. ->where('task_id', $id)->delete();
  53. if (!$res) {
  54. throw new Exception('重置任务人员失败');
  55. }
  56. }
  57. else {
  58. $data['create_time'] = date('Y-m-d H:i:s');
  59. $ret = $this->allowField(true)->save($data);
  60. $id = $this->getLastInsID();
  61. }
  62. if (!$ret) {
  63. throw new Exception('任务保存失败');
  64. }
  65. //添加任务内容
  66. $clean_task_form = explode(',', $clean_task_form);
  67. $nArr = [];
  68. foreach ($clean_task_form as $v) {
  69. $nArr[] = [
  70. 'task_id' => $id,
  71. 'form_id' => $v
  72. ];
  73. }
  74. $res = Db::name('f_clean_task_form')
  75. ->insertAll($nArr);
  76. if (!$res) {
  77. throw new Exception('保存任务内容失败');
  78. }
  79. //添加任务人员
  80. $user = explode(',', $user);
  81. $nArr = [];
  82. foreach ($user as $v) {
  83. $nArr[] = [
  84. 'task_id' => $id,
  85. 'user_id' => $v
  86. ];
  87. }
  88. $res = Db::name('f_clean_task_user')
  89. ->insertAll($nArr);
  90. if (!$res) {
  91. throw new Exception('保存任务人员失败');
  92. }
  93. $this->commit();
  94. return true;
  95. } catch (Exception $e) {
  96. $this->rollback();
  97. $this->error = $e->getMessage();
  98. return false;
  99. }
  100. }
  101. public function lists($page, $size, $status, $userId, $orgId, $type) {
  102. $ret = [];
  103. if ($type == 1) {
  104. $ret = $this
  105. ->field('id as task_id,start_time,title,end_time,status,timeout,type_id')
  106. ->where([
  107. 'org_id' => $orgId,
  108. 'del' => 0,
  109. 'status' => $status,
  110. ])
  111. ->order('id desc')
  112. ->page($page, $size)
  113. ->select();
  114. $ret = $ret ? $ret->toArray() : [];
  115. }
  116. else {
  117. $ret = $this->alias('a')
  118. ->join('f_clean_task_user b', 'a.id=b.task_id')
  119. ->field('a.id as task_id,a.start_time,a.title,a.end_time,a.status,a.timeout,a.type_id')
  120. ->where([
  121. 'a.org_id' => $orgId,
  122. 'a.del' => 0,
  123. 'a.status' => $status,
  124. 'b.user_id' => $userId,
  125. ])
  126. ->order('a.id desc')
  127. ->page($page, $size)
  128. ->select();
  129. $ret = $ret ? $ret->toArray() : [];
  130. }
  131. foreach ($ret as $k => $v) {
  132. $ret[$k]['type_name'] = Db::name('f_clean_type')
  133. ->where('id', $v['type_id'])
  134. ->where('org_id', $orgId)
  135. ->value('title');
  136. $user = Db::name('f_clean_task_user')
  137. ->alias('tu')
  138. ->join('user u', 'u.id = tu.user_id')
  139. ->where('tu.task_id', $v['task_id'])
  140. ->column('u.real_name');
  141. $implode = empty($user) ? [] : implode('/', $user);
  142. $ret[$k]['staff'] = $implode;
  143. }
  144. return $ret ? $ret : [];
  145. }
  146. public function taskCount($status, $userId, $orgId) {
  147. $isDispatch = check_is_dispatch($userId);
  148. if ($isDispatch) {
  149. $ret = $this
  150. ->field('id as task_id,start_time,title,end_time,status,timeout,type_id')
  151. ->where([
  152. 'org_id' => $orgId,
  153. 'del' => 0,
  154. 'status' => $status,
  155. ])
  156. ->count();
  157. }
  158. else {
  159. $ret = $this->alias('a')
  160. ->join('f_clean_task_user b', 'a.id=b.task_id')
  161. ->field('a.id as task_id,a.start_time,a.title,a.end_time,a.status,a.timeout,a.type_id')
  162. ->where([
  163. 'a.org_id' => $orgId,
  164. 'a.del' => 0,
  165. 'a.status' => $status,
  166. 'b.user_id' => $userId,
  167. ])
  168. ->count();
  169. }
  170. return $ret ? $ret : 0;
  171. }
  172. public function detail($id) {
  173. $ret = $this->alias('ct')
  174. ->field('ct.id,ct.start_time,ct.type_id,ct.end_time,ct.title,t.title as type_name,ct.check_content as report_text')
  175. ->join('f_clean_type t', 't.id=ct.type_id', 'left')
  176. ->where('ct.id', $id)
  177. ->find();
  178. $ret = $ret ? $ret->toArray() : [];
  179. $userName = Db::name('f_clean_task_user')
  180. ->alias('tu')
  181. ->join('user u', 'u.id = tu.user_id')
  182. ->where('tu.task_id', $id)
  183. ->column('u.real_name');
  184. $implode = empty($userName) ? [] : implode('/', $userName);
  185. $ret['name'] = $implode;
  186. $formList = Db::name('f_clean_task_form')
  187. ->alias('ctf')
  188. ->join('f_clean_form cf', 'cf.id = ctf.form_id')
  189. ->field('cf.id,cf.title as name')
  190. ->where('task_id', $ret['id'])
  191. ->select();
  192. $formList = $formList ? $formList : [];
  193. foreach ($formList as $k => $v) {
  194. $record = Db::name('f_clean_record')
  195. ->where('task_id', $id)
  196. ->where('form_id', $v['id'])->find();
  197. $formList[$k]['status'] = $record ? 1 : 0;
  198. }
  199. $ret['form_list'] = $formList;
  200. return $ret;
  201. }
  202. public function addSave($content, $formId, $taskId, $orgId, $formJson, $userId, $consItems) {
  203. // 检查该任务是已保存
  204. $res = Db::name('f_clean_record')
  205. ->where('task_id', $taskId)
  206. ->where('form_id', $formId)
  207. ->find();
  208. if ($res) {
  209. HelpHander::error('已上传记录');
  210. }
  211. $task = $this->where('id', $taskId)->find();
  212. $countrecord = Db::name('f_clean_record')
  213. ->where('task_id', $taskId)->count();
  214. $this->startTrans();
  215. try {
  216. $data = [
  217. 'org_id' => $orgId,
  218. 'user_id' => $userId,
  219. 'form_id' => $formId,
  220. 'content' => $content,
  221. 'check_json' => $formJson,
  222. 'create_time' => date('Y-m-d H:i:s'),
  223. 'task_id' => $taskId,
  224. ];
  225. $ret = Db::name('f_clean_record')->insertGetId($data);
  226. $record_id = $ret;
  227. if (!$ret) {
  228. \exception('保存失败');
  229. }
  230. $countaddr = Db::name('f_clean_task_form')
  231. ->where('task_id', $taskId)
  232. ->count();
  233. if ($countaddr == $countrecord + 1) {
  234. $status = 2;
  235. }
  236. else {
  237. $status = 1;
  238. }
  239. if ($task['status'] != $status) {
  240. $ret = $this->where('id', $taskId)->update(['status' => $status]);
  241. if (!$ret) {
  242. \exception('操作失败');
  243. }
  244. }
  245. if ($consItems) { // 耗材
  246. $consItems = json_decode($consItems, true);
  247. $mate = [
  248. 'todo_id' => $record_id,
  249. 'order_id' => $record_id,
  250. 'org_id' => $orgId,
  251. 'user_id' => $userId,
  252. 'type' => 1,
  253. 'create_time' => getTime()
  254. ];
  255. $todo_mate_id = Db::name('todo_mate')
  256. ->insertGetId($mate);
  257. if (!$todo_mate_id) {
  258. \exception('使用物品记录失败');
  259. }
  260. $items = [];
  261. $a = [];
  262. foreach ($consItems as $k => $v) {
  263. $itemInfo = Db::name('mate_goods')
  264. ->where('id', $v['itemsId'])
  265. ->find();
  266. // $pInfo = Db::name('mate_goods')
  267. // ->where('id', $itemInfo['pid'])
  268. // ->find();
  269. if ($itemInfo['nums'] < $v['total']) {
  270. \exception($itemInfo['title'] . ' 库存不足');
  271. }
  272. $items[] = [
  273. 'todo_mate_id' => $todo_mate_id,
  274. 'items_id' => $v['itemsId'],
  275. 'total' => $v['total'],
  276. 'create_time' => getTime(),
  277. 'user_id' => $userId,
  278. 'money' => $itemInfo['price'],
  279. 'total_money' => $itemInfo['price'] * $v['total'],
  280. ];
  281. //clean_record_items
  282. $a[] = [
  283. 'org_id'=>$orgId,
  284. 'record_id'=>$record_id,
  285. 'items_id'=>$v['itemsId'],
  286. 'code'=>'',
  287. 'money'=>$itemInfo['price'],
  288. 'total'=>$v['total'],
  289. ];
  290. $res = Db::name('mate_goods')
  291. ->where('id', $v['itemsId'])
  292. ->setDec('nums', $v['total']);
  293. if (!$res) {
  294. \exception($itemInfo['title'] . ' 数量修改失败');
  295. }
  296. }
  297. $res = Db::name('todo_mate_item')
  298. ->insertAll($items);
  299. if (!$res) {
  300. \exception('物品使用记录失败');
  301. }
  302. $res = Db::name('f_clean_record_items')
  303. ->insertAll($a);
  304. if (!$res) {
  305. \exception('专项保洁使用记录失败');
  306. }
  307. }
  308. $this->commit();
  309. return true;
  310. } catch (Exception $e) {
  311. $this->rollback();
  312. $this->error = $e->getMessage();
  313. return false;
  314. }
  315. }
  316. //定时处理超时任务
  317. public function timer_action(){
  318. $curTime = date('Y-m-d');
  319. $map[] = ['del','=',0];
  320. $map[] = ['status','in',[0,1]];
  321. $map[] = ['end_time','<',$curTime];
  322. $this->where($map)
  323. ->update(['timeout'=>1]);
  324. }
  325. public function taskCount2($status, $userId, $orgId) {
  326. // $isDispatch = check_is_dispatch($userId);
  327. // if ($isDispatch) {
  328. // $ret = Db::name('clean_plan_record')
  329. // ->where('enable',0)
  330. // ->where('del',0)
  331. // ->where('org_id',$orgId)
  332. // ->count();
  333. // }
  334. // else {
  335. // $ret = Db::name('clean_plan_record')
  336. // ->alias('clr')
  337. // ->join('clean_plan_user cpu','cpu.record_id = clr.id')
  338. // ->where('clr.enable',0)
  339. // ->where('clr.del',0)
  340. // ->where('clr.org_id',$orgId)
  341. // ->where('cpu.user_id',$userId)
  342. // ->count();
  343. // }
  344. $ret = Db::name('f_clean_plan_record')
  345. ->alias('clr')
  346. ->join('f_clean_plan_user cpu','cpu.record_id = clr.id')
  347. ->where('clr.enable',0)
  348. ->where('clr.del',0)
  349. ->where('clr.org_id',$orgId)
  350. ->where('cpu.user_id',$userId)
  351. ->count();
  352. return $ret ? $ret : 0;
  353. }
  354. }