| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace app\admin\model\store;
- use crmeb\basic\BaseModel;
- use think\facade\Db;
- /**
- * 设备报修申请表模型
- * 处理单号生成、表单新增、状态更新、列表查询
- */
- class DeviceRepair extends BaseModel
- {
- // 关联数据表名
- protected $name = 'device_repair';
- // 主键字段
- protected $pk = 'id';
- /**
- * 自动生成报修单号(规则:REPAIR+日期+6位随机数,确保唯一)
- * @return string 唯一报修单号
- */
- public static function generateRepairSn()
- {
- $date = date('Ymd'); // 日期:20251021
- $rand = mt_rand(100000, 999999); // 6位随机数
- $sn = "REPAIR{$date}{$rand}";
- // 校验单号唯一性(极端情况重名时重新生成)
- $exists = self::where('repair_sn', $sn)->find();
- if ($exists) {
- return self::generateRepairSn();
- }
- return $sn;
- }
- /**
- * 提交新的报修表单
- * @param array $data 表单数据(device_name/device_number等)
- */
- public static function addRepairForm($data)
- {
- // 补充默认数据
- $submitData = [
- 'repair_sn' => \app\models\store\DeviceRepair::getNewOrderId(), // 自动填单号
- 'device_name' => $data['device_name'],
- 'device_number' => $data['device_number'],
- 'repair_problem' => $data['repair_problem'],
- 'return_address' => $data['return_address'],
- 'contact_name' => $data['contact_name'],
- 'contact_phone' => $data['contact_phone'],
- 'create_time' => time(), // 提交时间
- 'status' => 0, // 初始状态:未处理
- 'order_id' =>$data['order_id'],
- 'product_id' => $data['product_id']
- ];
- // 新增数据
- return self::create($submitData);
- }
- /**
- * 生成充值订单号
- * @param int $uid
- * @return bool|string
- */
- // public static function getNewOrderId($uid = 0)
- // {
- // if (!$uid) return false;
- // $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();
- // return 'bx' . date('YmdHis', time()) . (10000 + $count + $uid);
- // }
- // public static function getNewOrderId()
- // {
- // do {
- // list($msec, $sec) = explode(' ', microtime());
- // $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
- // $orderId = 'bx' . $msectime . mt_rand(10000, 99999);
- // } while (self::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
- // return $orderId;
- // }
- /**
- * 后台获取报修列表(带分页、状态筛选)
- * @param int $page 页码
- * @param int $limit 每页条数
- * @param int $status 处理状态(-1=全部,0=未处理,1=已处理)
- * @return \think\Paginator 分页列表
- */
- public static function getAdminRepairList($page = 1, $limit = 10, $status = -1)
- {
- $query = self::order('create_time DESC'); // 按提交时间倒序
- // 状态筛选(-1表示不筛选)
- if ($status != -1) {
- $query->where('status', $status);
- }
- // 分页查询
- return $query->paginate([
- 'page' => $page,
- 'list_rows' => $limit,
- 'query' => request()->param() // 保留分页链接参数
- ]);
- }
- /**
- * 标记报修单为“已处理”
- * @param int $id 报修单ID
- * @param int $adminId 处理人ID
- * @return bool 成功返回true,失败返回false
- */
- public static function markAsHandled($id, $adminId)
- {
- return self::where('id', $id)->update([
- 'status' => 1,
- 'handle_admin_id' => $adminId,
- 'handle_time' => time()
- ]) > 0;
- }
- /**
- * 根据ID获取单个报修单详情(用于下载)
- * @param int $id 报修单ID
- * @return array|null 详情数据
- */
- public static function getRepairDetailForDownload($id)
- {
- $detail = self::find($id);
- if (!$detail) {
- return null;
- }
- // 格式化时间(时间戳转字符串)
- $detail = $detail->toArray();
- $detail['create_time_text'] = date('Y-m-d H:i:s', $detail['create_time']);
- $detail['handle_time_text'] = $detail['handle_time'] ? date('Y-m-d H:i:s', $detail['handle_time']) : '未处理';
- $detail['status_text'] = $detail['status'] ? '已处理' : '未处理';
- return $detail;
- }
- }
|