ContractProduct.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use addons\qingdongams\model\Product;
  4. use addons\qingdongams\model\ProductPart;
  5. use think\Model;
  6. /**
  7. *合同关联产品
  8. */
  9. class ContractProduct Extends Model {
  10. // 表名,不含前缀
  11. protected $name = 'qingdongams_contract_product';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. //产品
  15. public function product() {
  16. return $this->hasOne(Product::class, 'id', 'product_id')
  17. ->bind('name,img,num,unit,wholesale');
  18. }
  19. //合同
  20. public function contract() {
  21. return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,num,name');
  22. }
  23. //产品详情
  24. public function productinfo(){
  25. return $this->belongsTo(Product::class,'product_id','id')
  26. ->field('id,goods_id,name,type,unit,price,cost_price')->with(['goods']);
  27. }
  28. //获取产品配置
  29. public function getPartsAttr($value)
  30. {
  31. $value = json_decode($value, true);
  32. if(empty($value)){
  33. return $value;
  34. }
  35. $part_ids = [];
  36. foreach ($value as $v) {
  37. $part_ids[] = $v['part_id'];
  38. }
  39. $model=new ProductPart();
  40. $product_part = $model->where(['id' => ['in', $part_ids]])->column('name,img', 'id');
  41. foreach ($value as $k => $v) {
  42. if (isset($product_part[$v['part_id']])) {
  43. $v['name'] = $product_part[$v['part_id']]['name'];
  44. $v['img'] = cdnurl($product_part[$v['part_id']]['img'], true);
  45. }
  46. $value[$k] = $v;
  47. }
  48. return $value;
  49. }
  50. //导入
  51. public static function importProduct($data) {
  52. $model = new self;
  53. // 调用当前模型对应的User验证器类进行数据验证
  54. $result = $model->allowField(true)->insertAll($data);
  55. return $result;
  56. }
  57. }