Parts.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *零件表
  8. */
  9. class Parts Extends Model
  10. {
  11. use SoftDelete;
  12. protected $name = 'qingdongams_parts';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. public function staff(){
  20. return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,name,img');
  21. }
  22. //创建人
  23. public function createStaff() {
  24. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
  25. }
  26. public function getImgAttr($value) {
  27. if ($value) {
  28. return cdnurl(File::getUrl($value), true);
  29. } else {
  30. return $value;
  31. }
  32. }
  33. //获取产品
  34. public function getProductIdAttr($value) {
  35. $value=explode(',',$value);
  36. $names=Product::where(['id'=>['in',$value]])->column('name');
  37. return implode(',',$names);
  38. }
  39. //创建零件
  40. public static function createParts($params) {
  41. $staff = Staff::info();
  42. if (!empty($staff)) {
  43. $params['create_staff_id'] = $staff->id;
  44. $params['update_staff_id'] = $staff->id;
  45. }
  46. $customer = new self;
  47. // 调用当前模型对应的User验证器类进行数据验证
  48. $result = $customer->allowField(true)->save($params);
  49. $lastId = $customer->getLastInsID();
  50. if (false === $result) {
  51. // 验证失败 输出错误信息
  52. throw new Exception($customer->getError());
  53. }
  54. return $lastId;
  55. }
  56. /**
  57. *修改零件信息
  58. */
  59. public static function updateParts($params) {
  60. $customer = new self;
  61. // 调用当前模型对应的User验证器类进行数据验证
  62. $result = $customer->save($params, ['id' => $params['id']]);
  63. if (false === $result) {
  64. // 验证失败 输出错误信息
  65. throw new Exception($customer->getError());
  66. }
  67. return true;
  68. }
  69. }