Specs.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ChangeLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. //商品规格
  8. class Specs extends Base
  9. {
  10. public $post="";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post=$this->request->post();
  15. }
  16. public function create(){
  17. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
  18. if($spec_name==""){
  19. return error_show(1002,"参数spec_name不能为空");
  20. }
  21. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  22. // if($token==''){
  23. // return error_show(105,"参数token不能为空");
  24. // }
  25. // $user =GetUserInfo($token);
  26. // if(empty($user)||$user['code']!=0){
  27. // return error_show(1002,"创建人数据不存在");
  28. // }
  29. $createrid= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  30. $creater= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  31. $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
  32. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"1";
  33. $tmp = Db::name('specs')
  34. ->field('id')
  35. ->where(['is_del'=>0,'spec_name'=>$spec_name])
  36. ->findOrEmpty();
  37. if(!empty($tmp)) return json_show(1004,'该规格值已存在');
  38. $data = [
  39. "spec_name"=>$spec_name,
  40. "creater"=>$creater,
  41. "createrid"=>$createrid,
  42. "exam_status"=>$exam_status,
  43. "status"=>$status,
  44. "is_del"=>0,
  45. "addtime"=>date("Y-m-d H:i:s"),
  46. "updatetime"=>date("Y-m-d H:i:s")
  47. ];
  48. $datainfo = Db::name('specs')->insert($data);
  49. return $datainfo?json_show(0,"新建成功"):json_show(1002,"创建失败");
  50. // if($datainfo){
  51. // return error_show(0,"新建成功");
  52. // }else{
  53. // return error_show(1002,"创建失败");
  54. // }
  55. }
  56. public function list(){
  57. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  58. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  59. $where=[["s.is_del","=",0]];
  60. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  61. if($spec_name!=""){
  62. $where[]=['s.spec_name',"like","%$spec_name%"];
  63. }
  64. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  65. if($status!==""){
  66. $where[]=['s.status',"=",$status];
  67. }
  68. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  69. if($creater!=""){
  70. $where[]=['s.creater',"like","%$creater%"];
  71. }
  72. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  73. if($start!==""){
  74. $where[]=['s.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  75. }
  76. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  77. if($end!==""){
  78. $where[]=['s.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  79. }
  80. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  81. if ($company_name !== "") $where[] = ["s.createrid", 'in', get_company_item_user_by_name($company_name)];
  82. $count = Db::name('specs')
  83. ->alias('s')
  84. ->where($where)
  85. ->count();
  86. $total = ceil($count / $size);
  87. $page = $page >= $total ? $total : $page;
  88. $list = Db::name('specs')
  89. ->alias('s')
  90. ->where($where)
  91. ->page($page,$size)
  92. ->order("addtime desc")
  93. ->select()
  94. ->toArray();
  95. $all_createrid = array_column($list,'createrid');
  96. $item = get_company_name_by_uid($all_createrid);
  97. foreach ($list as &$val){
  98. $val['company_name']=$item[$val['createrid']]??'';
  99. }
  100. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  101. }
  102. public function status(){
  103. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  104. if($id==""){
  105. return error_show(1002,"参数id不能为空");
  106. }
  107. $info = Db::name("specs")->where([["id","=",$id],["is_del","=",0]])->find();
  108. if(!$info){
  109. return error_show(1002,"未找到对应数据");
  110. }
  111. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  112. if($status===""){
  113. return error_show(1002,"参数status不能为空");
  114. }
  115. if(!in_array($status,[0,1])){
  116. return error_show(1002,"参数status无效");
  117. }
  118. $info['status']=$status;
  119. $info['updatetime']=date("Y-m-d H:i:s");
  120. $msg = $status==1?"启用":"禁用";
  121. $update = Db::name("specs")->save($info);
  122. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  123. }
  124. public function title(){
  125. $where=[["is_del","=",0]];
  126. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  127. if($spec_name!=""){
  128. $where[]=['spec_name',"like","%$spec_name%"];
  129. }
  130. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  131. if($status!=""){
  132. $where[]=['status',"=",$status];
  133. }
  134. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  135. if($creater!=""){
  136. $where[]=['creater',"like","%$creater%"];
  137. }
  138. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  139. if($start!==""){
  140. $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  141. }
  142. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  143. if($end!==""){
  144. $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  145. }
  146. $list = Db::name('specs')->where($where)->order("addtime desc")->select();
  147. return app_show(0,"获取成功",$list);
  148. }
  149. public function edit(){
  150. $id = isset($this->post['id']) && $this->post['id']!==""?intval($this->post['id']):"";
  151. if($id==""){
  152. return error_show(1002,"参数id不能为空");
  153. }
  154. $info = Db::name("specs")->where(['id'=>$id,"is_del"=>0])->find();
  155. if($info==""){
  156. return error_show(1002,"未找到数据");
  157. }
  158. if($info['status']==1){
  159. return error_show(1002,"状态是启用状态,无法编辑");
  160. }
  161. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
  162. if($spec_name==""){
  163. return error_show(1002,"参数spec_name不能为空");
  164. }
  165. $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
  166. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  167. $tmp = Db::name('specs')
  168. ->field('id')
  169. ->where(['is_del'=>0,'spec_name'=>$spec_name])
  170. ->where('id','<>',$id)
  171. ->findOrEmpty();
  172. if(!empty($tmp)) return json_show(1004,'该规格值已存在');
  173. $data = [
  174. "id"=>$id,
  175. "spec_name"=>$spec_name,
  176. "exam_status"=>$exam_status,
  177. "status"=>$status,
  178. "is_del"=>0,
  179. "updatetime"=>date("Y-m-d H:i:s")
  180. ];
  181. $datainfo=Db::name('specs')->save($data);
  182. $item = array_diff($data,$info);
  183. $json = json_encode($item,JSON_UNESCAPED_UNICODE);
  184. $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
  185. if($datainfo){
  186. ChangeLog::logAdd(5,$info['id'],$jsp,$json,['id' => $this->uid, 'nickname' => $this->uname]);
  187. return error_show(0,"更新成功");
  188. }else{
  189. return error_show(1002,"更新失败");
  190. }
  191. }
  192. public function del(){
  193. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  194. if($id===""){
  195. return error_show(1004,"参数id不能为空");
  196. }
  197. $str= Db::name('specs')->where(['id'=>$id,'is_del'=>0])->find();
  198. if(empty($str)){
  199. return error_show(1002,"未找到数据");
  200. }
  201. $end = Db::name('specs')->update(['id'=>$id,'is_del'=>1]);
  202. if($end){
  203. return error_show(0,"删除成功");
  204. }else{
  205. return error_show(1002,"删除失败");
  206. }
  207. }
  208. }