package com.ruoyi.basic.controller;


import com.ruoyi.basic.api.domain.HwTenant;
import com.ruoyi.basic.service.IHwTenantService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 租户信息Controller
 *
 * @author xins
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/tenant")
public class HwTenantController extends BaseController
{
    @Autowired
    private IHwTenantService hwTenantService;

    /**
     * 分页查询租户信息列表
     */
    @RequiresPermissions("business:tenant:list")
    @GetMapping("/list")
    public TableDataInfo list(HwTenant hwTenant)
    {
        List<HwTenant> list = new ArrayList<HwTenant>();
        startPage();
        //todo 放到add中

         list = hwTenantService.selectHwTenantList(hwTenant);
        return getDataTable(list);
    }

    /**
     * 导出租户信息列表
     */
    @RequiresPermissions("business:tenant:export")
    @Log(title = "租户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HwTenant hwTenant)
    {
        List<HwTenant> list = hwTenantService.selectHwTenantList(hwTenant);
        ExcelUtil<HwTenant> util = new ExcelUtil<HwTenant>(HwTenant.class);
        util.exportExcel(response, list, "租户信息数据");
    }

    /**
     * 获取租户信息详细信息
     */
    @RequiresPermissions("business:tenant:query")
    @GetMapping(value = "/{tenantId}")
    public AjaxResult getInfo(@PathVariable("tenantId") Long tenantId)
    {
        return success(hwTenantService.selectHwTenantByTenantId(tenantId));
    }

    /**
     * 新增租户信息
     */
    @RequiresPermissions("business:tenant:add")
    @Log(title = "租户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HwTenant hwTenant)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        hwTenant.setIsRegister("0");
        hwTenant.setCreateBy(sysUser.getNickName());
        return toAjax(hwTenantService.insertHwTenant(hwTenant));
    }

    /**
     * 修改租户信息
     */
    @RequiresPermissions("business:tenant:edit")
    @Log(title = "租户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HwTenant hwTenant)
    {LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();

        hwTenant.setUpdateBy(sysUser.getNickName());
        return toAjax(hwTenantService.updateHwTenant(hwTenant));
    }

    /**
     * 删除租户信息
     */
    @RequiresPermissions("business:tenant:remove")
    @Log(title = "租户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable Long[] tenantIds)
    {
        return toAjax(hwTenantService.deleteHwTenantByTenantIds(tenantIds));
    }

    /**
     * 查询租户信息列表
     */
    @GetMapping("/getTenants")
    public R<List<HwTenant>> getTenants(HwTenant hwTenant)
    {
        return R.ok(hwTenantService.selectHwTenantList(hwTenant));
    }
}
