package cn.ezhizao.project.tenant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.tenant.domain.BizTenantApply;
import cn.ezhizao.project.tenant.domain.BizUserTenant;
import cn.ezhizao.project.tenant.domain.Tenant;
import cn.ezhizao.project.tenant.service.IBizTenantApplyService;
import cn.ezhizao.project.tenant.service.IBizUserTenantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业加入申请记录Controller
 *
 * @author zhushan
 * @date 2023-08-31
 */
@RestController
@RequestMapping("/tenant/apply")
public class BizTenantApplyController extends BaseController
{
    @Resource
    private IBizTenantApplyService bizTenantApplyService;

    @Resource
    private IBizUserTenantService userTenantService;

    /**
     * 查询企业加入申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizTenantApply bizTenantApply) {
        startPage();
        List<BizTenantApply> list = bizTenantApplyService.getList(bizTenantApply);
        return getDataTable(list);
    }

    /**
     * 导出企业加入申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:export')")
    @Log(title = "企业加入申请记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizTenantApply bizTenantApply)
    {
        List<BizTenantApply> list = bizTenantApplyService.getList(bizTenantApply);
        ExcelUtil<BizTenantApply> util = new ExcelUtil<BizTenantApply>(BizTenantApply.class);
        util.exportExcel(response, list, "企业加入申请记录数据");
    }

    /**
     * 获取企业加入申请记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizTenantApplyService.getById(id));
    }


    @PreAuthorize("@ss.hasPermi('tenant:apply:add')")
    @Log(title = "加入申请", businessType = BusinessType.INSERT)
    @PostMapping("/joinApply")
    public AjaxResult joinApply(@RequestBody Tenant tenant)
    {
        BizTenantApply tenantApply = new BizTenantApply();
        tenantApply.setTenantId(tenant.getId());
        tenantApply.setUserId(SecurityUtils.getUserId());
        return toAjax(bizTenantApplyService.save(tenantApply));
    }


    @PreAuthorize("@ss.hasPermi('tenant:apply:edit')")
    @Log(title = "加入申请", businessType = BusinessType.INSERT)
    @PostMapping("/joinAudit")
    public AjaxResult joinAudit(@RequestBody BizTenantApply tenantApply)
    {
        if(tenantApply.getStatus()==1){
            tenantApply = bizTenantApplyService.getById(tenantApply.getId());
            /** 保险起见 先删除 */
            QueryWrapper<BizUserTenant> userTenantQueryWrapper = new QueryWrapper<>();
            userTenantQueryWrapper.eq("user_id",tenantApply.getUserId()).eq("tenant_id",tenantApply.getTenantId());
            userTenantService.remove(userTenantQueryWrapper);
            BizUserTenant userTenant = new BizUserTenant();
            userTenant.setTenantId(tenantApply.getTenantId());
            userTenant.setUserId(tenantApply.getUserId());
            userTenant.setDeptId(SecurityUtils.getDeptId());
            userTenantService.save(userTenant);
            tenantApply.setStatus(1);
        }
        tenantApply.setAuditId(SecurityUtils.getUserId());
        return toAjax(bizTenantApplyService.saveOrUpdate(tenantApply));
    }

    /**
     * 新增企业加入申请记录
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:add')")
    @Log(title = "企业加入申请记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizTenantApply bizTenantApply)
    {
        return toAjax(bizTenantApplyService.save(bizTenantApply));
    }

    /**
     * 修改企业加入申请记录
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:edit')")
    @Log(title = "企业加入申请记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTenantApply bizTenantApply)
    {
        return toAjax(bizTenantApplyService.updateById(bizTenantApply));
    }

    /**
     * 删除企业加入申请记录
     */
    @PreAuthorize("@ss.hasPermi('tenant:apply:remove')")
    @Log(title = "企业加入申请记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizTenantApplyService.removeBatchByIds(ids));
    }
}
