Specs.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 BaseController
  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= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  30. $creater= 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. $data = [
  34. "spec_name"=>$spec_name,
  35. "creater"=>$creater,
  36. "createrid"=>$createrid,
  37. "exam_status"=>$exam_status,
  38. "status"=>$status,
  39. "is_del"=>0,
  40. "addtime"=>date("Y-m-d H:i:s"),
  41. "updatetime"=>date("Y-m-d H:i:s")
  42. ];
  43. $datainfo = Db::name('specs')->insert($data);
  44. if($datainfo){
  45. return error_show(0,"新建成功");
  46. }else{
  47. return error_show(1002,"创建失败");
  48. }
  49. }
  50. public function list(){
  51. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  52. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  53. $where=[["s.is_del","=",0]];
  54. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  55. if($spec_name!=""){
  56. $where[]=['s.spec_name',"like","%$spec_name%"];
  57. }
  58. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  59. :"";
  60. if($status!==""){
  61. $where[]=['s.status',"=",$status];
  62. }
  63. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  64. if($creater!=""){
  65. $where[]=['s.creater',"like","%$creater%"];
  66. }
  67. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  68. if($start!==""){
  69. $where[]=['s.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  70. }
  71. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  72. if($end!==""){
  73. $where[]=['s.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  74. }
  75. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  76. if ($company_name !== "") $where[] = ["s.createrid", 'in', get_company_item_user_by_name($company_name)];
  77. $count = Db::name('specs')
  78. ->alias('s')
  79. ->where($where)
  80. ->count();
  81. $total = ceil($count / $size);
  82. $page = $page >= $total ? $total : $page;
  83. $list = Db::name('specs')
  84. ->alias('s')
  85. ->field('s.*,u.itemid')
  86. ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  87. ->where($where)
  88. ->append(['company_name'])
  89. ->withAttr('company_name',function($val,$data){
  90. return implode('/', array_column(GetPart($data['itemid']), 'name'));
  91. })
  92. ->page($page,$size)
  93. ->order("addtime desc")
  94. ->select()
  95. ->toArray();
  96. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  97. }
  98. public function status(){
  99. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  100. if($id==""){
  101. return error_show(1002,"参数id不能为空");
  102. }
  103. $info = Db::name("specs")->where([["id","=",$id],["is_del","=",0]])->find();
  104. if(!$info){
  105. return error_show(1002,"未找到对应数据");
  106. }
  107. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  108. if($status===""){
  109. return error_show(1002,"参数status不能为空");
  110. }
  111. if(!in_array($status,[0,1])){
  112. return error_show(1002,"参数status无效");
  113. }
  114. $info['status']=$status;
  115. $info['updatetime']=date("Y-m-d H:i:s");
  116. $msg = $status==1?"启用":"禁用";
  117. $update = Db::name("specs")->save($info);
  118. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  119. }
  120. public function title(){
  121. $where=[["is_del","=",0]];
  122. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  123. if($spec_name!=""){
  124. $where[]=['spec_name',"like","%$spec_name%"];
  125. }
  126. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  127. if($status!=""){
  128. $where[]=['status',"=",$status];
  129. }
  130. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  131. if($creater!=""){
  132. $where[]=['creater',"like","%$creater%"];
  133. }
  134. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  135. if($start!==""){
  136. $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  137. }
  138. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  139. if($end!==""){
  140. $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  141. }
  142. $list = Db::name('specs')->where($where)->order("addtime desc")->select();
  143. return app_show(0,"获取成功",$list);
  144. }
  145. public function edit(){
  146. $id = isset($this->post['id']) && $this->post['id']!==""?intval($this->post['id']):"";
  147. if($id==""){
  148. return error_show(1002,"参数id不能为空");
  149. }
  150. $info = Db::name("specs")->where(['id'=>$id,"is_del"=>0])->find();
  151. if($info==""){
  152. return error_show(1002,"未找到数据");
  153. }
  154. if($info['status']==1){
  155. return error_show(1002,"状态是启用状态,无法编辑");
  156. }
  157. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
  158. if($spec_name==""){
  159. return error_show(1002,"参数spec_name不能为空");
  160. }
  161. $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
  162. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  163. $data = [
  164. "id"=>$id,
  165. "spec_name"=>$spec_name,
  166. "exam_status"=>$exam_status,
  167. "status"=>$status,
  168. "is_del"=>0,
  169. "updatetime"=>date("Y-m-d H:i:s")
  170. ];
  171. $datainfo=Db::name('specs')->save($data);
  172. $item = array_diff($data,$info);
  173. $json = json_encode($item,JSON_UNESCAPED_UNICODE);
  174. $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
  175. if($datainfo){
  176. ChangeLog::logAdd(5,$info['id'],$jsp,$json,$this->post['token'],$this->post);
  177. return error_show(0,"更新成功");
  178. }else{
  179. return error_show(1002,"更新失败");
  180. }
  181. }
  182. public function del(){
  183. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  184. if($id===""){
  185. return error_show(1004,"参数id不能为空");
  186. }
  187. $str= Db::name('specs')->where(['id'=>$id,'is_del'=>0])->find();
  188. if(empty($str)){
  189. return error_show(1002,"未找到数据");
  190. }
  191. $end = Db::name('specs')->update(['id'=>$id,'is_del'=>1]);
  192. if($end){
  193. return error_show(0,"删除成功");
  194. }else{
  195. return error_show(1002,"删除失败");
  196. }
  197. }
  198. }