DeviceRepair.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\admin\model\store;
  3. use think\Model;
  4. use think\facade\Db;
  5. /**
  6. * 设备报修申请表模型
  7. * 处理单号生成、表单新增、状态更新、列表查询
  8. */
  9. class DeviceRepair extends Model
  10. {
  11. // 关联数据表名
  12. protected $name = 'device_repair';
  13. // 主键字段
  14. protected $pk = 'id';
  15. /**
  16. * 自动生成报修单号(规则:REPAIR+日期+6位随机数,确保唯一)
  17. * @return string 唯一报修单号
  18. */
  19. public static function generateRepairSn()
  20. {
  21. $date = date('Ymd'); // 日期:20251021
  22. $rand = mt_rand(100000, 999999); // 6位随机数
  23. $sn = "REPAIR{$date}{$rand}";
  24. // 校验单号唯一性(极端情况重名时重新生成)
  25. $exists = self::where('repair_sn', $sn)->find();
  26. if ($exists) {
  27. return self::generateRepairSn();
  28. }
  29. return $sn;
  30. }
  31. /**
  32. * 提交新的报修表单
  33. * @param array $data 表单数据(device_name/device_number等)
  34. * @return false|Model 成功返回模型实例,失败返回false
  35. */
  36. public static function addRepairForm($data)
  37. {
  38. // 补充默认数据
  39. $submitData = [
  40. 'repair_sn' => self::getNewOrderId(), // 自动填单号
  41. 'device_name' => $data['device_name'],
  42. 'device_number' => $data['device_number'],
  43. 'repair_problem' => $data['repair_problem'],
  44. 'return_address' => $data['return_address'],
  45. 'contact_name' => $data['contact_name'],
  46. 'contact_phone' => $data['contact_phone'],
  47. 'create_time' => strval(time()), // 提交时间
  48. 'status' => 0 // 初始状态:未处理
  49. ];
  50. var_dump($submitData);die();
  51. // 新增数据
  52. return self::create($submitData);
  53. }
  54. /**
  55. * 生成充值订单号
  56. * @param int $uid
  57. * @return bool|string
  58. */
  59. public static function getNewOrderId($uid = 0)
  60. {
  61. if (!$uid) return false;
  62. $count = (int)self::where('uid', $uid)->where('add_time', '>=', strtotime(date("Y-m-d")))->where('add_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
  63. return 'bx' . date('YmdHis', time()) . (10000 + $count + $uid);
  64. }
  65. /**
  66. * 后台获取报修列表(带分页、状态筛选)
  67. * @param int $page 页码
  68. * @param int $limit 每页条数
  69. * @param int $status 处理状态(-1=全部,0=未处理,1=已处理)
  70. * @return \think\Paginator 分页列表
  71. */
  72. public static function getAdminRepairList($page = 1, $limit = 10, $status = -1)
  73. {
  74. $query = self::order('create_time DESC'); // 按提交时间倒序
  75. // 状态筛选(-1表示不筛选)
  76. if ($status != -1) {
  77. $query->where('status', $status);
  78. }
  79. // 分页查询
  80. return $query->paginate([
  81. 'page' => $page,
  82. 'list_rows' => $limit,
  83. 'query' => request()->param() // 保留分页链接参数
  84. ]);
  85. }
  86. /**
  87. * 标记报修单为“已处理”
  88. * @param int $id 报修单ID
  89. * @param int $adminId 处理人ID
  90. * @return bool 成功返回true,失败返回false
  91. */
  92. public static function markAsHandled($id, $adminId)
  93. {
  94. return self::where('id', $id)->update([
  95. 'status' => 1,
  96. 'handle_admin_id' => $adminId,
  97. 'handle_time' => time()
  98. ]) > 0;
  99. }
  100. /**
  101. * 根据ID获取单个报修单详情(用于下载)
  102. * @param int $id 报修单ID
  103. * @return array|null 详情数据
  104. */
  105. public static function getRepairDetailForDownload($id)
  106. {
  107. $detail = self::find($id);
  108. if (!$detail) {
  109. return null;
  110. }
  111. // 格式化时间(时间戳转字符串)
  112. $detail = $detail->toArray();
  113. $detail['create_time_text'] = date('Y-m-d H:i:s', $detail['create_time']);
  114. $detail['handle_time_text'] = $detail['handle_time'] ? date('Y-m-d H:i:s', $detail['handle_time']) : '未处理';
  115. $detail['status_text'] = $detail['status'] ? '已处理' : '未处理';
  116. return $detail;
  117. }
  118. }