Shops.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\validate\Shop as ShopValidate;
  4. use app\common\model\ShopList;
  5. use app\common\model\ShopDelivery;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use app\api\service\SpecService;
  9. use think\exception\ValidateException;
  10. class Shops extends Base
  11. {
  12. //protected $noNeedLogin = ['*'];
  13. /**
  14. * @return void 全部平台
  15. */
  16. public function getPlatform(){
  17. $list = site_config('addonsd.platform_list');
  18. $this->success('提交成功', $list);
  19. }
  20. //获取店铺
  21. public function getShop(ShopList $shopList){
  22. $platform_id = $this->request->post('platform_id/d');
  23. if(empty($platform_id)){
  24. $this->error('参数有误');
  25. }
  26. return $this->success('ok', $shopList->where('platform', $platform_id)->column('name', 'id'));
  27. }
  28. //获取规格
  29. public function getSpec(ShopList $shopList){
  30. $shop_id = $this->request->post('shop_id/d');
  31. if( empty($shop_id)){
  32. $this->error('参数有误');
  33. }
  34. $spec = $shopList->where('id', $shop_id)->value('type_spec');
  35. $shopList = $spec?json_decode($spec, true): [];
  36. return $this->success('ok', $shopList);
  37. }
  38. //发货记录
  39. public function delivery(ShopDelivery $shopDelivery){
  40. $plat_id = $this->request->post('plat_id/d', 0); //平台
  41. $shop_id = $this->request->post('shop_id/d', 0); //店铺
  42. $variety_id = $this->request->post('variety_id/d', 0); //品种
  43. $where['a.user_id'] = $this->userinfo['id'];
  44. if($plat_id > 0) $where['a.plat_id'] = $plat_id;
  45. if($shop_id > 0) $where['a.shop_id'] = $shop_id;
  46. if($variety_id > 0) $where['a.variety_id']= $variety_id;
  47. $result = $shopDelivery::alias('a')
  48. ->leftjoin('shop_list b', 'a.shop_id = b.id') //店铺
  49. ->leftjoin('stock_config c', 'a.variety_id = c.id') //品种
  50. ->leftjoin('product_config d', 'a.spec_id = d.id') //规格
  51. ->field('a.id,a.num, b.name shop_name,c.title variety_name,d.title spec_name')
  52. ->where($where)
  53. ->paginate(15);
  54. $this->success('ok', $result);
  55. }
  56. //添加记录
  57. public function create(ShopDelivery $shopDelivery, SpecService $specService)
  58. {
  59. $data = $this->request->post();
  60. $result = false;
  61. Db::startTrans();
  62. try {
  63. validate(ShopValidate::class)->scene('add')->check($data);
  64. //发货数据
  65. $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
  66. //扣除库存
  67. $result = $shopDelivery->saveAll($resData);
  68. Db::commit();
  69. }catch (ValidateException $e) {
  70. Db::rollback();
  71. return $this->error($e->getError());
  72. } catch (\Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($result === false) {
  77. $this->error(__('没有新增任何数据'));
  78. }
  79. $this->success();
  80. }
  81. }