Exclusive.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. //泰康专属的一个列表
  8. class Exclusive extends Base
  9. {
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. }
  14. public function list(){
  15. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  16. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  17. $where =[["e.is_del","=",0]];
  18. $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  19. if($cat_name!==""){
  20. $where[]=['e.name',"like","%$cat_name%"];
  21. }
  22. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  23. if($pid!==""){
  24. $where[]=['e.pid',"=",$pid];
  25. }
  26. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  27. if($status!==""){
  28. $where[]=['e.status',"=",$status];
  29. }
  30. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  31. if ($company_name !== "") $where[] = ["e.createrid", 'in', get_company_item_user_by_name($company_name)];
  32. $count = Db::name("exclusive")->alias('e')->where($where)->count();
  33. $total = ceil($count / $size);
  34. $page = $page >= $total ? $total : $page;
  35. $list = Db::name('exclusive')
  36. ->alias('e')
  37. ->where($where)
  38. ->page($page, $size)
  39. ->select()
  40. ->toArray();
  41. $all_createrid = array_column($list,'createrid');
  42. $item = get_company_name_by_uid($all_createrid);
  43. foreach ($list as &$value){
  44. $value['company_name']=$item[$value['createrid']]??'';
  45. }
  46. return app_show(0, "获取成功", ['list' =>$list, 'count' => $count]);
  47. }
  48. public function add(){
  49. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  50. if($name==""){
  51. return error_show(1004,"参数name不能为空");
  52. }
  53. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  54. if($pid===""){
  55. return error_show(1002,"父级id不能为空");
  56. }
  57. $rename = Db::name('exclusive')->where(["pid"=>$pid,'is_del' => 0, 'name' => $name])->find();
  58. if (!empty($rename)) {
  59. return error_show(1002, "专属类型名称已存在");
  60. }
  61. $level=1;
  62. $search = $name;
  63. if($pid!==0){
  64. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  65. if($parent==false){
  66. return error_show(1003,"父级数据未找到");
  67. }
  68. $search=$parent['search']."/".$name;
  69. $level=$parent['level']+1;
  70. }
  71. $token = isset($this->post['token'])&&$this->post['token']!="" ? trim($this->post['token']):"";
  72. $user =GetUserInfo($token);
  73. if(empty($user)||$user['code']!=0){
  74. return error_show($user['code'],$user['msg']);
  75. }
  76. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  77. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  78. $data=[
  79. "name"=>$name,
  80. "pid"=>$pid,
  81. "level"=>$level,
  82. "search"=>$search,
  83. "createrid"=>$createrid,
  84. "creater"=>$creater,
  85. "status"=>0,
  86. "is_del"=>0,
  87. "addtime"=>date("Y-m-d H:i:s"),
  88. "updatetime"=>date("Y-m-d H:i:s")
  89. ];
  90. $in =Db::name("exclusive")->insert($data);
  91. if($in){
  92. return app_show(0,"新建成功");
  93. }else{
  94. return error_show(1003,"新建失败");
  95. }
  96. }
  97. //业务参数-专属类型管理的启用/禁用
  98. public function status()
  99. {
  100. // $id=isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']):"";
  101. // if($id===""){
  102. // return error_show(1004,"参数id不能为空");
  103. // }
  104. // $exclusive=Db::name("exclusive")->where(["id"=>$id,"is_del"=>0])->find();
  105. // if($exclusive==false){
  106. // return error_show(1004,"未找到数据");
  107. // }
  108. // $msg = $exclusive['status']==0? "启用":"禁用";
  109. // $exclusive['status'] = $exclusive['status']==0?1:0;
  110. // $exclusive['updatetime'] =date("Y-m-d H:i:s");
  111. // $in =Db::name("exclusive")->save($exclusive);
  112. // if($in){
  113. // return app_show(0,"{$msg}成功");
  114. // }else{
  115. // return error_show(1004,"{$msg}失败");
  116. // }
  117. $param = $this->request->only(['id', 'status']);
  118. $val = Validate::rule([
  119. 'id' => 'require|number|gt:0',
  120. 'status' => 'require|number|in:0,1',
  121. ]);
  122. if (!$val->check($param)) return error_show(1004, $val->getError());
  123. Db::startTrans();
  124. try {
  125. $res = Db::name('exclusive')
  126. ->field('id,level,status,pid')
  127. ->where('id', $param['id'])
  128. ->find();
  129. if (empty($res)) throw new Exception('该专属类型不存在');
  130. //启用
  131. if ($param['status'] == 1) {
  132. //一级
  133. if ($res['level'] == 1) {
  134. //是否存在子级
  135. $total = Db::name('exclusive')
  136. ->where(['pid' => $param['id'], 'is_del' => 0])
  137. ->count('id');
  138. if ($total) {
  139. //有子级的话,子级有一个启用,即可启用
  140. $child_id = Db::name('exclusive')
  141. ->field('id')
  142. ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
  143. ->find();
  144. if (empty($child_id)) throw new Exception('请先启用一个子级');
  145. }
  146. } else {
  147. //二级的话,顺便把它的上级也启用了
  148. Db::name('exclusive')
  149. ->where(['id' => $res['pid'], 'is_del' => 0, 'status' => 0])
  150. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  151. }
  152. } elseif ($param['status'] == 0) {
  153. //禁用
  154. if ($res['level'] == 1) {
  155. //是否存在子级
  156. $total = Db::name('exclusive')
  157. ->where(['pid' => $param['id'], 'is_del' => 0])
  158. ->count('id');
  159. if ($total) {
  160. //有子级的话,子级全部禁用,方可禁用
  161. $child_id = Db::name('exclusive')
  162. ->field('id')
  163. ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
  164. ->find();
  165. if (!empty($child_id)) throw new Exception('请先禁用全部子级');
  166. }
  167. }else{
  168. //如果它的兄弟全部禁用的话,连同它的父级也禁用
  169. $normal_c = Db::name('exclusive')
  170. ->field('id')
  171. ->where(['pid' => $res['pid'], 'is_del' => 0, 'status' => 1])
  172. ->where('id', '<>', $param['id'])
  173. ->find();
  174. if (empty($normal_c)) {
  175. Db::name('exclusive')
  176. ->where(['id' => $res['pid'], 'status' => 1])
  177. ->update(['status' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  178. }
  179. }
  180. }
  181. //修改自己
  182. Db::name('exclusive')
  183. ->where(['id' => $param['id'], 'is_del' => 0])
  184. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  185. Db::commit();
  186. return error_show(0, "状态更新成功");
  187. } catch (\Exception $e) {
  188. Db::rollback();
  189. return error_show(1005, $e->getMessage());
  190. }
  191. // $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  192. // if($id===""){
  193. // return error_show(1002,"参数id不能为空");
  194. // }
  195. // $stn = Db::name('exclusive')->where(['id'=>$id])->find();
  196. // if(empty($stn)){
  197. // return error_show(1002,"未找到商品数据");
  198. // }
  199. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  200. // if($status===""){
  201. // return error_show(1002,"参数status不能为空");
  202. // }
  203. // if ($stn['level'] == 2) {
  204. // if ($status == 1) {
  205. // $can = mai($stn['id']);
  206. // $cat = array_column($can, 'id');//子级如果启用,连同它的父级一起启用
  207. // } else {
  208. // $cat = $stn['id'];
  209. // }
  210. // // return error_show(1002, "所在级别不能启用");
  211. // }
  212. // if ($stn['level'] == 1) {
  213. // if ($status == 1) {
  214. // return error_show(1002, "所在等级不能启用");
  215. // } else {
  216. // $db = Db::name('exclusive')->where(['pid' => $stn['id'], 'status' => 1, 'is_del' => 0])->count();
  217. // if ($db == 0) {
  218. // $cat = $stn['id'];
  219. // } else {
  220. // return error_show(1002, "子级分类未禁用");
  221. // }
  222. // }
  223. // }
  224. // // $pd= $stn['status'];
  225. // $it = [];
  226. // $it['status'] = $status;
  227. // $it['updatetime'] = date("Y-m-d H:i:s");
  228. // $str = Db::name('exclusive')->where(['id' => $cat, 'is_del' => 0])->save($it);
  229. // if ($str) {
  230. //// $order = ["order_code"=>$id,"status"=>$pd,"action_remark"=>'',"action_type"=>"edit"];
  231. //// ActionLog::logAdd($this->post['token'],$order,"sxd",$stn['status'],$order);
  232. // return error_show(0, "状态更新成功");
  233. // } else {
  234. // return error_show(1002, "状态更新失败");
  235. // }
  236. }
  237. public function query(){
  238. // $where =[["is_del","=",0]];
  239. // $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
  240. // if($cat_name!==""){
  241. // $where[]=['name',"like","%$cat_name%"];
  242. // }
  243. // $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  244. // if($pid!==""){
  245. // $where[]=['pid',"=",$pid];
  246. // }
  247. // $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  248. // if($status!==""){
  249. // $where[]=['status',"=",$status];
  250. // }
  251. // $level=isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']) :"";
  252. // if($level!==""){
  253. // $where[]=['level',"=",$level];
  254. // }
  255. // $list = Db::name("exclusive")->where($where)->select();
  256. // return app_show(0,"获取成功",$list);
  257. $where=[["is_del","=",0]];
  258. $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
  259. $where[]=["pid","=",$pid];
  260. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? intval($this->post['cat_name']) :"";
  261. if($cat_name!==""){
  262. $where[]=['cat_name',"like","%$cat_name%"];
  263. }
  264. $data = Db::name("exclusive")->where($where)->select();
  265. $vmp = [];
  266. foreach ($data as $sts){
  267. $vmp[]=coco($sts);
  268. }
  269. return app_show(0,"获取成功",$vmp);
  270. }
  271. public function edit(){
  272. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  273. if($id==""){
  274. return error_show(1004,"参数id不能为空");
  275. }
  276. $info = Db::name('exclusive')->where(['id'=>$id,'is_del'=>0])->find();
  277. if($info==""){
  278. return error_show(1002,"未找到数据");
  279. }
  280. if($info['status']===1){
  281. return error_show(1004,"启用状态不可编辑");
  282. }
  283. $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  284. if($name==""){
  285. return error_show(1004,"参数name不能为空");
  286. }
  287. $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
  288. if($pid===""){
  289. return error_show(1004,"参数pid不能为空");
  290. }
  291. $rename = Db::name('exclusive')->where(["pid"=>$pid,'is_del' => 0, 'name' => $name])->where("id","<>",$id)
  292. ->findOrEmpty();
  293. if (!empty($rename)) {
  294. return error_show(1002, "专属类型名称已存在");
  295. }
  296. $level=1;
  297. $search = $name;
  298. if($pid!==0){
  299. $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
  300. if($parent==false){
  301. return error_show(1003,"父级数据未找到");
  302. }
  303. $search=$parent['search']."/".$name;
  304. $level=$parent['level']+1;
  305. }
  306. $data=[
  307. "id"=>$id,
  308. "name"=>$name,
  309. "pid"=>$pid,
  310. "level"=>$level,
  311. "search"=>$search,
  312. "status"=>0,
  313. "is_del"=>0,
  314. "updatetime"=>date("Y-m-d H:i:s")
  315. ];
  316. $in =Db::name("exclusive")->save($data);
  317. if($in){
  318. return app_show(0,"编辑成功");
  319. }else{
  320. return error_show(1003,"编辑失败");
  321. }
  322. }
  323. public function delete(){
  324. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
  325. $info = Db::name('exclusive')->where(["is_del"=>0,'id'=>$id])->find();
  326. if($info==false){
  327. return error_show(1002,"未找到数据");
  328. }
  329. $supp= Db::name('exclusive')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  330. if($supp){
  331. return error_show(0,"删除成功");
  332. }else{
  333. return error_show(1002,"删除失败");
  334. }
  335. }
  336. }