package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.admin.io.request.UserTenantBindRecordVo;
import com.yonge.cooleshow.biz.dal.entity.UserTenantBindRecord;
import com.yonge.cooleshow.biz.dal.service.UserTenantBindRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.UserTenantBindRecordWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
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
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/userTenantBindRecord")
@Api(tags = "用户机构绑定解绑记录")
public class UserTenantBindRecordController extends BaseController {

    @Autowired
    private UserTenantBindRecordService userTenantBindRecordService;

    @ApiOperation(value = "详情", notes = "用户机构绑定解绑记录-根据详情ID查询单条, 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
    @PreAuthorize("@pcs.hasPermissions('userTenantBindRecord/detail', {'BACKEND'})")
//    @GetMapping("/detail/{id}")
    public R<UserTenantBindRecordVo.UserTenantBindRecord> detail(@PathVariable("id") Long id) {

        UserTenantBindRecord wrapper = userTenantBindRecordService.detail(id);

        return R.from(UserTenantBindRecordVo.UserTenantBindRecord.from(JSON.toJSONString(wrapper)));
    }

    @ApiOperation(value = "查询分页", notes = "用户机构绑定解绑记录- 传入 UserTenantBindRecordVo.UserTenantBindRecordQuery")
    @PreAuthorize("@pcs.hasPermissions('userTenantBindRecord/page', {'BACKEND'})")
    @PostMapping("/page")
    public R<PageInfo<UserTenantBindRecordWrapper.UserTenantBindRecord>> page(@RequestBody UserTenantBindRecordWrapper.UserTenantBindRecordQuery query) {

        if (query.getTenantId() == null || StringUtils.isEmpty(query.getUserType())) {
            throw new BizException("参数错误");
        }
        // 查询数据
        IPage<UserTenantBindRecordWrapper.UserTenantBindRecord> pages = userTenantBindRecordService.selectPage(QueryInfo.getPage(query), query);
        return R.from(QueryInfo.pageInfo(pages, pages.getRecords()));
    }

    @ApiOperation(value = "新增", notes = "用户机构绑定解绑记录- 传入 UserTenantBindRecordVo.UserTenantBindRecord")
    @PreAuthorize("@pcs.hasPermissions('userTenantBindRecord/save', {'BACKEND'})")
//    @PostMapping("/save")
    public R<JSONObject> add(@Validated @RequestBody UserTenantBindRecordVo.UserTenantBindRecord userTenantBindRecordVo) {

        // 新增数据
        userTenantBindRecordService.save(JSON.parseObject(userTenantBindRecordVo.jsonString(), UserTenantBindRecord.class));

        return R.defaultR();
    }

    @ApiOperation(value = "修改", notes = "用户机构绑定解绑记录- 传入 UserTenantBindRecordVo.UserTenantBindRecord")
    @PreAuthorize("@pcs.hasPermissions('userTenantBindRecord/update', {'BACKEND'})")
//    @PostMapping("/update")
    public R<JSONObject> update(@Validated @RequestBody UserTenantBindRecordVo.UserTenantBindRecord userTenantBindRecordVo) {

        // 更新数据
        userTenantBindRecordService.updateById(JSON.parseObject(userTenantBindRecordVo.jsonString(), UserTenantBindRecord.class));

        return R.defaultR();
    }

    @ApiOperation(value = "删除", notes = "用户机构绑定解绑记录- 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
    @PreAuthorize("@pcs.hasPermissions('userTenantBindRecord/remove', {'BACKEND'})")
//    @PostMapping("/remove")
    public R<Boolean> remove(@RequestParam Long id) {

        return R.from(userTenantBindRecordService.removeById(id));
    }
}