package org.dromara.controller.houseInfo;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.houseInfo.bo.HouseInfoBo;
import org.dromara.domain.houseInfo.vo.HouseInfoVo;
import org.dromara.listener.HouseInfoImportListener;
import org.dromara.service.IHouseInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 房屋信息
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/houseInfo")
public class HouseInfoController extends BaseController {

    private final IHouseInfoService houseInfoService;

    /**
     * 查询房屋信息列表
     */
    @SaCheckPermission("wuYe:houseInfo:list")
    @GetMapping("/list")
    public TableDataInfo<HouseInfoVo> list(HouseInfoBo bo, PageQuery pageQuery) {
        return houseInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询房屋信息列表不分页
     */
    @SaCheckPermission("wuYe:houseInfo:listNoPage")
    @GetMapping("/listNoPage")
    public R<List<HouseInfoVo>> listNoPage(HouseInfoBo bo) {
        List<HouseInfoVo> list = houseInfoService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 查询房屋信息列表不分页查询条件带租户
     */
   // @SaCheckPermission("wuYe:houseInfo:listNoPageTenant")
    @GetMapping("/listNoPageTenant")
    public R<List<HouseInfoVo>> listNoPageTenant(HouseInfoBo bo) {
        List<HouseInfoVo> list = TenantHelper.ignore(() -> {
            return houseInfoService.queryList(bo);
        });
        return R.ok(list);
    }

    /**
     * 导出房屋信息列表
     */
    @SaCheckPermission("wuYe:houseInfo:export")
    @Log(title = "房屋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HouseInfoBo bo, HttpServletResponse response) {
        List<HouseInfoVo> list = houseInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "房屋信息", HouseInfoVo.class, response);
    }

    /**
     * 导入房屋信息列表
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<HouseInfoVo> result = ExcelUtil.importExcel(file.getInputStream(), HouseInfoVo.class, new HouseInfoImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取房屋信息详细信息
     *
     * @param houseId 主键
     */
    @SaCheckPermission("wuYe:houseInfo:query")
    @GetMapping("/{houseId}")
    public R<HouseInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long houseId) {
        return R.ok(houseInfoService.queryById(houseId));
    }

    /**
     * 新增房屋信息
     */
    @SaCheckPermission("wuYe:houseInfo:add")
    @Log(title = "房屋信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HouseInfoBo bo) {
        return toAjax(houseInfoService.insertByBo(bo));
    }

    /**
     * 修改房屋信息
     */
    @SaCheckPermission("wuYe:houseInfo:edit")
    @Log(title = "房屋信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HouseInfoBo bo) {
        return toAjax(houseInfoService.updateByBo(bo));
    }

    /**
     * 删除房屋信息
     *
     * @param houseIds 主键串
     */
    @SaCheckPermission("wuYe:houseInfo:remove")
    @Log(title = "房屋信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{houseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] houseIds) {
        return toAjax(houseInfoService.deleteWithValidByIds(List.of(houseIds), true));
    }
}
