package com.yx.face.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yx.face.boot.core.BaseController;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.RestResponse;
import com.yx.face.boot.restful.RestResult;
import com.yx.face.model.search.UserWhitelistSearch;
import com.yx.face.model.vo.UserBlackListVO;
import com.yx.face.model.vo.UserWhitelistVO;
import com.yx.face.service.UserBlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @ClassName UserWhitelistController
 * @Author WXG
 * @Date 2021/11/5 14:12
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "B 黑名单 API")
@RestController
@RequestMapping("admin/userBlackList")
public class UserBlackListController extends BaseController {

    private final UserBlackListService userBlackListService;


    @ApiOperation("黑名单列表分页")
    @PostMapping("/getPageList")
    public RestResult<PageInfo<UserBlackListVO>> getPageList(@RequestBody RestDTO<UserWhitelistSearch> dto) {
        reBuildQuery(dto);
        PageInfo<UserBlackListVO> pageList = userBlackListService.getPageList(dto);
        if (pageList != null) {
            return RestResponse.ok(pageList);
        }
        return RestResponse.error("获取常客列表分页失败");
    }


    @SneakyThrows
    @ApiOperation("删除黑名单")
    @GetMapping("delete")
    public RestResult<Boolean> delete(@RequestParam("id") Integer id)  {
        Integer insert = userBlackListService.delete(id, getUserId());
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("删除失败");
    }


    private void reBuildQuery(@RequestBody RestDTO<UserWhitelistSearch> dto) {
        UserWhitelistSearch object = dto.getData();
        if (object == null) {
            object = new UserWhitelistSearch();
        }
        dto.setData(object);
    }



}
