package com.yonge.cooleshow.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.biz.dal.entity.UnbindAuthUser;
import com.yonge.cooleshow.biz.dal.service.UnbindAuthUserService;
import com.yonge.cooleshow.biz.dal.wrapper.UnbindAuthUserWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/unbindAuthUser")
@Api(tags = "解绑审核人员设置")
public class UnbindAuthUserController {

    @Autowired
    private UnbindAuthUserService unbindAuthUserService;

    
    @ApiOperation(value = "查询分页", notes = "解绑审核人员设置- 传入 UnbindAuthUserWrapper.UnbindAuthUserQuery") 
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<UnbindAuthUserWrapper.UnbindAuthUser>> page(@RequestBody UnbindAuthUserWrapper.UnbindAuthUserQuery query) {
        
        IPage<UnbindAuthUserWrapper.UnbindAuthUser> pages = unbindAuthUserService.selectPage(QueryInfo.getPage(query), query);
        
        return HttpResponseResult.succeed(PageUtil.pageInfo(pages));
	}
    
    @ApiOperation(value = "新增", notes = "解绑审核人员设置- 传入 UnbindAuthUserWrapper.UnbindAuthUser")
	@PostMapping("/save")
	public HttpResponseResult<JSONObject> add(@Validated @RequestBody UnbindAuthUser unbindAuthUser) {

        if (unbindAuthUser.getUserId() == null) {
            throw new BizException("用户ID不能为空");
        }

        List<UnbindAuthUser> list = unbindAuthUserService.list();
        if (!CollectionUtils.isEmpty(list)) {
            unbindAuthUser.setId(list.get(0).getId());
        }
        // 新增数据
        unbindAuthUserService.save(unbindAuthUser);
        
        return HttpResponseResult.succeed();
	}
    
    @ApiOperation(value = "修改", notes = "解绑审核人员设置- 传入 UnbindAuthUserWrapper.UnbindAuthUser")
	@PostMapping("/update")
	public HttpResponseResult<JSONObject> update(@Validated @RequestBody UnbindAuthUser unbindAuthUser) {

        if (unbindAuthUser.getId() == null) {
            throw new BizException("ID不能为空");
        }

        if (unbindAuthUser.getUserId() == null) {
            throw new BizException("用户ID不能为空");
        }
        // 更新数据
        unbindAuthUserService.updateById(unbindAuthUser);

        return HttpResponseResult.succeed();
	}

	@ApiOperation(value = "删除", notes = "解绑审核人员设置- 传入id")
	@PostMapping("/remove")
	public HttpResponseResult<Boolean> remove(@RequestParam Long id) {
    
		return HttpResponseResult.succeed(unbindAuthUserService.removeById(id));
	}
}