ServiceTime.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class ServiceTime extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $result = validate('ServiceTime')->check($data,[],'');
  9. if(true !== $result){
  10. $this->error = validate('ServiceTime')->getError();
  11. return false;
  12. }
  13. if($data['start'] > $data['end']){
  14. $this->error = '开始时间不能大于结束时间';
  15. return false;
  16. }
  17. $id = $data['id'];
  18. unset($data['id']);
  19. if($id > 0){
  20. $data['update_time'] = date('Y-m-d H:i:s');
  21. $info = db('service_time')
  22. ->where('org_id',cur_org_id())
  23. ->where('del','=',0)
  24. ->where('id','<>',$id)
  25. ->where('day','=',$data['day'])
  26. ->where('start','<',$data['end'])
  27. ->where('end','>',$data['start'])
  28. ->find();
  29. if($info) {
  30. $this->error = '时间有重复';
  31. return false;
  32. }
  33. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  34. if(!$ret){
  35. $this->error = '操作失败';
  36. return false;
  37. }
  38. }else{
  39. $days = $data['days'];
  40. $sData = [[
  41. 'org_id' => cur_org_id(),
  42. 'service_id' => $data['service_id'],
  43. 'start' => $data['start'],
  44. 'end' => $data['end'],
  45. 'limit' => $data['limit'],
  46. 'day' => $data['day'],
  47. 'enable' => $data['enable'],
  48. 'create_time' => date('Y-m-d H:i:s')
  49. ]];
  50. if($days > 0){
  51. for($i=1;$i<=$days;$i++){
  52. $day = date('Y-m-d',strtotime($data['day']) + $i*24*60*60);
  53. $info = Db::name('service_time')
  54. ->where('org_id',cur_org_id())
  55. ->where('del','=',0)
  56. ->where('day','=',$day)
  57. ->where('start','<',$data['end'])
  58. ->where('end','>',$data['start'])
  59. ->find();
  60. if($info){
  61. $this->error = '时间有重复';
  62. return false;
  63. }
  64. $sData[] = [
  65. 'org_id' => cur_org_id(),
  66. 'service_id' => $data['service_id'],
  67. 'start' => $data['start'],
  68. 'end' => $data['end'],
  69. 'limit' => $data['limit'],
  70. 'day' => $day,
  71. 'enable' => $data['enable'],
  72. 'create_time' => date('Y-m-d H:i:s')
  73. ];
  74. }
  75. }
  76. // 启动事务
  77. Db::startTrans();
  78. try{
  79. $add = Db::name('service_time')->insertAll($sData);
  80. if(!$add){
  81. exception('操作失败');
  82. }
  83. // 提交事务
  84. Db::commit();
  85. }catch (\Exception $e) {
  86. Db::rollback();
  87. $this->error = $e->getMessage();
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. }