Shops.php 4.7 KB

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