Shops.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\validate\Shop as ShopValidate;
  4. use app\common\model\ShopDelivery;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use app\api\service\SpecService;
  8. use think\exception\ValidateException;
  9. class Shops extends Base
  10. {
  11. //protected $noNeedLogin = ['*'];
  12. //发货记录
  13. public function delivery(ShopDelivery $shopDelivery){
  14. $plat_id = $this->request->post('plat_id/d', 0); //平台
  15. $shop_id = $this->request->post('shop_id/d', 0); //店铺
  16. $variety_id = $this->request->post('variety_id/d', 0); //品种
  17. $limit = $this->request->post('limit/d', 15); //条数
  18. $where['a.user_id'] = $this->userinfo['id'];
  19. if($plat_id > 0) $where['a.plat_id'] = $plat_id;
  20. if($shop_id > 0) $where['a.shop_id'] = $shop_id;
  21. if($variety_id > 0) $where['a.variety_id']= $variety_id;
  22. $result = $shopDelivery::alias('a')
  23. ->leftjoin('shop_list b', 'a.shop_id = b.id') //店铺
  24. ->leftjoin('stock_config c', 'a.variety_id = c.id') //品种
  25. ->leftjoin('product_config d', 'a.spec_id = d.id') //规格
  26. ->field('a.id,a.plat_id,a.shop_id,a.createtime,a.num, b.name shop_name,c.title variety_name,d.title spec_name')
  27. ->where($where)
  28. ->paginate($limit);
  29. $this->success('ok', $result);
  30. }
  31. //添加记录
  32. public function create(ShopDelivery $shopDelivery, SpecService $specService)
  33. {
  34. $data = $this->request->post();
  35. $result = false;
  36. Db::startTrans();
  37. try {
  38. validate(ShopValidate::class)->scene('add')->check($data);
  39. //发货数据
  40. $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
  41. //扣除库存
  42. $result = $shopDelivery->saveAll($resData);
  43. Db::commit();
  44. }catch (ValidateException $e) {
  45. Db::rollback();
  46. return $this->error($e->getError());
  47. } catch (\Exception $e) {
  48. Db::rollback();
  49. $this->error($e->getMessage());
  50. }
  51. if ($result === false) {
  52. $this->error(__('没有新增任何数据'));
  53. }
  54. $this->success();
  55. }
  56. }