CsdnUserController.java 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
package com.kwan.springbootkwan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.Result;
import com.kwan.springbootkwan.entity.dto.CsdnUserInfoDTO;
import com.kwan.springbootkwan.entity.query.CsdnUserInfoQuery;
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/csdn/user")
public class CsdnUserController {


    @Autowired
    private CsdnUserInfoService csdnUserInfoService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result selectAll(@RequestParam Integer page
            , @RequestParam Integer pageSize
            , @RequestParam String userName
            , @RequestParam String nickName) {
        Page<CsdnUserInfo> pageParm = new Page<>();
        pageParm.setCurrent(page);
        pageParm.setSize(pageSize);
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("user_weight");
        wrapper.eq("is_delete", 0);
        if (StringUtils.isNotEmpty(userName)) {
            wrapper.eq("user_name", userName);
        }
        if (StringUtils.isNotEmpty(nickName)) {
            wrapper.like("nick_name", nickName);
        }
        return Result.ok(CsdnUserInfoDTO.Converter.INSTANCE.from(this.csdnUserInfoService.page(pageParm, wrapper)));
    }


    /**
     * 新增用户
     *
     * @return 所有数据
     */
    @PostMapping("/add")
    public Result add(@RequestBody CsdnUserInfoQuery addInfo) {
        final String userName = addInfo.getUserName();
        final Integer addType = addInfo.getAddType();
        if (StringUtils.isEmpty(userName)) {
            return Result.error("名字不能为空");
        }
        //批量添加
        if (addType == 1) {
            final String[] split = userName.split("\n");
            for (String str : split) {
                str = str.trim();
                if (StringUtils.isEmpty(str)) {
                    continue;
                }
                CsdnUserInfo csdnUserInfo = new CsdnUserInfo();
                QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
                wrapper.eq("user_name", str);
                wrapper.eq("is_delete", 0);
                final CsdnUserInfo one = this.csdnUserInfoService.getOne(wrapper);
                if (one == null) {
                    BeanUtils.copyProperties(addInfo, csdnUserInfo);
                    csdnUserInfo.setUserName(str);
                    this.csdnUserInfoService.save(csdnUserInfo);
                }
            }
        } else {
            CsdnUserInfo csdnUserInfo = new CsdnUserInfo();
            QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("user_name", userName);
            wrapper.eq("is_delete", 0);
            final CsdnUserInfo one = this.csdnUserInfoService.getOne(wrapper);
            if (one == null) {
                BeanUtils.copyProperties(addInfo, csdnUserInfo);
                this.csdnUserInfoService.save(csdnUserInfo);
                return Result.ok();
            } else {
                return Result.error("该用户已存在");
            }
        }
        return Result.ok();
    }

    /**
     * 更新用户
     *
     * @param query
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CsdnUserInfoQuery query) {
        CsdnUserInfo csdnUserInfo = new CsdnUserInfo();
        BeanUtils.copyProperties(query, csdnUserInfo);
        return Result.ok(this.csdnUserInfoService.updateById(csdnUserInfo));
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        CsdnUserInfo csdnUserInfo = new CsdnUserInfo();
        csdnUserInfo.setIsDelete(1);
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return Result.ok(this.csdnUserInfoService.update(csdnUserInfo, wrapper));
    }

}