CustomerProduct.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. /**
  8. *客户产品
  9. */
  10. class CustomerProduct Extends Model {
  11. use SoftDelete;
  12. // 表名,不含前缀
  13. protected $name = 'qingdongams_customer_product';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. // 最后一个工单idw
  21. protected $append = [
  22. 'last_workorder_id',
  23. 'file_ids_data',
  24. ];
  25. public function getLastWorkorderIdAttr(){
  26. return Workorder::where(['customer_product_id'=>$this->id])->order('id desc')->value('id')??0;
  27. }
  28. //
  29. public function getFileIdsDataAttr($value,$data) {
  30. $files = explode(',', $data['file_ids']??'');
  31. $result = [];
  32. foreach ($files as $fid) {
  33. if ($fid) {
  34. $result[] = ['id'=>$fid,'url'=>cdnurl(File::getUrl($fid), true)];
  35. }
  36. }
  37. return $result;
  38. }
  39. //获取创建时间
  40. public function getCreatetimeAttr($value) {
  41. return date('Y-m-d H:i:s', $value);
  42. }
  43. //
  44. public function getFileIdsAttr($value) {
  45. $files = explode(',', $value);
  46. $result = [];
  47. foreach ($files as $fid) {
  48. if ($fid) {
  49. $result[] = cdnurl(File::getUrl($fid), true);
  50. }
  51. }
  52. return $result;
  53. }
  54. //创建人
  55. public function createStaff() {
  56. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
  57. }
  58. //客户
  59. public function customer() {
  60. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
  61. }
  62. //产品
  63. public function product() {
  64. return $this->hasOne(Product::class, 'id', 'product_id')->with('productType')->field('id,name,unit,num,img,price,description,type_id');
  65. }
  66. //合同
  67. public function contracts() {
  68. return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num,money,order_date,end_time,start_time');
  69. }
  70. public function equipment() {
  71. return $this->hasOne(Equipment::class, 'number', 'number')->field('id,number,logo');
  72. }
  73. //创建客户
  74. public static function createProduct($params) {
  75. $staff = Staff::info();
  76. if(!empty($staff)){
  77. $params['create_staff_id'] = $staff->id;
  78. }
  79. $customer = new self;
  80. // 调用当前模型对应的User验证器类进行数据验证
  81. $result = $customer->allowField(true)->save($params);
  82. $lastId=$customer->getLastInsID();
  83. if (false === $result) {
  84. // 验证失败 输出错误信息
  85. throw new Exception($customer->getError());
  86. }
  87. return $lastId;
  88. }
  89. public function getStatusAttr($value){
  90. $list = [0=>'待发货',1=>'发货中',2=>'待安装',3=>'安装成功',4=>'旧机维修',9=>'已删除'];
  91. return $list[$value]??'';
  92. }
  93. /**
  94. *修改客户信息
  95. */
  96. public static function updateProduct($params) {
  97. $customer = new self;
  98. // 调用当前模型对应的User验证器类进行数据验证
  99. $result = $customer->save($params, ['id' => $params['id']]);
  100. if (false === $result) {
  101. // 验证失败 输出错误信息
  102. throw new Exception($customer->getError());
  103. }
  104. return true;
  105. }
  106. //是否可以修改产品编号
  107. public static function isEditNumber($id){
  108. $product=CustomerProduct::where(['id'=>$id,'status'=>['in',[0,1]]])->find();
  109. if(empty($product)){
  110. return false;
  111. }
  112. return true;
  113. }
  114. }