package com.casic.missiles.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.application.service.AbstractTenantService;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dto.TenantDTO;
import com.casic.missiles.modular.system.model.Tenant;
import com.casic.missiles.modular.system.model.TenantDept;
import com.casic.missiles.modular.system.service.ITenantDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
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.ResponseBody;

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

/**
 * 组织绑定关系控制器
 *
 * @author dev
 * @Date 2022-06-07 13:03:40
 */
@Controller
@RequestMapping("/bus/tenant")
public class TenantDeptController extends BaseController {

    @Resource
    private ITenantDeptService tenantDeptService;

    @Resource
    private AbstractPermissionContext permissionService;

    /**
     * 获取组织绑定关系列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String tenantName) {
        tenantName = StringUtils.isEmpty(tenantName) ? "":tenantName;
        Long deptId = permissionService.getAuthService().getLoginUser().getDeptId();
        QueryWrapper<Tenant> query = new QueryWrapper<>();
        query.like("tenant_name", tenantName);
        List<TenantDTO> list = tenantDeptService.tenantList(query, deptId);
        return ResponseData.success(list);
    }

    /**
     * 获取组织绑定关系分页列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String tenantName) {
        Page<TenantDept> page = PageFactory.defaultPage();
        QueryWrapper<TenantDept> query = new QueryWrapper<>();
        query.like("tenant_name", tenantName);
        page = tenantDeptService.page(page, query);
        return super.packForBT(page);
    }

    /**
     * 新增组织绑定关系
     */
    @RequestMapping(value = "/relateTenant")
    @ResponseBody
    public Object add(@RequestBody TenantDept tenantDept) {
        tenantDeptService.relateTenant(tenantDept);
        return ResponseData.success();
    }

    /**
     * 删除组织绑定关系
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String tenantDeptId) {
        tenantDeptService.removeById(tenantDeptId);
        return ResponseData.success();
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("tenantDeptIds") List<Long> tenantDeptIds) {
        tenantDeptService.removeByIds(tenantDeptIds);
        return ResponseData.success();
    }

    /**
     * 修改组织绑定关系
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TenantDept tenantDept) {
        tenantDeptService.updateById(tenantDept);
        return ResponseData.success();
    }

}
