package com.ruoyi.web.controller.zdsz;

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

import com.ruoyi.zdsz.service.IZHouseService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
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.zdsz.domain.vo.ZUnitVo;
import com.ruoyi.zdsz.domain.bo.ZUnitBo;
import com.ruoyi.zdsz.service.IZUnitService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元管理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/unit")
public class ZUnitController extends BaseController {

    private final IZUnitService iZUnitService;

    @Autowired
    private IZHouseService houseService;
    /**
     * 单元下拉
     *
     * @return
     */
    @GetMapping("/getUnitList")
    public R getUnitList(String buildingId) {
        return R.ok(iZUnitService.getUnitList(buildingId));
    }

    /**
     * 查询单元管理列表
     */
    @SaCheckPermission("zdsz:unit:list")
    @GetMapping("/list")
    public TableDataInfo<ZUnitVo> list(ZUnitBo bo, PageQuery pageQuery) {
        return iZUnitService.getPageList(bo, pageQuery);
    }

    /**
     * 导出单元管理列表
     */
    @SaCheckPermission("zdsz:unit:export")
    @Log(title = "单元管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZUnitBo bo, HttpServletResponse response) {
        List<ZUnitVo> list = iZUnitService.queryList(bo);
        ExcelUtil.exportExcel(list, "单元管理", ZUnitVo.class, response);
    }

    /**
     * 获取单元管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zdsz:unit:query")
    @GetMapping("/{id}")
    public R<ZUnitVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iZUnitService.queryById(id));
    }

    /**
     * 新增单元管理
     */
    @SaCheckPermission("zdsz:unit:add")
    @Log(title = "单元管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZUnitBo bo) {
        return toAjax(iZUnitService.insertByBo(bo));
    }

    /**
     * 修改单元管理
     */
    @SaCheckPermission("zdsz:unit:edit")
    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZUnitBo bo) {
        return toAjax(iZUnitService.updateByBo(bo));
    }

    /**
     * 删除单元管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zdsz:unit:remove")
    @Log(title = "单元管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        //是否需要校验
//        int i = houseService.getHouseByUnitId(Arrays.asList(ids));
//        if (i>0) {
//            return R.fail("单元已关联房间不能删除!");
//        }
        return toAjax(iZUnitService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
