CabinetModel.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. use app\index\model\BoxModel;
  5. use think\exception\PDOException;
  6. class CabinetModel extends Model{
  7. // 确定链接表名
  8. protected $name = 'cabinet';
  9. /**
  10. * 插入柜子信息
  11. * @param $param
  12. */
  13. public function insertCabinet($url,$alias,$count,$number,$login_key)
  14. {
  15. try
  16. {
  17. $this->startTrans();
  18. $result = $this->save(['req_url' => $url,'alias' => $alias,'box_count' => $count,'cabinet_code' => $number,'login_key' => $login_key]);
  19. if(false === $result){
  20. $this->rollback();
  21. return msg(-1, '', $this->getError());
  22. }
  23. else
  24. {
  25. $cabinet_number = intval($this->id);
  26. $boxData = [];
  27. for ($i=1; $i <= $count; $i++) {
  28. $boxData['cabinet_number'] = $cabinet_number;
  29. $boxData['box_number'] = $i;
  30. $saveAllData[] = $boxData;
  31. }
  32. $BoxModel = new BoxModel();
  33. $result = $BoxModel->saveAll($saveAllData);
  34. if(false === $result){
  35. $this->rollback();
  36. return msg(-1, '', $this->getError());
  37. }else{
  38. $this->commit();
  39. return msg(1, '', '添加成功');
  40. }
  41. }
  42. }
  43. catch(PDOException $e){
  44. $this->rollback();
  45. return msg(-2, '', $e->getMessage());
  46. }
  47. }
  48. /**
  49. * 根据搜索条件获取柜子列表信息
  50. * @param $where
  51. * @param $offset
  52. * @param $limit
  53. */
  54. public function getCabinetsByWhere()
  55. {
  56. return $this->order('id desc')->select();
  57. }
  58. /**
  59. * 根据搜索条件获取所有的柜子数量
  60. * @param $where
  61. */
  62. public function getAllCabinets()
  63. {
  64. return $this->count();
  65. }
  66. public function DelCabinetBox($cabinet_number){
  67. try {
  68. $this->startTrans();
  69. $ret = $this->where('id' , $cabinet_number)->delete($cabinet_number);
  70. if(false === $ret){
  71. $this->rollback();
  72. return msg(-1, '', $this->getError());
  73. }else{
  74. $BoxModel = new BoxModel();
  75. $ret = $BoxModel->where('cabinet_number' , $cabinet_number)->delete();
  76. if(false === $ret){
  77. $this->rollback();
  78. return msg(-1, '', $this->getError());
  79. }else{
  80. $this->commit();
  81. return msg(1, '', '删除成功');
  82. }
  83. }
  84. }catch(PDOException $e){
  85. $this->rollback();
  86. return msg(-2, '', $e->getMessage());
  87. }
  88. }
  89. }