Model.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\products;
  10. use app\admin\service\Service;
  11. use app\common\models\goods\GoodsModel;
  12. use app\common\models\goods\ProductsModel;
  13. use app\common\models\goods\ProductsModelData;
  14. /**
  15. * 商品属性服务类
  16. * Class Model
  17. * @package app\admin\service\products
  18. */
  19. class Model extends Service {
  20. /**
  21. * 获取列表数据
  22. * @param $data
  23. * @return array
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public static function getList($data){
  29. $count = ProductsModel::count();
  30. $list = array_map(function ($res){
  31. $res['count'] = ProductsModelData::where(['pid' => $res['id']])->count();
  32. return $res;
  33. },ProductsModel::page($data["page"]??1,$data["limit"]??10)->order("id","asc")->select()->toArray());
  34. return [ "count"=>$count,"data"=>$list ];
  35. }
  36. /**
  37. * 详情
  38. * @param $id
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public static function detail($id){
  45. $row = ProductsModel::where("id",$id)->find();
  46. if(!empty($row)){
  47. $row["attr"] = ProductsModelData::where("")->where(["pid"=>$row["id"]])->order("sort","ASC")->select()->toArray();
  48. }
  49. return [ "data"=>$row ];
  50. }
  51. /**
  52. * 保存数据
  53. * @param array $data
  54. * @return bool
  55. */
  56. public static function save($data=[]){
  57. if(ProductsModel::where("id",$data["id"])->count()){
  58. ProductsModel::where("id",$data["id"])->save($data);
  59. }else{
  60. unset($data["id"]);
  61. $data["id"] = ProductsModel::create($data)->id;
  62. }
  63. if(empty($data["attr"]["name"])){
  64. return true;
  65. }
  66. $i = 0; $arr = [];
  67. foreach($data["attr"]["name"] as $key=>$val){
  68. $attr = [
  69. "pid"=>$data["id"],
  70. "name"=>$val,
  71. "value"=>$data["attr"]["value"][$key],
  72. "type"=>$data["attr"]["type"][$key],
  73. "sort"=>$i
  74. ];
  75. $model_id = intval($data["attr"]["id"][$key]);
  76. if(ProductsModelData::where("id",$model_id)->count()){
  77. $arr[] = $model_id;
  78. ProductsModelData::where("id",$model_id)->save($attr);
  79. }else{
  80. $arr[] = ProductsModelData::create($attr)->id;
  81. }
  82. $i++;
  83. }
  84. if(!empty($arr)){
  85. ProductsModelData::where('pid',$data["id"])->where("id","not in",$arr)->delete();
  86. }
  87. return true;
  88. }
  89. /**
  90. * 删除
  91. * @param $id
  92. * @return bool
  93. */
  94. public static function delete($id){
  95. if(ProductsModel::where("id",$id)->delete()){
  96. return ProductsModelData::where('pid',$id)->delete();
  97. }
  98. return false;
  99. }
  100. /**
  101. * 获取商品属性数据
  102. * @param $data
  103. * @return array
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public static function getModel($data){
  109. $id = $data["id"]??0;
  110. $goods_id = $data["goods_id"]??0;
  111. $result = ProductsModelData::where(['pid'=>$id])->order("sort","ASC")->select()->toArray();
  112. if(empty($result)){
  113. throw new \Exception("您要查找的模型内容不存在!",0);
  114. }
  115. $goods_attr = [];
  116. $module = GoodsModel::where(["model_id"=>$id,"goods_id"=>$goods_id])->order("sort","ASC")->select()->toArray();
  117. if ($module) {
  118. foreach ($module as $item) {
  119. $goods_attr[$item['attribute_id']] = $item['attribute_value'];
  120. }
  121. }
  122. return [ "goods_attr"=>$goods_attr, "result"=>$result ];
  123. }
  124. }