QcodeProduct.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeClassification;
  4. use app\admin\model\QcodeCompany;
  5. use app\common\controller\Backend;
  6. use \think\Session;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class QcodeProduct extends Backend
  13. {
  14. /**
  15. * Product模型对象
  16. * @var \app\admin\model\QcodeProduct
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\QcodeProduct();
  23. $qcodeClassification = new QcodeClassification();
  24. $codeList = $qcodeClassification->where(['status'=>1,'delete_time'=>''])->select();
  25. $this->view->assign("codeList", $codeList);
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = false;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. $where = [
  47. 'delete_time'=> ''
  48. ];
  49. $req = input();
  50. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  51. $order = $req['order'];
  52. $offset = $req['offset'];
  53. $limit = $req['limit'];
  54. // 构造模糊查询条件
  55. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  56. // $filter = ['field' => $regex];
  57. $filter = json_decode($req['filter'], true);
  58. foreach ($filter as $k => $v){
  59. $where[$k] = new \MongoDB\BSON\Regex($v);
  60. }
  61. $list = $this->model->where($where)
  62. ->order($sort,$order)
  63. ->limit($limit)
  64. ->skip($offset)
  65. ->select();
  66. foreach ($list as $k=>$v) {
  67. $oid = $v['_id']->jsonSerialize();
  68. $list[$k]['id'] = $oid['$oid'];
  69. }
  70. $result = array("total" => count($list), "rows" => $list);
  71. return json($result);
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 产品总列表
  77. */
  78. public function products()
  79. {
  80. //当前是否为关联查询
  81. $this->relationSearch = false;
  82. //设置过滤方法
  83. $this->request->filter(['strip_tags', 'trim']);
  84. if ($this->request->isAjax()) {
  85. //如果发送的来源是Selectpage,则转发到Selectpage
  86. if ($this->request->request('keyField')) {
  87. return $this->selectpage();
  88. }
  89. $where = [
  90. 'delete_time'=> ''
  91. ];
  92. $req = input();
  93. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  94. $order = $req['order'];
  95. $offset = $req['offset'];
  96. $limit = $req['limit'];
  97. // 构造模糊查询条件
  98. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  99. // $filter = ['field' => $regex];
  100. $filter = json_decode($req['filter'], true);
  101. foreach ($filter as $k => $v){
  102. $where[$k] = new \MongoDB\BSON\Regex($v);
  103. }
  104. $total = $this->model->where($where)->count();
  105. $list = $this->model->where($where)
  106. // ->order($sort,$order)
  107. ->order('oid_id','desc')
  108. ->limit($limit)
  109. ->skip($offset)
  110. ->select();
  111. foreach ($list as $k=>$v) {
  112. $oid = $v['_id']->jsonSerialize();
  113. $list[$k]['id'] = $oid['$oid'];
  114. }
  115. $result = array("total" => $total, "rows" => $list);
  116. return json($result);
  117. }
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 厂商产品列表
  122. */
  123. public function product()
  124. {
  125. //当前是否为关联查询
  126. $this->relationSearch = false;
  127. //设置过滤方法
  128. $this->request->filter(['strip_tags', 'trim']);
  129. if ($this->request->isAjax()) {
  130. //如果发送的来源是Selectpage,则转发到Selectpage
  131. if ($this->request->request('keyField')) {
  132. return $this->selectpage();
  133. }
  134. $userInfo = Session::get('admin');
  135. $company_id = (int)$userInfo['company'];
  136. $where = [
  137. // 'company_id'=>(int)$userInfo['company'],
  138. 'delete_time'=> ''
  139. ];
  140. $req = input();
  141. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  142. $order = $req['order'];
  143. $offset = $req['offset'];
  144. $limit = $req['limit'];
  145. // 构造模糊查询条件
  146. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  147. // $filter = ['field' => $regex];
  148. $filter = json_decode($req['filter'], true);
  149. foreach ($filter as $k => $v){
  150. $where[$k] = new \MongoDB\BSON\Regex($v);
  151. }
  152. $db = new QcodeCompany();
  153. $rows = $db->name($company_id.'_'."qcode_company")
  154. ->where('delete_time','')
  155. ->column('product_id');
  156. $rows = array_values($rows);
  157. $total = $this->model->where($where)->where('_id','in',$rows)->count();
  158. $list = $this->model->where($where)
  159. ->where('_id','in',$rows)
  160. ->order($sort,$order)
  161. ->limit($limit)
  162. ->skip($offset)
  163. ->select();
  164. foreach ($list as $k=>$v) {
  165. $oid = $v['_id']->jsonSerialize();
  166. $list[$k]['id'] = $oid['$oid'];
  167. }
  168. $result = array("total" => $total, "rows" => $list);
  169. return json($result);
  170. }
  171. return $this->view->fetch();
  172. }
  173. /**
  174. * 新增产品
  175. */
  176. public function add()
  177. {
  178. if ($this->request->isAjax()){
  179. $req = $this->request->post();
  180. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  181. $this->error('请填写编号');
  182. }else{
  183. $data = [
  184. 'product_code'=>$req['row']['product_code'],
  185. 'delete_time'=>'',
  186. ];
  187. if ($this->model->where($data)->find()){
  188. $this->error('编号已存在');
  189. }
  190. }
  191. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  192. $this->error('请填写名称');
  193. }else{
  194. $data = [
  195. 'product_name'=>$req['row']['product_name'],
  196. 'delete_time'=>'',
  197. ];
  198. if ($this->model->where($data)->find()){
  199. $this->error('名称已存在');
  200. }
  201. }
  202. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  203. $this->error('请填写辅料代号');
  204. }
  205. if(!isset($req['row']['main_unit'])){
  206. $this->error('请填写主计量单位');
  207. }
  208. if(!isset($req['row']['sec_unit'])){
  209. $this->error('请填写辅计量单位');
  210. }
  211. if(!isset($req['row']['proportion'])){
  212. $this->error('请填写换算关系');
  213. }
  214. $data = [
  215. 'product_code' =>$req['row']['product_code'],
  216. 'product_name' =>$req['row']['product_name'],
  217. 'temple' =>$req['row']['temple'],
  218. 'main_unit' =>$req['row']['main_unit'],
  219. 'sec_unit' =>$req['row']['sec_unit'],
  220. 'proportion' =>$req['row']['proportion'],
  221. 'code' =>substr($req['row']['product_code'],0,4),
  222. 'create_time' =>time(),
  223. ];
  224. //插入数据
  225. $re = $this->model->save($data);
  226. if($re){
  227. $this->success('成功');
  228. }else{
  229. $this->error('失败');
  230. }
  231. }
  232. return $this->view->fetch();
  233. }
  234. /**
  235. * 编辑产品
  236. */
  237. public function edit($ids = NULL)
  238. {
  239. if ($this->request->isAjax()){
  240. $req = $this->request->post();
  241. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  242. $this->error('请填写编号');
  243. }else{
  244. $data = [
  245. 'product_code'=>$req['row']['product_code'],
  246. 'delete_time'=>'',
  247. ];
  248. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  249. $this->error('编号已存在');
  250. }
  251. }
  252. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  253. $this->error('请填写名称');
  254. }else{
  255. $data = [
  256. 'product_name'=>$req['row']['product_name'],
  257. 'delete_time'=>'',
  258. ];
  259. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  260. $this->error('名称已存在');
  261. }
  262. }
  263. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  264. $this->error('请填写辅料代号');
  265. }
  266. if(!isset($req['row']['main_unit'])){
  267. $this->error('请填写主计量单位');
  268. }
  269. if(!isset($req['row']['sec_unit'])){
  270. $this->error('请填写辅计量单位');
  271. }
  272. if(!isset($req['row']['proportion'])){
  273. $this->error('请填写换算关系');
  274. }
  275. $data = [
  276. 'product_code' =>$req['row']['product_code'],
  277. 'product_name' =>$req['row']['product_name'],
  278. 'temple' =>$req['row']['temple'],
  279. 'main_unit' =>$req['row']['main_unit'],
  280. 'sec_unit' =>$req['row']['sec_unit'],
  281. 'proportion' =>$req['row']['proportion'],
  282. 'update_time' =>time(),
  283. ];
  284. //修改数据
  285. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  286. $re = $department->save($data);
  287. if($re){
  288. $this->success('修改成功');
  289. }else{
  290. $this->error('修改失败');
  291. }
  292. }
  293. $row = $this->model->where('_id',$ids)->find();
  294. $this->view->assign('row',$row);
  295. return $this->view->fetch();
  296. }
  297. /**
  298. * 删除产品
  299. */
  300. public function del($ids = NULL)
  301. {
  302. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  303. $department->delete_time = time();
  304. $re = $department->save();
  305. if($re){
  306. $this->success('删除成功');
  307. }else{
  308. $this->error('删除失败');
  309. }
  310. }
  311. /**
  312. * 配置产品
  313. */
  314. public function bind($ids = NULL)
  315. {
  316. $userInfo = Session::get('admin');
  317. $company_id = (int)$userInfo['company'];
  318. $db = new QcodeCompany();
  319. //查询是否已经添加
  320. $row = $db->name($company_id.'_'."qcode_company")
  321. ->where(['delete_time'=>'','product_id'=>$ids])
  322. ->find();
  323. if($row) $this->success('已添加');
  324. $data = [
  325. 'product_id'=>$ids,
  326. 'create_time'=>time(),
  327. ];
  328. //插入qcode_company数据
  329. $bool = $db->save($data);
  330. if($bool){
  331. $this->success('添加成功');
  332. }else{
  333. $this->error('添加失败');
  334. }
  335. }
  336. /**
  337. * 取消配置产品
  338. */
  339. public function unbind($ids = NULL)
  340. {
  341. $userInfo = Session::get('admin');
  342. $company_id = (int)$userInfo['company'];
  343. $db = new QcodeCompany();
  344. //查询是否已经添加
  345. $bool = $db->name($company_id.'_'."qcode_company")
  346. ->where(['delete_time'=>'','product_id'=>$ids])
  347. ->update(['delete_time'=>time()]);
  348. if($bool){
  349. $this->success('删除成功');
  350. }else{
  351. $this->error('删除失败');
  352. }
  353. }
  354. }