Distribution.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Distribution as DistributionModel;
  12. use app\common\models\Area as AreaModel;
  13. class Distribution extends Service {
  14. /**
  15. * 获取列表数据
  16. * @param $data
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getList($data){
  23. return [
  24. "count"=>DistributionModel::count(),
  25. "data"=>DistributionModel::page($data["page"]??1,$data["limit"]??10)->order("id","desc")->select()->toArray()
  26. ];
  27. }
  28. /**
  29. * 详情
  30. * @param $id
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public static function detail($id){
  37. $row = DistributionModel::where("id",$id)->find();
  38. if(!empty($row['area_group'])){
  39. $row["area_group"] = json_decode($row['area_group'],true);
  40. }
  41. if(!empty($row['first_price_group'])){
  42. $row["first_price_group"] = json_decode($row['first_price_group'],true);
  43. }
  44. if(!empty($row['second_price_group'])){
  45. $row["second_price_group"] = json_decode($row['second_price_group'],true);
  46. }
  47. $temp = [];
  48. if(!empty($row["area_group"])){
  49. foreach ($row["area_group"] as $key => $item) {
  50. $area_id = explode(",", $item);
  51. $arr = array();
  52. foreach ($area_id as $val) {
  53. $arr[] = AreaModel::where(['id' => $val])->value("name");
  54. }
  55. $temp[$key]["id"] = $item;
  56. $temp[$key]["title"] = implode(",", $arr);
  57. $temp[$key]["first"] = $row["first_price_group"][$key];
  58. $temp[$key]["second"] = $row["second_price_group"][$key];
  59. }
  60. }
  61. $row["attr"] = $temp;
  62. return [
  63. "data"=>$row,
  64. "weight"=>[ "500"=>"500克", "1000"=>"1公斤", "1500"=>"1.5公斤", "2000"=>"2公斤", "5000"=>"5公斤", "10000"=>"10公斤", "20000"=>"20公斤", "50000"=>"50公斤" ]
  65. ];
  66. }
  67. /**
  68. * 保存数据
  69. * @param $data
  70. * @return DistributionModel|bool|\think\Model
  71. */
  72. public static function save($data){
  73. if(!empty($data['area_group'])){
  74. $data["area_group"] = json_encode($data['area_group'],JSON_UNESCAPED_UNICODE);
  75. }
  76. if(!empty($data['first_price_group'])){
  77. $data["first_price_group"] = json_encode($data['first_price_group'],JSON_UNESCAPED_UNICODE);
  78. }
  79. if(!empty($data['second_price_group'])){
  80. $data["second_price_group"] = json_encode($data['second_price_group'],JSON_UNESCAPED_UNICODE);
  81. }
  82. if(DistributionModel::where("id",$data["id"])->count()){
  83. return DistributionModel::where("id",$data["id"])->save($data);
  84. }else{
  85. return DistributionModel::create($data);
  86. }
  87. }
  88. /**
  89. * 删除
  90. * @param $id
  91. * @return bool
  92. */
  93. public static function delete($id){
  94. return DistributionModel::where("id",$id)->delete();
  95. }
  96. /**
  97. * 更新字段值
  98. * @return DistributionModel
  99. */
  100. public static function setFields(){
  101. $data = self::getFields();
  102. return DistributionModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  103. }
  104. }