SiteProduct.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\admin;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class SiteProduct extends BaseModel
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. //仓库
  16. private $warehouse;
  17. private $sassid;
  18. public function setSassId($sassid) {
  19. $this->sassid = $sassid;
  20. }
  21. /**
  22. * 获取列表数据
  23. * @param $page
  24. * @param $where
  25. * @param $pageCount
  26. * @param $desc
  27. */
  28. public function getList($page = 1,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
  29. $data =
  30. $this->field("sp.id,sp.p_id,sp.price,p.title,p.commission as pro_price,sp.status,sp.sales,p.count,p.img,p.add_time,p.wget,p.seq,p.warehouse_ids,sp.ver_bug_count,(SELECT cate_name from table_category where p.cate_id = id) as cate_name")
  31. ->alias("sp")
  32. ->join("product p","p.id = sp.p_id","left")
  33. // ->where('sp.status',1)
  34. ->when(1 == 1,function ($query) use($where){
  35. if(!empty($where['warehouse'])) {
  36. $query->whereRaw('FIND_IN_SET(:id,p.warehouse_ids)',['id'=>$where['warehouse']]);
  37. }
  38. if(!empty($where['name'])){
  39. $query->whereLike('p.title',"%{$where['name']}%");
  40. }
  41. if($where['artType']=='open'){
  42. $query->where('sp.status',1);
  43. }
  44. if($where['artType']=='stop'){
  45. $query->where('sp.status',0);
  46. }
  47. })
  48. ->where('sp.sassid',$this->sassid)
  49. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  50. ->toArray();
  51. foreach ($data['data'] as $k=>$v) {
  52. $data['data'][$k]['warehouseAr'] = $this->getWarehouse($v['warehouse_ids']);
  53. $data['data'][$k]['add_time'] = date('Y-m-d',$v['add_time']);
  54. }
  55. return [$data['total'],$data['data']];
  56. }
  57. /**
  58. * 获取仓库数据
  59. * @param $ids
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. private function getWarehouse($ids) {
  66. $idAr = explode(',',$ids);
  67. $rAr = [];
  68. if(empty($this->warehouse))
  69. $this->warehouse = (new Warehouse)->select()->toArray();
  70. foreach ($this->warehouse as $v) {
  71. if(in_array($v['id'],$idAr)) {
  72. $rAr[] = $v;
  73. }
  74. }
  75. return $rAr;
  76. }
  77. /**
  78. * 获取商品详情
  79. * @param type $post
  80. */
  81. public function getProInfo($post){
  82. if(empty($post["id"])){
  83. return self::setErrorInfo('商品不存在');
  84. }
  85. $data=$this
  86. ->field("sp.id,sp.p_id,p.csno,p.title,p.tag,p.cate_id,p.img,p.unit_name,p.count,p.commission as pro_price,p.wget,p.warehouse_ids,sp.price,sp.sales,sp.ver_bug_count,sp.status,sp.is_host,sp.is_new,(SELECT cate_name from table_category where p.cate_id = id) as cate_name")
  87. ->alias("sp")
  88. ->join("product p","p.id = sp.p_id","left")
  89. ->where('sp.id',$post["id"])
  90. ->where('sp.sassid',$this->sassid)
  91. ->find()
  92. ->toArray();
  93. $data['img'] = explode(',',$data['img']);
  94. if(!empty($data['warehouse_ids'])) {
  95. $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
  96. $data['warehouse'] = $warehouse;
  97. } else {
  98. $data['warehouse'] = [];
  99. }
  100. return $data;
  101. }
  102. public function saveProduct($post){
  103. if(empty($post["id"])){
  104. return self::setErrorInfo('商品不存在');
  105. }
  106. $id = $post['id'];
  107. if(intval($post["ver_bug_count"])<0){
  108. return self::setErrorInfo('虚拟数量不符合');
  109. }
  110. if(!is_numeric($post["price"]) || floatval($post["price"]) < 0){
  111. return self::setErrorInfo('商品售价不符合');
  112. }
  113. //判断价格
  114. $pro = $this->alias("sp")
  115. ->field("p.commission")
  116. ->join("product p","p.id = sp.p_id","left")
  117. ->where('sp.id',$id)
  118. ->find();
  119. if(empty($pro)) {
  120. return self::setErrorInfo('找不到对于产品');
  121. }
  122. if($pro['commission'] > $post["price"]) {
  123. return self::setErrorInfo('不能低于商品成本价');
  124. }
  125. $save = [];
  126. $save['price'] = $post['price'];
  127. $save['ver_bug_count'] = $post['ver_bug_count'];
  128. $save['status'] = $post['status'];
  129. $save['is_host'] = $post['is_host'];
  130. $save['is_new'] = $post['is_new'];
  131. unset($post['id']);
  132. $this->where('id',$id)->where("sassid",$this->sassid)->save($save);
  133. return true;
  134. }
  135. }