package com.ruoyi.web.controller.zdsz;

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

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

import javax.annotation.Resource;
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.zdsz.domain.vo.ZHouseVo;
import com.ruoyi.zdsz.domain.bo.ZHouseBo;
import com.ruoyi.zdsz.service.IZHouseService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间管理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/house")
public class ZHouseController extends BaseController {

    private final IZHouseService iZHouseService;

    @Resource
    private IZEngineeringCivilService civilService;

    /**
     * 查询房间管理列表
     */
    @SaCheckPermission("zdsz:house:list")
    @GetMapping("/list")
    public TableDataInfo<ZHouseVo> list(ZHouseBo bo, PageQuery pageQuery) {
        return iZHouseService.queryPageList(bo, pageQuery);
    }
    /**
     * 房间下拉
     *
     */
    @GetMapping("/getHousesList")
    public R getHousesList(String unitId) {
        return R.ok(iZHouseService.getHouses(unitId));
    }
    /**
     * 户主查房间信息（小程序端）
     */
    @GetMapping("/getHousesByIdCard")
    public R getHousesByIdCard(String idCard) {
        return R.ok(iZHouseService.getHousesByIdCard(idCard));
    }
    /**
     * 导出房间管理列表
     */
    @SaCheckPermission("zdsz:house:export")
    @Log(title = "房间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZHouseBo bo, HttpServletResponse response) {
        List<ZHouseVo> list = iZHouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "房间管理", ZHouseVo.class, response);
    }

    /**
     * 获取房间管理详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ZHouseVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iZHouseService.queryById(id));
    }

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

    /**
     * 修改房间管理
     */
    @SaCheckPermission("zdsz:house:edit")
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZHouseBo bo) {
        return toAjax(iZHouseService.updateByBo(bo));
    }
    /**
     * 修改房间管理
     */
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/editById")
    public R<Void> editById( @RequestBody ZHouseBo bo) {
        return toAjax(iZHouseService.updateByBo(bo));
    }

    /**
     * 删除房间管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zdsz:house:remove")
    @Log(title = "房间管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        //是否需要校验
//        int i = civilService.getCivilByhouseId(Arrays.asList(ids));
//        if (i>0) {
//            return R.fail("房间已关联民用工程不能删除!");
//        }
        return toAjax(iZHouseService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
//    @SaCheckPermission("zdsz:house:remove")
//    @Log(title = "房间信息", businessType = BusinessType.Q)
    @GetMapping("/getHousesMessage")
    public R getHousesMessage(@RequestBody ZHouseBo zHouseBo) {
        //是否需要校验
//        int i = civilService.getCivilByhouseId(Arrays.asList(ids));
//        if (i>0) {
//            return R.fail("房间已关联民用工程不能删除!");
//        }
        return R.ok(iZHouseService.getHousesMessage(zHouseBo));
    }


}
