package com.tuoheng.system.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.annotation.Log;
import com.tuoheng.common.common.BaseController;
import com.tuoheng.common.enums.LogType;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.entity.Tenant;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.query.TenantQuery;
import com.tuoheng.system.service.ITenantService;
import net.sf.cglib.beans.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 系统租户表 前端控制器
 * </p>
 *
 * @author
 * @since 2022-12-31
 */
@RestController
@RequestMapping("/tenant")
public class TenantController extends BaseController {


    @Autowired
    private ITenantService tenantService;
    /**
     * 获取租户列表
     * @param  tenantQuery 查询条件
     * @return
     */
    @GetMapping("/index")
    public JsonResult index(TenantQuery tenantQuery) {
        return tenantService.getList(tenantQuery);
    }

    /**
     * 添加租户
     * @param jsonObject 实体对象
     * @return
     */
    @Log(title = "租户管理", logType = LogType.INSERT)
    @PostMapping("/add")
    public JsonResult add(@RequestBody JSONObject jsonObject) {
        String username = jsonObject.get("username").toString();
        String password = jsonObject.get("password").toString();
        String tenantName = jsonObject.get("tenantName").toString();
        String tenantCode = jsonObject.get("tenantCode").toString();
        return tenantService.add(new Tenant().setName(tenantName).setCode(tenantCode),
                new User().setUsername(username).setPassword(password));
    }

    /**
     * 编辑租户
     * @param entity 实体对象
     * @return
     */
    @Log(title = "租户管理", logType = LogType.UPDATE)
    //@RequiresPermissions("sys:tenant:edit")
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody Tenant entity) {
        return tenantService.edit(entity);
    }

    /**
     * 删除租户
     * @param tenantIds 租户ID
     * @return
     */
    @Log(title = "租户管理", logType = LogType.DELETE)
    //@RequiresPermissions("sys:tenant:delete")
    @DeleteMapping("/delete/{tenantIds}")
    public JsonResult delete(@PathVariable("tenantIds") Integer[] tenantIds) {
        return tenantService.deleteByIds(tenantIds);
    }



}
