CustomCompany.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 客户资质模型
  6. *
  7. */
  8. class CustomCompany extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'custom_company';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 编码转id
  20. *
  21. * @param string $code 编码
  22. *
  23. */
  24. public function codeToId($code){
  25. return intval(str_ireplace('klzz','',$code));
  26. }
  27. /**
  28. * id转编码
  29. *
  30. * @param int $id 编码
  31. *
  32. */
  33. public function idToCode($id){
  34. return 'klzz'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  35. }
  36. /**
  37. * 添加数据
  38. *
  39. */
  40. public function add($data)
  41. {
  42. // 时间
  43. $data['insert_time'] = time();
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $id = $this->query()->insertGetId($data);
  47. // 返回结果
  48. return $id;
  49. }
  50. /**
  51. * 添加数据
  52. *
  53. */
  54. public function edit($id,$data)
  55. {
  56. // 更新时间
  57. $data['update_time'] = time();
  58. // 写入数据表
  59. $result = $this->query()->where(['id'=>$id])->update($data);
  60. // 返回结果
  61. return $result;
  62. }
  63. /**
  64. * 添加数据
  65. *
  66. */
  67. public function upsertOne($data)
  68. {
  69. // // 数据
  70. // $data['uid'] = $uid;
  71. $data['insert_time'] = time();
  72. $data['update_time'] = time();
  73. // 获取结果
  74. $uid = $this->query()->upsert($data,'custom_uid',['remark','remark_company','license_name','license_code','license_area','license_addr','legal_name','status','update_time']);
  75. // 如果存在的话,更新
  76. if( $uid ) return $uid;
  77. // 如果不存在的话,新增
  78. // return $this->add($data);
  79. }
  80. /**
  81. * 查询某条数据
  82. *
  83. */
  84. public function getOne($uid){
  85. // 返回结果
  86. $result = $this->query()->where([['custom_uid','=',$uid]])->first(['id','custom_uid','remark','remark_company','license_name','license_code','license_area','license_addr','legal_name','status','update_time']);
  87. // 返回结果
  88. return $result;
  89. }
  90. /**
  91. * 添加数据
  92. *
  93. * @param int $uid 客户ID
  94. * @param string $field 字段
  95. */
  96. public function getValue($uid,$field)
  97. {
  98. // 返回结果
  99. $result = $this->query()->where([['custom_uid','=',$uid]])->value($field);
  100. // 数据结构
  101. return $result;
  102. }
  103. }