Product.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\CompanyModel;
  5. use app\common\model\ProductCategoryModel;
  6. use app\common\model\ProductModel;
  7. use think\App;
  8. use think\facade\View;
  9. use think\Request;
  10. class Product extends BaseController
  11. {
  12. private $productModel;
  13. private $categoryModel;
  14. private $companyModel;
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. $this->productModel = new ProductModel();
  19. $this->categoryModel = new ProductCategoryModel();
  20. $this->companyModel = new CompanyModel();
  21. }
  22. /**
  23. * @param Request $request
  24. * @return \think\response\View
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function index(Request $request) {
  28. $params = $request->param();
  29. $format_params = [
  30. 'bar_code' => format_string($params['bar_code'] ?? null),
  31. 'category_id' => format_string($params['category_id'] ?? null) != null ? (int)$params['category_id']: null,
  32. 'is_serve' => format_string($params['is_serve'] ?? null) != null ? (int)$params['is_serve']: null,
  33. ];
  34. View::assign([
  35. 'list' => $this->productModel->findByPaginate($format_params),
  36. 'params' => $format_params,
  37. 'all_category' => recursion($this->categoryModel->findAll(),0),
  38. ]);
  39. return view();
  40. }
  41. public function add(Request $request) {
  42. $params = $request->param();
  43. if($request->isAjax()) {
  44. $isExist = $this->productModel->doesItExist($params['bar_code']);
  45. if ($isExist)
  46. return $this->fail('Fail to add. Data duplication');
  47. $category = $this->categoryModel->findById($params['category_id']);
  48. if(!$category)
  49. return $this->fail(lang('Category does not exist'));
  50. $company = $this->companyModel->findById($params['company_id']);
  51. if(!$company)
  52. return $this->fail(lang('Company does not exist'));
  53. $res = $this->productModel->save([
  54. 'bar_code' => $params['bar_code'],
  55. 'name' => $params['name'] ?? null,
  56. 'image' => $params['image'] ?? null,
  57. 'category_id' => $params['category_id'] ?? null,
  58. 'company_id' => $company->id,
  59. 'is_serve' => $params['is_serve'],
  60. 'purchase_price'=> $params['purchase_price'],
  61. 'real_price' => $params['real_price'],
  62. 'lineate_price' => $params['lineate_price'],
  63. 'is_upload_numerology' => $params['is_upload_numerology'],
  64. 'is_gather_annuity' => $params["is_gather_annuity"],
  65. 'annuity' => $params["annuity"],
  66. 'sales_tax_rate' => $params['sales_tax_rate']
  67. ]);
  68. return $this->ok($res);
  69. }
  70. View::assign([
  71. 'all_category' => recursion($this->categoryModel->findAll(),0),
  72. "all_company" => $this->companyModel->findAll()
  73. ]);
  74. return view();
  75. }
  76. public function edit(Request $request) {
  77. $params = $request->param();
  78. if(!isset($params['id']))
  79. return $this->fail(lang('ID not exist'));
  80. $product = $this->productModel->findById($params['id']);
  81. if($request->isAjax()) {
  82. $category = $this->categoryModel->findById($params['category_id']);
  83. if(!$category)
  84. return $this->fail(lang('Category does not exist'));
  85. $company = $this->companyModel->findById($params['company_id'] ?? 0);
  86. if(!$company)
  87. return $this->fail(lang('Company does not exist'));
  88. $res = $this->productModel->where('id',$params['id'])->update([
  89. 'bar_code' => $params['bar_code'],
  90. 'name' => isset($params['name']) ? $params['name'] : $product->name,
  91. 'image' => isset($params['image']) ? $params['image'] : $product->image,
  92. 'category_id' => $category->id,
  93. 'company_id' => $company->id,
  94. 'is_serve' => (int)$params['is_serve'],
  95. 'purchase_price'=> isset($params['purchase_price']) ? $params['purchase_price'] : $product->purchase_price,
  96. 'real_price' => isset($params['real_price']) && $params['real_price'] > 0 ? $params['real_price'] : $product->real_price,
  97. 'lineate_price' => isset($params['lineate_price']) && $params['lineate_price'] > 0 ? $params['lineate_price'] : $product->lineate_price,
  98. 'is_upload_numerology' => $params['is_upload_numerology'],
  99. 'is_gather_annuity' => $params["is_gather_annuity"],
  100. 'annuity' => $params["annuity"],
  101. 'sales_tax_rate' => $params['sales_tax_rate'],
  102. 'update_time' => time()
  103. ]);
  104. return $this->ok($res);
  105. }
  106. View::assign([
  107. 'product' => $product,
  108. 'all_category' => recursion($this->categoryModel->findAll(),0),
  109. 'all_company' => $this->companyModel->findAll()
  110. ]);
  111. return view();
  112. }
  113. public function delete(Request $request) {
  114. $params = $request->param();
  115. if(!isset($params['ids']))
  116. return $this->fail(lang("Please select the data you want to delete"));
  117. $this->productModel->deleteByIds(explode(',',$params['ids']));
  118. return $this->ok(true);
  119. }
  120. /**
  121. * @return \think\response\Json
  122. */
  123. public function upload() {
  124. // 获取表单上传文件 例如上传了001.jpg
  125. $file = request()->file('file');
  126. // 上传到本地服务器
  127. $save_name = \think\facade\Filesystem::disk('public')->putFile( 'product', $file);
  128. return $this->ok(str_replace('\\',"/",'/storage/'.$save_name));
  129. }
  130. public function findProducts(Request $request) {
  131. $params = $request->param();
  132. if(!isset($params['text']))
  133. return $this->fail(lang('Data not exist'));
  134. $products = $this->productModel->findProducts($params['text']);
  135. return $this->ok($products);
  136. }
  137. }