GreenPlan.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class GreenPlan extends Base {
  6. public function updates($userId){
  7. $post = request()->post();
  8. $post['org_id'] = cur_org_id();
  9. $post['user_id'] = $userId;
  10. $validate = new \app\common\validate\GreenPlan();
  11. $result = $validate->check($post,[]);
  12. if(true !== $result){
  13. $this->error = $validate->getError();
  14. return false;
  15. }
  16. Db::startTrans();
  17. try{
  18. $curDate = date('Y-m-d H:i:s');
  19. $data = [
  20. "title" => $post['title'],
  21. "start_time" => $post['start_time'],
  22. "end_time" => $post['end_time'],
  23. "content" => $post['content'],
  24. "status" => 0,
  25. "org_id" => $post['org_id'],
  26. "user_id" => $post['user_id'],
  27. "create_time" => $curDate
  28. ];
  29. if($post['start_time'] == date('Y-m-d')){
  30. $data['status'] = 1; // 进行中
  31. }
  32. $planId = Db::name('green_plan')->insertGetId($data);
  33. if(!$planId){
  34. \exception('操作失败');
  35. }
  36. $content = json_decode($post['content'],true);
  37. $tasks = $content['tasks']['list']; // 任务数组
  38. $addrForms = $content['addrForms'];
  39. $nforms = [];
  40. foreach ($addrForms as $k=>$v){
  41. $nforms[] = [
  42. 'green_addr_id' => $v['addrId'],
  43. 'green_form_id' => $v['formId']
  44. ];
  45. }
  46. foreach ($tasks as $k=>$v){
  47. foreach ($v['list'] as $kk=>$vv){
  48. $stime = $vv['day'].' '.$v['time']['stime'];
  49. $etime = $vv['day'].' '.$v['time']['etime'];
  50. if($v['time']['stype'] == 1){ // 次日
  51. $nextday = date('Y-m-d',strtotime($vv['day']) + $v['time']['sday']*24*60*60);
  52. $etime = $nextday.' '.$v['time']['etime'];
  53. }
  54. $d = [
  55. 'org_id' => $post['org_id'],
  56. 'title' => $post['title'],
  57. 'start_time' => $stime,
  58. 'end_time' => $etime,
  59. 'status' => 0,
  60. 'create_time' => $curDate,
  61. 'update_time' => $curDate,
  62. 'plan_id' => $planId,
  63. 'create_yyyy' => date('Y',strtotime($stime)),
  64. 'create_yyyymm' => date('Ym',strtotime($stime)),
  65. 'create_yyyymmdd' => date('Ymd',strtotime($stime)),
  66. ];
  67. $taskId = Db::name('green_task')->insertGetId($d);
  68. if(!$taskId){
  69. \exception('操作失败');
  70. }
  71. foreach ($nforms as $key=>$val){
  72. $nforms[$key]['green_task_id'] = $taskId;
  73. }
  74. $fromRet = Db::name('green_task_addr')->insertAll($nforms);
  75. if($fromRet != count($nforms)){
  76. \exception('操作失败');
  77. }
  78. $uds = [];
  79. foreach ($vv['userIds'] as $key=>$val){
  80. $uds[] = [
  81. 'green_task_id' => $taskId,
  82. 'user_id' => $val
  83. ];
  84. }
  85. $userRet = Db::name('green_task_user')->insertAll($uds);
  86. if($userRet != count($uds)){
  87. \exception('操作失败');
  88. }
  89. }
  90. }
  91. Db::commit();
  92. return true;
  93. }catch (\Exception $e){
  94. // 回滚事务
  95. $this->error = $e->getMessage();
  96. Db::rollback();
  97. return false;
  98. }
  99. }
  100. // 获取组织绿化计划
  101. public function getList($orgId){
  102. $lists = Db::name('green_plan')
  103. ->where('del',0)
  104. ->where('org_id',$orgId)
  105. ->field('id,title,start_time,end_time,status')
  106. ->order('id desc')
  107. ->select();
  108. return $lists?$lists:[];
  109. }
  110. }