Product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace app\admin\controller\qingdong\product;
  3. use addons\qingdong\model\Delivery;
  4. use addons\qingdong\model\DeliveryProduct;
  5. use addons\qingdong\model\Goods;
  6. use addons\qingdong\model\InstockProduct;
  7. use addons\qingdong\model\ProductPart;
  8. use addons\qingdong\model\Producttype;
  9. use addons\qingdong\model\ProductWarehouse;
  10. use addons\qingdong\model\Staff;
  11. use app\admin\controller\qingdong\Base;
  12. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  13. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  14. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  16. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  17. use think\Db;
  18. use think\Exception;
  19. /**
  20. * 产品列表
  21. * 操作文档:https://doc.fastadmin.net/qingdong
  22. * 软件介绍:https://www.fastadmin.net/store/qingdong.html
  23. * 售后微信:qingdong_crm
  24. */
  25. class Product extends Base
  26. {
  27. protected $relationSearch = true;
  28. protected $searchFields = 'id,name';
  29. /**
  30. * @var \addons\qingdong\model\Product
  31. */
  32. protected $model = null;
  33. public function _initialize()
  34. {
  35. parent::_initialize();
  36. $this->model = new \addons\qingdong\model\Product;
  37. }
  38. /**
  39. * 产品列表
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. //负责人为空
  48. $list = $this->model->where($where)->order($sort, $order)->with(['producttype','goods'])->paginate($limit);
  49. $row=$list->items();
  50. foreach ($row as $k => $v) {
  51. $v['type_id'] = $v['producttype']['name']??'';
  52. $row[$k] = $v;
  53. }
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加产品
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isPost()) {
  65. $params = $this->request->post("row/a");
  66. if ($params) {
  67. $params = $this->preExcludeFields($params);
  68. $result = false;
  69. Db::startTrans();
  70. try {
  71. $result = $this->model->allowField(true)->save($params);
  72. Db::commit();
  73. } catch (Exception $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. if ($result !== false) {
  78. $this->success();
  79. } else {
  80. $this->error(__('No rows were inserted'));
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 修改产品
  89. */
  90. public function edit($ids = null)
  91. {
  92. $map['id'] = $ids;
  93. if ($this->request->isAjax()) {
  94. $params = $this->request->post('row/a');
  95. $params = $this->preExcludeFields($params);
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. $result = $this->model->allowField(true)->update($params, $map);
  100. Db::commit();
  101. } catch (Exception $e) {
  102. Db::rollback();
  103. $this->error($e->getMessage());
  104. }
  105. if ($result !== false) {
  106. $this->success('修改成功');
  107. } else {
  108. $this->error('修改失败');
  109. }
  110. }
  111. $data = $this->model->where($map)->find();
  112. $this->view->assign("row", $data);
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 产品详情
  117. */
  118. public function detail($ids = null)
  119. {
  120. $row = $this->model->get($ids);
  121. $this->assign('row', $row);
  122. $this->assign('ids', $ids);
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 删除产品
  127. */
  128. public function del($ids = "")
  129. {
  130. if (!$this->request->isPost()) {
  131. $this->error(__("Invalid parameters"));
  132. }
  133. $ids = $ids ? $ids : $this->request->post("ids");
  134. $row = $this->model->get($ids);
  135. $this->modelValidate = true;
  136. if (!$row) {
  137. $this->error(__('No Results were found'));
  138. }
  139. $row->delete();
  140. $this->success();
  141. }
  142. //导入
  143. public function import()
  144. {
  145. set_time_limit(0);
  146. if ($this->request->isPost()) {
  147. $file = $this->request->request('file');
  148. $staff_id = $this->request->request('staff_id', 0);
  149. if (!$file) {
  150. $this->error(__('Parameter %s can not be empty', 'file'));
  151. }
  152. $filePath = ROOT_PATH . 'public' . $file;
  153. if (!is_file($filePath)) {
  154. $this->error(__('No results were found'));
  155. }
  156. //实例化reader
  157. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  158. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  159. $this->error(__('Unknown data format'));
  160. }
  161. if ($ext === 'csv') {
  162. $file = fopen($filePath, 'r');
  163. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  164. $fp = fopen($filePath, "w");
  165. $n = 0;
  166. while ($line = fgets($file)) {
  167. $line = rtrim($line, "\n\r\0");
  168. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  169. if ($encoding != 'utf-8') {
  170. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  171. }
  172. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  173. fwrite($fp, $line . "\n");
  174. } else {
  175. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  176. }
  177. $n++;
  178. }
  179. fclose($file) || fclose($fp);
  180. $reader = new Csv();
  181. } elseif ($ext === 'xls') {
  182. $reader = new Xls();
  183. } else {
  184. $reader = new Xlsx();
  185. }
  186. if (!$PHPExcel = $reader->load($filePath)) {
  187. $this->error(__('Unknown data format'));
  188. }
  189. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  190. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  191. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  192. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  193. //开始读取数据
  194. $fields = [];
  195. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  196. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  197. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  198. $fields[$currentRow][] = $val;
  199. if($val instanceof RichText) {//富文本转换字符串
  200. $val = $val->__toString();
  201. }
  202. $values[] = is_null($val) ? '' : trim($val);
  203. }
  204. }
  205. if (!isset($fields[1])) {
  206. $this->error('导入文件第一行没有数据');
  207. }
  208. $types=Producttype::where([])->column('id','name');
  209. $goods=Goods::where([])->column('id','name');
  210. $addProduct=[];
  211. $errorInfo=[];
  212. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  213. $values = [];
  214. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  215. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  216. if($val instanceof RichText) {//富文本转换字符串
  217. $val = $val->__toString();
  218. }
  219. $values[] = is_null($val) ? '' : trim($val);
  220. }
  221. $params = [
  222. 'name' => $values[0],
  223. 'type_id' => $types[$values[1]] ?? 0,
  224. 'num' => $values[2],
  225. 'goods_id' => $goods[$values[3]] ?? 0,
  226. 'price' => $values[4],
  227. 'cost_price' => $values[5],
  228. 'unit' => $values[6],
  229. 'stock' => $values[7],
  230. 'instock' => $values[8],
  231. 'outstock' => $values[9],
  232. 'status' => '上架'
  233. ];
  234. $addProduct[] = $params;
  235. }
  236. if (!empty($errorInfo)) {
  237. $this->error(implode(',', $errorInfo));
  238. }
  239. Db::startTrans();
  240. try {
  241. $this->model->allowField(true)->saveAll($addProduct);
  242. Db::commit();
  243. } catch (Exception $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. }
  247. $this->success('导入成功');
  248. }
  249. $this->assign('staffs', Staff::getList());
  250. return $this->view->fetch();
  251. }
  252. /**
  253. * 获取产品分类
  254. */
  255. public function get_type(){
  256. $type = input('type');
  257. if ($type == 'search') {//搜索项
  258. $types = Producttype::where([])->field('id,name')->select();
  259. return json($types);
  260. }
  261. $where = [];
  262. if ($keyValue = $this->request->request("keyValue")) {
  263. $where['id'] = $keyValue;
  264. }
  265. $name = input('name');
  266. if(!empty($name)){
  267. $where['name'] = ['like','%'.$name.'%'];
  268. }
  269. $countrys = Producttype::where($where)->field('id,name')->order('id desc')->select();
  270. return json(['list' => $countrys, 'total' => count($countrys)]);
  271. }
  272. /**
  273. * 配件列表
  274. */
  275. public function part_list()
  276. {
  277. //设置过滤方法
  278. $this->request->filter(['strip_tags', 'trim']);
  279. if ($this->request->isAjax()) {
  280. $product_id = input('ids');
  281. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  282. $model =new ProductPart();
  283. //负责人为空
  284. $list = $model->where(['product_id' => $product_id])->order('id desc')->paginate($limit);
  285. $result = array("total" => $list->total(), "rows" => $list->items());
  286. return json($result);
  287. }
  288. return $this->view->fetch();
  289. }
  290. /**
  291. * 添加配件
  292. */
  293. public function add_part()
  294. {
  295. if ($this->request->isPost()) {
  296. $params = $this->request->post("row/a");
  297. $product_id=input('product_id',0);
  298. if(empty($product_id)){
  299. $this->error('参数错误');
  300. }
  301. $model = new ProductPart();
  302. if ($params) {
  303. $params = $this->preExcludeFields($params);
  304. $result = false;
  305. Db::startTrans();
  306. try {
  307. $params['product_id'] = $product_id;
  308. $result = $model->allowField(true)->save($params);
  309. Db::commit();
  310. } catch (Exception $e) {
  311. Db::rollback();
  312. $this->error($e->getMessage());
  313. }
  314. if ($result !== false) {
  315. $this->success();
  316. } else {
  317. $this->error(__('No rows were inserted'));
  318. }
  319. }
  320. $this->error(__('Parameter %s can not be empty', ''));
  321. }
  322. return $this->view->fetch();
  323. }
  324. /**
  325. * 修改配件
  326. */
  327. public function edit_part($ids = null)
  328. {
  329. $map['id'] = $ids;
  330. $model = new ProductPart();
  331. if ($this->request->isAjax()) {
  332. $params = $this->request->post('row/a');
  333. $params = $this->preExcludeFields($params);
  334. $result = false;
  335. Db::startTrans();
  336. try {
  337. $result = $model->allowField(true)->update($params, $map);
  338. Db::commit();
  339. } catch (Exception $e) {
  340. Db::rollback();
  341. $this->error($e->getMessage());
  342. }
  343. if ($result !== false) {
  344. $this->success('修改成功');
  345. } else {
  346. $this->error('修改失败');
  347. }
  348. }
  349. $data = $model->where($map)->find();
  350. $this->view->assign("row", $data);
  351. return $this->view->fetch();
  352. }
  353. /**
  354. * 删除配件
  355. */
  356. public function del_part($ids = "")
  357. {
  358. if (!$this->request->isPost()) {
  359. $this->error(__("Invalid parameters"));
  360. }
  361. $ids = $ids ? $ids : $this->request->post("ids");
  362. $model = new ProductPart();
  363. $row = $model->get($ids);
  364. $this->modelValidate = true;
  365. if (!$row) {
  366. $this->error(__('No Results were found'));
  367. }
  368. $row->delete();
  369. $this->success();
  370. }
  371. }