package com.ruoyi.web.controller.zdsz;

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

import com.ruoyi.zdsz.service.IZUnitService;
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.ZBuildingVo;
import com.ruoyi.zdsz.domain.bo.ZBuildingBo;
import com.ruoyi.zdsz.service.IZBuildingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼栋管理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/building")
public class ZBuildingController extends BaseController {

    private final IZBuildingService iZBuildingService;

    @Autowired
    private IZUnitService unitService;

    /**
     * 楼宇下拉
     *
     * @return
     */
    @GetMapping("/getBuildingList")
    public R getBuildingList(String areaId) {
        return R.ok(iZBuildingService.getBuildingList(areaId));
    }

    /**
     * 查询楼栋管理列表
     */
    @SaCheckPermission("zdsz:building:list")
    @GetMapping("/list")
    public TableDataInfo<ZBuildingVo> list(ZBuildingBo bo, PageQuery pageQuery) {
        return iZBuildingService.getPageList(bo, pageQuery);
    }

    /**
     * 导出楼栋管理列表
     */
    @SaCheckPermission("zdsz:building:export")
    @Log(title = "楼栋管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZBuildingBo bo, HttpServletResponse response) {
        List<ZBuildingVo> list = iZBuildingService.queryList(bo);
        ExcelUtil.exportExcel(list, "楼栋管理", ZBuildingVo.class, response);
    }

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

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

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

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