QualityPlan.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class QualityPlan 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\QualityPlan();
  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('quality_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. $forms = [];
  42. foreach ($v['forms'] as $fk=>$fv){
  43. $forms[] = $fv['id'];
  44. }
  45. $busids = [];
  46. foreach ($v['buss'] as $fk=>$fv){
  47. $busids[] = $fv['id'];
  48. }
  49. $groupId = Db::name('quality_group')->insertGetId([
  50. 'title' => $v['title'],
  51. 'org_id' => $post['org_id'],
  52. 'forms' => $forms?implode(',',$forms):'',
  53. 'busids' => $busids?implode(',',$busids):'',
  54. 'type' => $v['type'],
  55. 'enable' => 1,
  56. 'del' => 0,
  57. 'create_time' => getTime()
  58. ]);
  59. if(!$groupId){
  60. \exception('操作失败');
  61. }
  62. $nforms[] = [
  63. 'quality_group_id' => $groupId
  64. ];
  65. }
  66. foreach ($tasks as $k=>$v){
  67. foreach ($v['list'] as $kk=>$vv){
  68. if($content['dateType']==4 || $content['dateType']==5){//按月,循环周
  69. list($a,$b) = explode('~',$vv['day']);
  70. $stime = $a.' '.$v['time']['stime'];
  71. $etime = $b.' '.$v['time']['etime'];
  72. }else{
  73. $stime = $vv['day'].' '.$v['time']['stime'];
  74. $etime = $vv['day'].' '.$v['time']['etime'];
  75. if($v['time']['stype'] == 1){ // 次日
  76. $nextday = date('Y-m-d',strtotime($vv['day']) + $v['time']['sday']*24*60*60);
  77. $etime = $nextday.' '.$v['time']['etime'];
  78. }
  79. }
  80. $d = [
  81. 'org_id' => $post['org_id'],
  82. 'title' => $post['title'],
  83. 'start_time' => $stime,
  84. 'end_time' => $etime,
  85. 'status' => 0,
  86. 'create_time' => $curDate,
  87. 'update_time' => $curDate,
  88. 'plan_id' => $planId,
  89. 'create_yyyy' => date('Y',strtotime($stime)),
  90. 'create_yyyymm' => date('Ym',strtotime($stime)),
  91. 'create_yyyymmdd' => date('Ymd',strtotime($stime)),
  92. ];
  93. $taskId = Db::name('quality_task')->insertGetId($d);
  94. if(!$taskId){
  95. \exception('操作失败');
  96. }
  97. foreach ($nforms as $key=>$val){
  98. $nforms[$key]['quality_task_id'] = $taskId;
  99. }
  100. $fromRet = Db::name('quality_task_addr')->insertAll($nforms);
  101. if($fromRet != count($nforms)){
  102. \exception('操作失败');
  103. }
  104. $uds = [];
  105. foreach ($vv['userIds'] as $key=>$val){
  106. $uds[] = [
  107. 'quality_task_id' => $taskId,
  108. 'user_id' => $val
  109. ];
  110. }
  111. $userRet = Db::name('quality_task_user')->insertAll($uds);
  112. if($userRet != count($uds)){
  113. \exception('操作失败');
  114. }
  115. }
  116. }
  117. Db::commit();
  118. return true;
  119. }catch (\Exception $e){
  120. // 回滚事务
  121. $this->error = $e->getMessage();
  122. Db::rollback();
  123. return false;
  124. }
  125. }
  126. public function updatesOld($userId){
  127. $post = request()->post();
  128. $post['org_id'] = cur_org_id();
  129. $post['user_id'] = $userId;
  130. $validate = new \app\common\validate\QualityPlan();
  131. $result = $validate->check($post,[]);
  132. if(true !== $result){
  133. $this->error = $validate->getError();
  134. return false;
  135. }
  136. Db::startTrans();
  137. try{
  138. $curDate = date('Y-m-d H:i:s');
  139. $data = [
  140. "title" => $post['title'],
  141. "start_time" => $post['start_time'],
  142. "end_time" => $post['end_time'],
  143. "content" => $post['content'],
  144. "status" => 0,
  145. "org_id" => $post['org_id'],
  146. "user_id" => $post['user_id'],
  147. "create_time" => $curDate,
  148. ];
  149. if($post['start_time'] == date('Y-m-d')){
  150. $data['status'] = 1; // 进行中
  151. }
  152. $planId = Db::name('quality_plan')->insertGetId($data);
  153. if(!$planId){
  154. \exception('操作失败');
  155. }
  156. $content = json_decode($post['content'],true);
  157. $tasks = $content['tasks']['list']; // 任务数组
  158. $addrForms = $content['addrForms'];
  159. $addrForms = list_sort_by($addrForms,'sorts','asc'); // 排序
  160. $nforms = [];
  161. foreach ($addrForms as $k=>$v){
  162. $nforms[] = [
  163. 'quality_group_id' => $v['addrId'],
  164. 'sorts' => (int)$v['sorts']
  165. ];
  166. }
  167. foreach ($tasks as $k=>$v){
  168. foreach ($v['list'] as $kk=>$vv){
  169. $stime = $vv['day'].' '.$v['time']['stime'];
  170. $etime = $vv['day'].' '.$v['time']['etime'];
  171. if($v['time']['stype'] == 1){ // 次日
  172. $nextday = date('Y-m-d',strtotime($vv['day']) + $v['time']['sday']*24*60*60);
  173. $etime = $nextday.' '.$v['time']['etime'];
  174. }
  175. $d = [
  176. 'org_id' => $post['org_id'],
  177. 'title' => $post['title'],
  178. 'start_time' => $stime,
  179. 'end_time' => $etime,
  180. 'status' => 0,
  181. 'create_time' => $curDate,
  182. 'update_time' => $curDate,
  183. 'plan_id' => $planId,
  184. 'create_yyyy' => date('Y',strtotime($stime)),
  185. 'create_yyyymm' => date('Ym',strtotime($stime)),
  186. 'create_yyyymmdd' => date('Ymd',strtotime($stime)),
  187. ];
  188. $taskId = Db::name('quality_task')->insertGetId($d);
  189. if(!$taskId){
  190. \exception('操作失败');
  191. }
  192. foreach ($nforms as $key=>$val){
  193. $nforms[$key]['quality_task_id'] = $taskId;
  194. }
  195. $fromRet = Db::name('quality_task_addr')->insertAll($nforms);
  196. if($fromRet != count($nforms)){
  197. \exception('操作失败');
  198. }
  199. $uds = [];
  200. foreach ($vv['userIds'] as $key=>$val){
  201. $uds[] = [
  202. 'quality_task_id' => $taskId,
  203. 'user_id' => $val
  204. ];
  205. }
  206. $userRet = Db::name('quality_task_user')->insertAll($uds);
  207. if($userRet != count($uds)){
  208. \exception('操作失败');
  209. }
  210. }
  211. }
  212. Db::commit();
  213. return true;
  214. }catch (\Exception $e){
  215. // 回滚事务
  216. $this->error = $e->getMessage();
  217. Db::rollback();
  218. return false;
  219. }
  220. }
  221. // 获取组织某一类型巡更计划
  222. public function getListByMode($orgId){
  223. $lists = Db::name('quality_plan')
  224. ->where('del',0)
  225. ->where('org_id',$orgId)
  226. ->field('id,title,start_time,end_time,status')
  227. ->order('id desc')
  228. ->select();
  229. return $lists?$lists:[];
  230. }
  231. }