GoodLogic.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\CommonModel;
  4. use app\model\CompanyGoodModel;
  5. use app\model\GoodModel;
  6. use app\model\InventoryExchangeModel;
  7. use app\model\InventoryShoppingModel;
  8. use think\response\Json;
  9. class GoodLogic extends BaseLogic
  10. {
  11. //商品列表
  12. public static function goodList(array $data = []): Json
  13. {
  14. $db = CompanyGoodModel::alias('a')
  15. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  16. ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => self::$group_id]);
  17. $count = $db->count('a.id');
  18. $list = $db
  19. ->field('a.id,a.code,a.good_id,b.good_cover_img,b.good_name,b.moq,b.step,b.price,b.type,0 inventory')
  20. ->page($data['page'], $data['size'])
  21. ->order(['a.is_top' => 'desc', 'a.weight' => 'desc', 'a.id' => 'desc'])
  22. ->withAttr('inventory', function ($val, $da) {
  23. if ($da['type'] == GoodModel::$type_exchange) return InventoryExchangeModel::where(['account_id' => self::$aid, 'good_id' => $da['good_id']])->value('inventory');
  24. else return InventoryShoppingModel::where(['good_id' => $da['good_id']])->value('inventory');
  25. })
  26. ->select()
  27. ->toArray();
  28. return json_show(CommonModel::$success, '获取商品列表成功', ['count' => $count, 'list' => $list]);
  29. }
  30. //商品详情
  31. public static function goodInfo(string $code = ''): Json
  32. {
  33. $rs = CompanyGoodModel::alias('a')
  34. ->field('a.id,a.code,a.good_id,b.good_cover_img,b.good_code,b.good_name,b.moq,b.step,b.good_banner_img,b.good_img,b.good_param,b.status, 0 inventory,d.unit,b.price,b.type')
  35. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  36. // ->leftJoin('inventory_shopping c', 'c.good_id=a.good_id')
  37. ->leftJoin('unit d', 'd.id=b.unit_id AND d.is_del=' . CommonModel::$del_normal)
  38. ->where(['a.is_del' => CommonModel::$del_normal, 'a.code' => $code])
  39. ->withAttr('good_param', function ($val) {
  40. return json_decode($val, true);
  41. })
  42. ->withAttr('good_img', function ($val) {
  43. return explode(',', $val);
  44. })->withAttr('good_banner_img', function ($val) {
  45. return explode(',', $val);
  46. })->withAttr('inventory', function ($val, $da) {
  47. if ($da['type'] == GoodModel::$type_exchange) return InventoryExchangeModel::where(['account_id' => self::$aid, 'good_id' => $da['good_id']])->value('inventory');
  48. else return InventoryShoppingModel::where(['good_id' => $da['good_id']])->value('inventory');
  49. })
  50. ->findOrEmpty()
  51. ->toArray();
  52. return json_show(CommonModel::$success, '获取商品详情成功', $rs);
  53. }
  54. }