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.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.biz.dal.entity.CooperateTenant;
import com.yonge.cooleshow.biz.dal.service.CooperateTenantService;
import com.yonge.cooleshow.biz.dal.wrapper.CooperateTenantWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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:}/cooperateTenant")
@Api(tags = "合作机构")
public class CooperateTenantController extends BaseController {

    @Autowired
    private CooperateTenantService cooperateTenantService;

    @ApiOperation(value = "详情", notes = "合作机构-根据详情ID查询单条, 传入id")
    @PreAuthorize("@pcs.hasPermissions('cooperateTenant/detail', {'BACKEND'})")
    //@GetMapping("/detail/{id}")
    public R<CooperateTenant> detail(@PathVariable("id") Long id) {

        CooperateTenant wrapper = cooperateTenantService.detail(id);

        return R.from(wrapper);
    }

    @ApiOperation(value = "查询分页", notes = "合作机构- 传入 CooperateTenantWrapper.CooperateTenantQuery")
    @PreAuthorize("@pcs.hasPermissions('cooperateTenant/page', {'BACKEND'})")
    @PostMapping("/page")
    public R<PageInfo<CooperateTenant>> page(@RequestBody CooperateTenantWrapper.CooperateTenantQuery query) {

        IPage<CooperateTenant> pages = cooperateTenantService.selectPage(QueryInfo.getPage(query), query);

        return R.from(QueryInfo.pageInfo(pages));
    }

    @ApiOperation(value = "新增", notes = "合作机构- 传入 CooperateTenantWrapper.CooperateTenant")
    @PreAuthorize("@pcs.hasPermissions('cooperateTenant/save', {'BACKEND'})")
    //@PostMapping("/save")
    public R<JSONObject> add(@Validated @RequestBody CooperateTenant cooperateTenant) {

        // 新增数据
        cooperateTenantService.save(cooperateTenant);

        return R.defaultR();
    }

    @ApiOperation(value = "修改", notes = "合作机构- 传入 CooperateTenantWrapper.CooperateTenant")
    @PreAuthorize("@pcs.hasPermissions('cooperateTenant/update', {'BACKEND'})")
    //@PostMapping("/update")
    public R<JSONObject> update(@Validated @RequestBody CooperateTenant cooperateTenant) {

        // 更新数据
        cooperateTenantService.updateById(cooperateTenant);

        return R.defaultR();
    }

    @ApiOperation(value = "删除", notes = "合作机构- 传入id")
    @PreAuthorize("@pcs.hasPermissions('cooperateTenant/remove', {'BACKEND'})")
    //@PostMapping("/remove")
    public R<Boolean> remove(@RequestParam Long id) {

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