package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.admin.io.request.TenantInfoVo;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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;

/**
 * @Author：haonan
 * @Date：2023/7/24 9:57
 * @Filename：TenantInfoController
 */
@RestController
@RequestMapping("${app-config.url.admin:}/tenantInfo")
@Api(value = "机构管理", tags = "机构管理")
public class TenantInfoController extends BaseController {

    @Autowired
    TenantInfoService tenantInfoService;

    /**
     * 查询分页
     *
     * @param query
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "TenantInfo")
    @PreAuthorize("@pcs.hasPermissions('tenantInfo/page')")
    public HttpResponseResult<PageInfo<TenantInfoWrapper.TenantInfo>> page(@RequestBody TenantInfoWrapper.TenantInfoQuery query) {

        IPage<TenantInfoWrapper.TenantInfo> pages = tenantInfoService.selectPage(query);
        return succeed(PageUtil.pageInfo(pages));
    }


    /**
     * 插入数据
     */
    @PostMapping("/add")
    @ApiOperation(value = "更新", notes = "传入TenantInfo")
    @PreAuthorize("@pcs.hasPermissions('tenantInfo/add')")
    public HttpResponseResult<Boolean> add( @RequestBody TenantInfoVo.TenantInfo info) {
        return succeed(tenantInfoService.add(JSON.parseObject(info.jsonString(), TenantInfo.class)));
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "传入TenantInfo")
    @PreAuthorize("@pcs.hasPermissions('tenantInfo/update')")
    public HttpResponseResult<Boolean> update( @RequestBody TenantInfoVo.TenantInfo info) {
        return succeed(tenantInfoService.update(JSON.parseObject(info.jsonString(), TenantInfo.class)));
    }

    /**
     * 冻结
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "冻结", notes = "传入id和布尔类型")
    @PreAuthorize("@pcs.hasPermissions('tenantInfo/updateStatus')")
    public HttpResponseResult<Boolean> updateStatus(@RequestBody TenantInfoWrapper.UpdateStatus updateStatus) {
        tenantInfoService.updateStatus(updateStatus);
        return succeed();
    }

    /**
     * 查询详情
     *
     * @param id
     */
    @PostMapping("/detail")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @PreAuthorize("@pcs.hasPermissions('tenantInfo/detail')")
    public HttpResponseResult<TenantInfoWrapper.TenantInfo> detail(@RequestParam("id")Long id) {

        TenantInfoWrapper.TenantInfo info = tenantInfoService.detailTenantInfo(id);
        return succeed(info);
    }

}
