Shops.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 getCount(ShopDelivery $shopDelivery)
  14. {
  15. $result = $shopDelivery->field('sum(num) total_num,sum(weigh) total_weight')->where('user_id', $this->userinfo['id'])->find();
  16. $this->success('ok', $result);
  17. }
  18. //发货记录
  19. public function delivery(ShopDelivery $shopDelivery){
  20. $plat_id = $this->request->post('plat_id/d', 0); //平台
  21. $shop_id = $this->request->post('shop_id/d', 0); //店铺
  22. $variety_id = $this->request->post('variety_id/d', 0); //品种
  23. $spec_id = $this->request->post('spec_id/d', 0); //规格
  24. $customer = $this->request->post('customer_name/s', ''); //客户
  25. $num = $this->request->post('num/d', 0); //数量
  26. $limit = $this->request->post('limit/d', 15); //条数
  27. $where['a.user_id'] = $this->userinfo['id'];
  28. if($plat_id > 0) $where['a.plat_id'] = $plat_id;
  29. if($shop_id > 0) $where['a.shop_id'] = $shop_id;
  30. if($variety_id > 0) $where['a.variety_id']= $variety_id;
  31. if($spec_id > 0) $where['a.spec_id'] = $spec_id;
  32. if(!empty($customer)) $where['customer_name'] = ['like', '%'.$customer.'%'];
  33. if($num > 0) $where['a.num']= $num;
  34. $result = $shopDelivery::alias('a')
  35. ->leftjoin('shop_list b', 'a.shop_id = b.id') //店铺
  36. ->leftjoin('stock_config c', 'a.variety_id = c.id') //品种
  37. ->leftjoin('product_config d', 'a.spec_id = d.id') //规格
  38. ->leftjoin('customer u', 'a.customer_id = u.id') //客户
  39. ->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,u.name customer_name')
  40. ->where($where)
  41. ->paginate($limit);
  42. $this->success('ok', $result);
  43. }
  44. //添加记录
  45. public function create(ShopDelivery $shopDelivery, SpecService $specService)
  46. {
  47. $data = $this->request->post();
  48. $result = false;
  49. Db::startTrans();
  50. try {
  51. validate(ShopValidate::class)->scene('add')->check($data);
  52. //发货数据
  53. $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
  54. //扣除库存
  55. $result = $shopDelivery->saveAll($resData);
  56. Db::commit();
  57. }catch (ValidateException $e) {
  58. Db::rollback();
  59. return $this->error($e->getError());
  60. } catch (\Exception $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. }
  64. if ($result === false) {
  65. $this->error(__('没有新增任何数据'));
  66. }
  67. $this->success();
  68. }
  69. //编辑录入
  70. public function deliveryedit(SpecService $specService)
  71. {
  72. $data = $this->request->post();
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. validate(ShopValidate::class)->scene('edit')->check($data);
  77. $result = $specService::getDeliveryEdit($this->userinfo['id'], $data);
  78. Db::commit();
  79. }catch (ValidateException $e) {
  80. return $this->error($e->getError());
  81. } catch (\Exception $e) {
  82. Db::rollback();
  83. $this->error($e->getMessage());
  84. }
  85. if ($result === false) {
  86. $this->error(__('没有新增任何数据'));
  87. }
  88. $this->success();
  89. }
  90. //删除录入
  91. public function deliverydel(ShopDelivery $shopDelivery, SpecService $specService)
  92. {
  93. $data = $this->request->post();
  94. $result = false;
  95. Db::startTrans();
  96. try {
  97. validate(ShopValidate::class)->scene('del')->check($data);
  98. $result = $specService::getDeliveryDel($this->userinfo['id'], $data);
  99. Db::commit();
  100. }catch (ValidateException $e) {
  101. return $this->error($e->getError());
  102. } catch (\Exception $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. }
  106. if ($result === false) {
  107. $this->error(__('没有新增任何数据'));
  108. }
  109. $this->success();
  110. }
  111. }