package com.ruoyi.pay.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PayTenantVo;
import com.ruoyi.pay.domain.bo.PayTenantBo;
import com.ruoyi.pay.service.IPayTenantService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租户
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/tenant")
public class PayTenantController extends BaseController {

    private final IPayTenantService iPayTenantService;

    /**
     * 查询租户列表
     */
    @SaCheckPermission("pay:tenant:list")
    @GetMapping("/list")
    public TableDataInfo<PayTenantVo> list(PayTenantBo bo, PageQuery pageQuery) {
        return iPayTenantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户列表
     */
    @SaCheckPermission("pay:tenant:export")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayTenantBo bo, HttpServletResponse response) {
        List<PayTenantVo> list = iPayTenantService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户", PayTenantVo.class, response);
    }

    /**
     * 获取租户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:tenant:query")
    @GetMapping("/{id}")
    public R<PayTenantVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayTenantService.queryById(id));
    }

    /**
     * 新增租户
     */
    @SaCheckPermission("pay:tenant:add")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayTenantBo bo) {
        //添加验证  手机号相同或者公司名相同不允许注册
        PayTenantBo queryBo = new PayTenantBo();
        queryBo.setPhone(bo.getPhone());
        List<PayTenantVo> asCompanyVos = iPayTenantService.queryList(queryBo);
        if(asCompanyVos!=null&&asCompanyVos.size()>0){
            return  new R().fail("手机号已注册");
        }
        queryBo.setName(bo.getName());
        queryBo.setPhone(null);
        List<PayTenantVo> asCompanyVos2 = iPayTenantService.queryList(queryBo);
        if(asCompanyVos2!=null&&asCompanyVos2.size()>0){
            return  new R().fail("公司名已注册");
        }
        return toAjax(iPayTenantService.insertByBo(bo));
    }

    /**
     * 修改租户
     */
    @SaCheckPermission("pay:tenant:edit")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayTenantBo bo) {
        return toAjax(iPayTenantService.updateByBo(bo));
    }

    /**
     * 删除租户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:tenant:remove")
    @Log(title = "租户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPayTenantService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
