package com.ruoyi.web.controller.zdsz;

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

import com.ruoyi.zdsz.service.*;
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.ZAreaVo;
import com.ruoyi.zdsz.domain.bo.ZAreaBo;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小区管理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/area")
public class ZAreaController extends BaseController {

    private final IZAreaService iZAreaService;
    @Resource
    private IZBuildingService buildingService;
    @Resource
    private IZEngineeringCivilService civilService;
    @Resource
    private IZEngineeringMaterialsResetsService materialsResetsService;
    /**
     * 小区下拉
     *
     * @return
     */
    @GetMapping("/getAreaList")
    public R getArea(String district) {
        return R.ok(iZAreaService.getArea(district));
    }


    /**
     * 查询小区管理列表
     */
    @SaCheckPermission("zdsz:area:list")
    @GetMapping("/list")
    public TableDataInfo<ZAreaVo> list(ZAreaBo bo, PageQuery pageQuery) {
        return iZAreaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小区管理列表
     */
    @SaCheckPermission("zdsz:area:export")
    @Log(title = "小区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZAreaBo bo, HttpServletResponse response) {
        List<ZAreaVo> list = iZAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "小区管理", ZAreaVo.class, response);
    }

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

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

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

    /**
     * 删除小区管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zdsz:area:remove")
    @Log(title = "小区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        //是否需要校验
        int i = buildingService.getBuildingListByAreaId(Arrays.asList(ids));
        if (i>0){
            return R.fail("小区已关联楼栋不能删除!");
        }
//        int j = civilService.getCivilByAreaId(Arrays.asList(ids));
//        if (j>0){
//            return R.fail("小区已关联民用工程不能删除!");
//        }
        int k = materialsResetsService.getMaterialByAreaId(Arrays.asList(ids));
        if (k>0){
            return R.fail("小区已关联用料统计不能删除!");
        }
        return toAjax(iZAreaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
