package com.eleadmin.oa.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.common.system.service.UserService;
import com.eleadmin.oa.entity.Tenant;
import com.eleadmin.oa.param.TenantParam;
import com.eleadmin.oa.server.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户控制器
 *
 * @author gxwebsoft.com
 * @since 2020-03-14 11:29:03
 */
@Api(tags = "租户管理")
@RestController
@RequestMapping("/api/tenant")
public class TenantController extends BaseController {
    @Resource
    private TenantService tenantService;

    @Resource
    private UserService userService;

    @PreAuthorize("hasAuthority('sys:tenant:list')")
    @OperationLog
    @ApiOperation("分页查询租户")
    @GetMapping("/page")
    public ApiResult<PageResult<Tenant>> page(TenantParam param) {
        PageParam<Tenant, TenantParam> page = new PageParam<>(param);
        return success(tenantService.page(page, page.getWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:tenant:list')")
    @OperationLog
    @ApiOperation("查询全部租户")
    @GetMapping()
    public ApiResult<List<Tenant>> list(TenantParam param) {
        PageParam<Tenant, TenantParam> page = new PageParam<>(param);
        return success(tenantService.list(page.getOrderWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:tenant:list')")
    @OperationLog
    @ApiOperation("根据id查询租户")
    @GetMapping("/{id}")
    public ApiResult<Tenant> get(@PathVariable("id") Integer id) {
        return success(tenantService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:tenant:save')")
    @ApiOperation("添加租户")
    @PostMapping()
    public ApiResult<?> add(@RequestBody Tenant tenant) {
        if (tenantService.saveTenant(tenant)) {
          return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:tenant:update')")
    @OperationLog
    @ApiOperation("修改租户")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Tenant tenant) {
        if (tenantService.updateById(tenant)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:tenant:remove')")
    @OperationLog
    @ApiOperation("删除租户")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (tenantService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:tenant:save')")
    @OperationLog
    @ApiOperation("批量添加租户")
    @PostMapping("/batch")
    public ApiResult<List<String>> saveBatch(@RequestBody List<Tenant> list) {
        if (tenantService.saveBatch(list)) {
            return success("添加成功", null);
        }
        return fail("添加失败", null);
    }

    @PreAuthorize("hasAuthority('sys:tenant:remove')")
    @OperationLog
    @ApiOperation("批量删除租户")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (tenantService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:tenant:update')")
    @OperationLog
    @ApiOperation("重置密码")
    @PutMapping("/password")
    public ApiResult<?> resetPassword(@RequestBody Tenant tenant) {
      if (userService.update(new LambdaUpdateWrapper<User>()
        .eq(User::getUsername, "admin")
        .eq(User::getTenantId, tenant.getTenantId())
        .set(User::getPassword, userService.encodePassword(tenant.getPassword())))) {
        return success("操作成功");
      }
      return fail("操作失败");
    }
}
