Cat.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. class Cat extends BaseController
  8. {
  9. public $post="";
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $this->post=$this->request->post();
  14. }
  15. public function list(){
  16. $where=[["is_del","=",0]];
  17. $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
  18. $where[]=["pid","=",$pid];
  19. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? intval($this->post['cat_name']) :"";
  20. if($cat_name!==""){
  21. $where[]=['cat_name',"like","%$cat_name%"];
  22. }
  23. $data = Db::name("cat")->where($where)->select();
  24. $vmp = [];
  25. foreach ($data as $sts){
  26. $vmp[]=stro($sts);
  27. }
  28. return app_show(0,"获取成功",$vmp);
  29. }
  30. public function plist(){
  31. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  32. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  33. $where =[["is_del","=",0]];
  34. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? trim($this->post['cat_name']) :"";
  35. if($cat_name!==""){
  36. $where[]=['cat_name',"like","%$cat_name%"];
  37. }
  38. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  39. if($pid!==""){
  40. $where[]=['pid',"=",$pid];
  41. }
  42. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  43. if($status!==""){
  44. $where[]=['status',"=",$status];
  45. }
  46. $count = Db::name("cat")->where($where)->count();
  47. $total = ceil($count / $size);
  48. $page = $page >= $total ? $total : $page;
  49. $list = Db::name('cat')->where($where)->page($page, $size)->select();
  50. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  51. }
  52. public function title(){
  53. $where =[["is_del","=",0]];
  54. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? trim($this->post['cat_name']) :"";
  55. if($cat_name!==""){
  56. $where[]=['cat_name',"like","%$cat_name%"];
  57. }
  58. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  59. if($pid!==""){
  60. $where[]=["pid","=",$pid];
  61. }
  62. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  63. if($status!==""){
  64. $where[]=['status',"=",$status];
  65. }
  66. $list = Db::name('cat')->where($where)->select();
  67. return app_show(0, "获取成功",$list);
  68. }
  69. public function create()
  70. {
  71. $cat_name = isset($this->post['cat_name']) && $this->post['cat_name'] !== "" ? trim($this->post['cat_name']) : "";
  72. if ($cat_name == "") {
  73. return error_show(1002, "参数cat_name不能为空");
  74. }
  75. $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? intval($this->post['pid']) : "";
  76. if ($pid === "") {
  77. return error_show(1002, "参数pid不能为空");
  78. }
  79. $cat_desc = isset($this->post['cat_desc']) && $this->post['cat_desc'] !== "" ? trim($this->post['cat_desc']) : "";
  80. $fund_code = isset($this->post['fund_code']) && $this->post['fund_code'] !== "" ? trim($this->post['fund_code']) : "";
  81. $levl=Db::name('cat')->where(['id'=>$pid])->find();
  82. $level =1;
  83. if ($pid !== 0) {
  84. if(empty($levl)){
  85. return error_show(1002,"未找到父级数据");
  86. }
  87. $level =$levl['level']+1;
  88. }
  89. $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== "" ? $this->post['specs_id'] : [];
  90. if (empty($specs_id)) {
  91. return error_show(1002, "参数specs_id不能为空");
  92. }
  93. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  94. if($token==''){
  95. return error_show(1005,"参数token不能为空");
  96. }
  97. $user =GetUserInfo($token);
  98. if(empty($user)||$user['code']!=0){
  99. return error_show(1002,"创建人数据不存在");
  100. }
  101. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  102. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  103. $rate = isset($this->post['rate']) && $this->post['rate'] !== "" ? intval($this->post['rate']) : "";
  104. $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? intval($this->post['weight']) : "0";
  105. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "1";
  106. Db::startTrans();
  107. try{
  108. $data = [
  109. "cat_name" => $cat_name,
  110. "pid" => $pid,
  111. "level" => $level,
  112. "rate" => $rate,
  113. "weight" => $weight,
  114. "status" => $status,
  115. "cat_desc"=>$cat_desc,
  116. "fund_code"=>$fund_code,
  117. "is_del"=>0,
  118. "addtime" => date("Y-m-d H:i:s"),
  119. "updatetime" => date("Y-m-d H:i:s")
  120. ];
  121. $datainfo = Db::name('cat')->insert($data,true);
  122. $vat =[];
  123. if ($datainfo>0) {
  124. $stx = ["order_code"=>$pid,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
  125. ActionLog::logAdd($this->post['token'],$stx,"sxd",$status,$stx);
  126. $spc =[];
  127. $spc['cat_id']=$datainfo;
  128. $spc['exam_status']=0;
  129. $spc['status']=0;
  130. $spc['creater']=$creater;
  131. $spc['createrid']=$createrid;
  132. $spc['is_del']=0;
  133. $spc['addtime']=date("Y-m-d H:i:d");
  134. $spc['updatetime']=date("Y-m_d H:i:s");
  135. $vpn = Db::name('cat_spec')->insert($spc,true);
  136. if ($vpn == "") {
  137. Db::rollback();
  138. return error_show(1002, "新建失败");
  139. }
  140. foreach ($specs_id as $value){
  141. $item =[];
  142. $item['cat_id']=$datainfo;
  143. $item['specs_id']=$value;
  144. $item['is_del']=0;
  145. $item['addtime']=date("Y-m-d H:i:s");
  146. $vat[] = $item;
  147. }
  148. $vp = Db::name('cat_specs')->insertAll($vat);
  149. if ($vp==0) {
  150. Db::rollback();
  151. return error_show(1005, "新建失败");
  152. }
  153. Db::commit();
  154. return error_show(0, "新建成功");
  155. } else {
  156. Db::rollback();
  157. return error_show(1002, "新建失败");
  158. }
  159. }catch (\Exception $e){
  160. Db::rollback();
  161. return error_show(1005,$e->getMessage());
  162. }
  163. }
  164. public function edit(){
  165. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
  166. $idinfo = Db::name('cat')->where(['id'=>$id])->find();
  167. if($idinfo==""){
  168. return error_show(1002,"未找到商品数据");
  169. }
  170. $cat_name= isset($this->post['cat_name']) && $this->post['cat_name'] !=="" ? trim($this->post['cat_name']) :"";
  171. if($cat_name==""){
  172. return error_show(1002,"参数cat_name不能为空");
  173. }
  174. $pid = isset($this->post['pid']) && $this->post['pid'] !=="" ? intval($this->post['pid']) :"";
  175. if($pid==""){
  176. return error_show(1002,"参数pid不能为空");
  177. }
  178. $cat_desc = isset($this->post['cat_desc']) && $this->post['cat_desc'] !=="" ? trim($this->post['cat_desc']) :"";
  179. $specs_id = isset($this->post['specs_id']) && $this->post['specs_id'] !== "" ? $this->post['specs_id'] : [];
  180. if (empty($specs_id)) {
  181. return error_show(1002, "参数specs_id不能为空");
  182. }
  183. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  184. if($token==''){
  185. return error_show(1005,"参数token不能为空");
  186. }
  187. $user =GetUserInfo($token);
  188. if(empty($user)||$user['code']!=0){
  189. return error_show(1002,"创建人数据不存在");
  190. }
  191. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  192. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  193. $level = isset($this->post['level']) && $this->post['level'] !=="" ? intval($this->post['level']) :"";
  194. $weight = isset($this->post['weight']) && $this->post['weight']!==""? intval($this->post['weight']):"0";
  195. $status = isset($this->post['status']) && $this->post['status'] !==""?intval($this->post['status']) :"1";
  196. Db::startTrans();
  197. try{
  198. $str = [
  199. "id"=>$id,
  200. "cat_name"=>$cat_name,
  201. "pid"=>$pid,
  202. "level"=>$level,
  203. "weight"=>$weight,
  204. "status"=>$status,
  205. "cat_desc"=>$cat_desc,
  206. "is_del"=>0,
  207. "updatetime"=>date("Y-m-d H:i:s")
  208. ];
  209. $strinfo = Db::name('cat')->where(['id'=>$id])->save($str);
  210. if($strinfo){
  211. $order = ["order_code"=>$id,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  212. ActionLog::logAdd($this->post['token'],$order,"sxd",$status,$order);
  213. $spc =[];
  214. $vp = Db::name('cat_spec')->where(['cat_id'=>$idinfo['id'],'is_del'=>0])->find();
  215. $str ="";
  216. if($vp!=false){
  217. isset($vp['id']) && $vp['id'] !== "" ? $spc['id'] = $vp['id'] : '';
  218. $spc['cat_id']=$idinfo['id'];
  219. $spc['exam_status']=0;
  220. $spc['status']=0;
  221. $spc['is_del']=0;
  222. $spc['updatetime']=date("Y-m_d H:i:s");
  223. $str=$vp['id'];
  224. $vpn = Db::name('cat_spec')->save($spc);
  225. }else{
  226. $spc['cat_id']=$idinfo['id'];
  227. $spc['exam_status']=0;
  228. $spc['status']=0;
  229. $spc['creater']=$creater;
  230. $spc['createrid']=$createrid;
  231. $spc['is_del']=0;
  232. $spc['addtime']=date("Y-m-d H:i:d");
  233. $spc['updatetime']=date("Y-m_d H:i:s");
  234. $vpn = Db::name('cat_spec')->insert($spc,true);
  235. $str=$vpn;
  236. }
  237. if ($vpn == false) {
  238. Db::rollback();
  239. return error_show(1002, "更新失败");
  240. }
  241. $db = Db::name('cat_specs')->where(['cat_id'=>$idinfo['id'],'is_del'=>0])->column("specs_id");
  242. if(empty($db)){
  243. $db=[];
  244. }
  245. $sn = array_diff($specs_id,$db);
  246. $st = array_diff($db,$specs_id);
  247. if(!empty($st)){
  248. $int['is_del']=1;
  249. $dn = Db::name('cat_specs')->where(['specs_id'=>$st,'is_del'=>0])->save($int);
  250. if ($dn==false) {
  251. Db::rollback();
  252. return error_show(1002, "编辑失败");
  253. }
  254. }
  255. if(!empty($sn)){
  256. $vat=[];
  257. foreach ($sn as $value){
  258. $item=[];
  259. $item['cat_id']=$idinfo['id'];
  260. $item['specs_id']=$value;
  261. $item['is_del']=0;
  262. $item['addtime'] = date("Y-m-d H:i:s");
  263. $vat[] = $item;
  264. }
  265. $vp = Db::name('cat_specs')->insertAll($vat);
  266. if ($vp==0) {
  267. Db::rollback();
  268. return error_show(1002, "编辑失败");
  269. }
  270. }
  271. Db::commit();
  272. return error_show(0,"编辑成功");
  273. }else{
  274. Db::rollback();
  275. return error_show(1002,"编辑失败");
  276. }
  277. }catch (\Exception $e){
  278. Db::rollback();
  279. return error_show(1005,$e->getMessage());
  280. }
  281. }
  282. public function info(){
  283. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  284. if($id==""){
  285. return error_show(1002,"参数id不能为空");
  286. }
  287. $idinfo = Db::name('cat')->where(['id'=>$id])->find();
  288. if($idinfo==""){
  289. return error_show(1002,"未找到数据");
  290. }
  291. $info = Db::name('cat_specs')->where(['cat_id'=>$idinfo['id'],'is_del'=>0])->find();
  292. $temp = Db::name('specs')->where(['id'=>$info['specs_id'],'is_del'=>0])->find();
  293. $idinfo['cat_id']=$info['cat_id'];
  294. $idinfo['spec_name']=$temp['spec_name'];
  295. return app_show(0,"获取成功",$idinfo);
  296. }
  297. public function status(){
  298. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  299. if($id===""){
  300. return error_show(1002,"参数id不能为空");
  301. }
  302. $stn = Db::name('cat')->where(['id'=>$id])->find();
  303. if(empty($stn)){
  304. return error_show(1002,"未找到商品数据");
  305. }
  306. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  307. if($status===""){
  308. return error_show(1002,"参数status不能为空");
  309. }
  310. $pd= $stn['status'];
  311. $stn['status']=$status;
  312. $stn['updatetime']=date("Y-m-d H:i:s");
  313. $str = Db::name('cat')->where(['id'=>$id])->save($stn);
  314. if($str){
  315. $order = ["order_code"=>$id,"status"=>$pd,"action_remark"=>'',"action_type"=>"edit"];
  316. ActionLog::logAdd($this->post['token'],$order,"sxd",$stn['status'],$order);
  317. return error_show(0,"状态更新成功");
  318. }else{
  319. return error_show(1002,"状态更新失败");
  320. }
  321. }
  322. }