Attribute.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 mall\utils\Data;
  12. use mall\utils\Tool;
  13. use app\common\models\goods\Attribute as AttributeModel;
  14. use app\common\models\goods\AttributeData as AttributeDataModel;
  15. use app\common\models\goods\GoodsAttribute as GoodsAttributeModel;
  16. use app\common\models\goods\GoodsItem as GoodsItemModel;
  17. class Attribute extends Service {
  18. /**
  19. * 获取列表数据
  20. * @param $data
  21. * @param array $condition
  22. * @return array
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public static function getList($data,$condition=[]){
  28. $count = AttributeModel::where($condition)->count();
  29. $result = AttributeModel::where($condition)->page($data["page"]??1,$data["limit"]??10)->order("id","asc")->select()->toArray();
  30. $list = [];
  31. foreach($result as $key=>$value){
  32. $list[] = $value;
  33. $children = self::getChildren($value["id"],null);
  34. $arr = Data::analysisTree(Data::familyProcess($children,[],$value["id"]));
  35. array_splice($list, count($list), 0, $arr);
  36. }
  37. foreach($list as $key=>$item){
  38. $list[$key]['name'] = (empty($item['level']) ? '' : $item['level']) . $item["name"];
  39. $list[$key]['count'] = AttributeModel::where(["pid"=>$item["id"]])->count();
  40. }
  41. return [ "count"=>$count, "data"=>$list ];
  42. }
  43. /**
  44. * 详情
  45. * @param $id
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function detail($id){
  52. $row = AttributeModel::where("id",$id)->find();
  53. if(!empty($row)){
  54. $row["attr"] = AttributeDataModel::where(["pid"=>$row["id"]])->order("sort","ASC")->select()->toArray();
  55. }
  56. return [ "ch"=>AttributeModel::where(['pid'=>0])->select()->toArray(), "data"=>$row ];
  57. }
  58. /**
  59. * 保存数据
  60. * @param array $data
  61. * @return bool
  62. */
  63. public static function save($data=[]){
  64. if(AttributeModel::where("id",$data["id"])->count()){
  65. AttributeModel::where("id",$data["id"])->save($data);
  66. }else{
  67. unset($data["id"]);
  68. $data["id"] = AttributeModel::create($data)->id;
  69. }
  70. $i = 0; $arr = [];
  71. if(!empty($data["attr"]["name"])){
  72. foreach($data["attr"]["name"] as $key=>$val){
  73. $attr = [ "pid"=>$data["id"], "value"=>$val, "sort"=>$i ];
  74. $attr_id = intval($data["attr"]["id"][$key]);
  75. if(AttributeDataModel::where("id",$attr_id)->count()){
  76. $arr[] = $attr_id;
  77. AttributeDataModel::where("id",$attr_id)->save($attr);
  78. }else{
  79. $arr[] = AttributeDataModel::create($attr)->id;
  80. }
  81. $i++;
  82. }
  83. }
  84. if(!empty($arr)){
  85. AttributeDataModel::where('pid',$data["id"])->where('id','not in',$arr)->delete();
  86. }
  87. return true;
  88. }
  89. /**
  90. * 删除
  91. * @param $id
  92. * @return bool
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public static function delete($id){
  98. $row = AttributeModel::where('id',$id)->find();
  99. if(empty($row)){
  100. throw new \Exception("您要查找的数据不存在!",0);
  101. }
  102. AttributeModel::where("id",$id)->delete();
  103. AttributeDataModel::where('pid',$id)->delete();
  104. return true;
  105. }
  106. /**
  107. * @param $id
  108. * @param string $field
  109. * @param array $res
  110. * @return array|mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. protected static function getChildren($id,$field='id',$res=[]){
  116. $list = AttributeModel::where(["pid"=>$id,"status"=>0])->select()->toArray();
  117. foreach($list as $key=>$value){
  118. if(!empty($field) && isset($value[$field])){
  119. $res[] = $value[$field];
  120. $res = array_merge($res,self::getChildren($value['id'],$field));
  121. }else{
  122. $res[] = $value;
  123. $res = array_merge($res,self::getChildren($value['id'],$field));
  124. }
  125. }
  126. return $res;
  127. }
  128. /**
  129. * 获取规格数据
  130. * @param $data
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public static function getAttr($data){
  137. $id = $data["id"]??0;
  138. $goods_id = $data["goods_id"]??0;
  139. $result = [];
  140. $rs = AttributeModel::where(["pid"=>$id])->select()->toArray();
  141. foreach($rs as $val){
  142. $result[$val['id']]["data"] = AttributeModel::where(["id"=>$val["id"]])->find();
  143. $result[$val['id']]["item"] = AttributeDataModel::where(["pid"=>$val["id"]])->order("sort","ASC")->select()->toArray();
  144. }
  145. $attr = GoodsAttributeModel::where(["goods_id"=>$goods_id])->select()->toArray();
  146. $spec_id = [];
  147. foreach($attr as $val){
  148. $spec_id[] = $val["attr_data_id"];
  149. }
  150. return [ "spec_checked"=>$spec_id, "result"=>$result ];
  151. }
  152. /**
  153. * 生成商品规格数据
  154. * @param $data
  155. * @return array
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public static function getAttrData($data){
  161. $id = $data["id"]??0;
  162. $t = $data["t"]??0;
  163. $goods_id = $data["goods_id"]??0;
  164. $in = array_map("intval", explode(",", $id));
  165. if(!$t){
  166. $a = [];
  167. $goods_attribute = GoodsAttributeModel::where(["goods_id"=>$goods_id])->select()->toArray();
  168. foreach($goods_attribute as $val){
  169. $a[] = $val["attr_data_id"];
  170. }
  171. $in = array_merge($in,$a);
  172. }
  173. $result = AttributeDataModel::where("id",'in',$in)->order("sort","ASC")->select()->toArray();
  174. if (empty($result)) {
  175. throw new \Exception("Data is empty",1);
  176. }
  177. $arr = [];
  178. $shop_attribute_item = [];
  179. foreach ($result as $val) {
  180. $shop_attribute_item[] = $val["pid"];
  181. }
  182. $shop_attribute = AttributeModel::where('id','in',$shop_attribute_item)->select()->toArray();
  183. foreach ($result as $val) {
  184. foreach ($shop_attribute as $item) {
  185. if ($val['pid'] == $item['id']) {
  186. $arr[$val["pid"]][$val["id"]] = $item['name'] . ";;;" . $val["value"] . ';;;' . $val["pid"] . ';;;' . $val["id"];
  187. }
  188. }
  189. }
  190. $data = Tool::descarte($arr);
  191. $table_head = [];
  192. foreach($data as &$item){
  193. foreach($item as $val){
  194. $param = explode(";;;",$val);
  195. if(!in_array($param[2],$table_head)){
  196. $table_head[] = $param[2];
  197. }
  198. }
  199. }
  200. $headArray = [];
  201. foreach ($table_head as $val) {
  202. $headArray[] = $val;
  203. }
  204. $head = AttributeModel::where('id','in',$headArray)->select()->toArray();
  205. $goods = GoodsItemModel::where(["goods_id" => $goods_id])->select()->toArray();
  206. $goods_temp = [];
  207. foreach ($goods as $val) {
  208. $goods_temp[$val["spec_key"]] = $val;
  209. }
  210. return [ "goods"=>$goods_temp, "head"=>$head, "data"=>$data ];
  211. }
  212. }