BusinessProduct.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 BusinessProduct Extends Model
  10. {
  11. use SoftDelete;
  12. // 表名,不含前缀
  13. protected $name = 'qingdongams_business_product';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. //产品
  21. public function product(){
  22. return $this->belongsTo(Product::class,'product_id','id', [], 'LEFT')->setEagerlyType(0);
  23. }
  24. //上级
  25. public function business(){
  26. return $this->belongsTo(Business::class,'business_id','id');
  27. }
  28. //产品
  29. public function productOne() {
  30. return $this->hasOne(Product::class, 'id', 'product_id')->bind('name,img,num,unit,wholesale');
  31. }
  32. //产品详情
  33. public function productinfo(){
  34. return $this->belongsTo(Product::class,'product_id','id')
  35. ->field('id,goods_id,name,type,unit,cost_price,price,wholesale')->with(['goods']);
  36. }
  37. //获取产品配置
  38. public function getPartsAttr($value)
  39. {
  40. $value = json_decode($value, true);
  41. if(empty($value)){
  42. return $value;
  43. }
  44. $part_ids = [];
  45. foreach ($value as $v) {
  46. $part_ids[] = $v['part_id'];
  47. }
  48. $model=new ProductPart();
  49. $product_part = $model->where(['id' => ['in', $part_ids]])->column('name,img', 'id');
  50. foreach ($value as $k => $v) {
  51. if (isset($product_part[$v['part_id']])) {
  52. $v['name'] = $product_part[$v['part_id']]['name'];
  53. $v['img'] = cdnurl($product_part[$v['part_id']]['img'], true);
  54. }
  55. $value[$k] = $v;
  56. }
  57. return $value;
  58. }
  59. //导入
  60. public static function importProduct($data) {
  61. $model = new self;
  62. // 调用当前模型对应的User验证器类进行数据验证
  63. $result = $model->allowField(true)->insertAll($data);
  64. return $result;
  65. }
  66. }