Shops.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.box_id,a.fast_mail,a.ship_date,a.settlement_data,a.createtime,a.num, b.name shop_name,c.title variety_name,d.title spec_name,u.name customer_name')
  41. ->where($where)
  42. ->order('a.ship_date desc')
  43. ->paginate($limit);
  44. $this->success('ok', $result);
  45. }
  46. //添加记录
  47. public function create(ShopDelivery $shopDelivery, SpecService $specService, StockLog $stockLog)
  48. {
  49. $data = $this->request->post();
  50. $result = false;
  51. Db::startTrans();
  52. try {
  53. validate(ShopValidate::class)->scene('add')->check($data);
  54. //发货数据
  55. list($deliveryData, $stockData) = $specService::getDeliveryList($this->userinfo['id'], $data);
  56. //扣除库存
  57. $shopDelivery->saveAll($deliveryData);
  58. //记录
  59. $result = $stockLog->saveAll($stockData);
  60. Db::commit();
  61. }catch (ValidateException $e) {
  62. Db::rollback();
  63. return $this->error($e->getError());
  64. } catch (\Exception $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. }
  68. if ($result === false) {
  69. $this->error(__('没有新增任何数据'));
  70. }
  71. $this->success();
  72. }
  73. //编辑录入
  74. public function deliveryedit(SpecService $specService)
  75. {
  76. $data = $this->request->post();
  77. $result = false;
  78. Db::startTrans();
  79. try {
  80. validate(ShopValidate::class)->scene('edit')->check($data);
  81. $result = $specService::getDeliveryEdit($this->userinfo['id'], $data);
  82. Db::commit();
  83. }catch (ValidateException $e) {
  84. return $this->error($e->getError());
  85. } catch (\Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($result === false) {
  90. $this->error(__('没有新增任何数据'));
  91. }
  92. $this->success();
  93. }
  94. //删除录入
  95. public function deliverydel(ShopDelivery $shopDelivery, SpecService $specService)
  96. {
  97. $data = $this->request->post();
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. validate(ShopValidate::class)->scene('del')->check($data);
  102. $result = $specService::getDeliveryDel($this->userinfo['id'], $data);
  103. Db::commit();
  104. }catch (ValidateException $e) {
  105. return $this->error($e->getError());
  106. } catch (\Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if ($result === false) {
  111. $this->error(__('没有新增任何数据'));
  112. }
  113. $this->success();
  114. }
  115. }