package com.sqx.modules.userinfo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserInfo;
import com.sqx.modules.app.service.UserInfoService;
import com.sqx.modules.userinfo.dao.UserInfoDAO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    private final UserInfoService userInfoService;

    public UserInfoController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @GetMapping("/list")
    public Result getList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size,
                          @RequestParam(required = false) String phone, @RequestParam(required = false) String name) {

        return Result.success().put("data", userInfoService.getList(page, size, phone, name));
    }


    @PutMapping
    public Result update(@RequestBody @Validated UserInfoDAO userInfoDAO) {
        UserInfo info = new UserInfo();
        BeanUtil.copyProperties(userInfoDAO, info);
        info.setUpdateTime(DateUtil.date());
        return Result.success().put("data", userInfoService.update(info, new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, userInfoDAO.getUserId())));
    }

    @DeleteMapping
    public Result delete(@RequestBody @Validated UserInfoDAO userInfoDAO) {
        return Result.success().put("data", userInfoService.remove(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, userInfoDAO.getUserId())));
    }
}
