ServiceWorkSparePartLists.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\workerapi\lists;
  3. use app\common\lists\ListsSearchInterface;
  4. use app\common\model\spare_part\SparePart;
  5. use app\common\model\works\ServiceWork;
  6. use app\common\model\works\ServiceWorkSpare;
  7. /**
  8. * SparePart列表
  9. * Class SparePartLists
  10. * @package app\workerapi\lists
  11. */
  12. class ServiceWorkSparePartLists extends BaseWorkerDataLists implements ListsSearchInterface
  13. {
  14. protected $count = 0;
  15. /**
  16. * 设置搜索条件
  17. * @return array[]
  18. * @author liuguanci
  19. * @date 2024/7/11 下午1:46
  20. */
  21. public function setSearch(): array
  22. {
  23. if(isset($this->params['user_id']) && !empty($this->params['user_id'])){
  24. $this->params['master_worker_id'] = $this->params['user_id'];
  25. }
  26. return [
  27. '=' => ['work_sn','master_worker_id'],
  28. ];
  29. }
  30. /*
  31. * 查询条件
  32. * @return array
  33. * @author liuguanci
  34. * @date 2024/7/11 下午1:46
  35. */
  36. public function queryWhere(){
  37. $where = [];
  38. if(isset($this->params['spare_name']) && !empty($this->params['spare_name'])){
  39. $where[] = ['spare_name','like','%'.$this->params['spare_name'].'%'];
  40. }
  41. return $where;
  42. }
  43. /**
  44. * @notes 获取列表
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @author likeadmin
  50. * @date 2024/09/14 15:08
  51. */
  52. public function lists(): array
  53. {
  54. // 配件信息
  55. $spare_parts = [];
  56. $service_work_spare_id = ServiceWork::where($this->searchWhere)->value('service_work_spare_id');
  57. if($service_work_spare_id){
  58. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$service_work_spare_id)->value('spare_parts'),true);
  59. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  60. ->where($this->queryWhere())
  61. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  62. ->select()
  63. ->toArray();
  64. $spare_parts = array_column($spare_parts,null,'id');
  65. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  66. isset($spare_parts[$k]) && $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  67. }
  68. $this->count = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  69. ->where($this->queryWhere())
  70. ->count();
  71. }
  72. return $spare_parts?array_values($spare_parts):[];
  73. }
  74. /**
  75. * @notes 获取数量
  76. * @return int
  77. * @author likeadmin
  78. * @date 2024/09/14 15:08
  79. */
  80. public function count(): int
  81. {
  82. return $this->count;
  83. }
  84. }