package com.xinelu.manage.controller.communityinfo;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.communityinfo.CommunityInfo;
import com.xinelu.manage.dto.community.CommunityInfoDTO;
import com.xinelu.manage.service.communityinfo.ICommunityInfoService;
import com.xinelu.manage.vo.community.CommunityInfoVO;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 居住社区信息Controller
 *
 * @author xinyilu
 * @date 2022-09-13
 */
@RestController
@RequestMapping("/system/communityInfo")
public class CommunityController extends BaseController {
    @Resource
    private ICommunityInfoService communityInfoService;

    /**
     * 查询居住社区信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityInfoVO communityInfo) {
        startPage();
        List<CommunityInfoVO> list = communityInfoService.selectCommunityInfoList(communityInfo);
        return getDataTable(list);
    }

    /**
     * 导出居住社区信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:export')")
    @Log(title = "居住社区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityInfoVO communityInfo) {
        List<CommunityInfoVO> list = communityInfoService.selectCommunityInfoList(communityInfo);
        ExcelUtil<CommunityInfoVO> util = new ExcelUtil<>(CommunityInfoVO.class);
        util.exportExcel(response, list, "居住社区信息数据");
    }

    /**
     * 获取居住社区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(communityInfoService.selectCommunityInfoById(id));
    }

    /**
     * 新增居住社区信息
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:add')")
    @Log(title = "居住社区信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody CommunityInfoDTO communityInfo) throws Exception {
        return communityInfoService.insertCommunityInfoList(communityInfo.getCommunityInfoList());
    }

    /**
     * 修改居住社区信息
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:edit')")
    @Log(title = "居住社区信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody CommunityInfo communityInfo) throws Exception {
        return communityInfoService.updateCommunityInfo(communityInfo);
    }

    /**
     * 删除居住社区信息
     */
    @PreAuthorize("@ss.hasPermi('system:communityInfo:remove')")
    @Log(title = "居住社区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(communityInfoService.deleteCommunityInfoByIds(ids));
    }

    /**
     * 查询省级区域信息信息
     *
     * @return 省级区域信息集合
     */
    @GetMapping("/getFirstLevelInfo")
    public AjaxResult getFirstLevelInfo() {
        return communityInfoService.getProvinceAreaInfo();
    }

    /**
     * 根据父级id
     *
     * @param parentId 父级id
     * @return 所属下级区域信息集合
     */
    @GetMapping("/getSecondaryLevelInfo")
    public AjaxResult getSecondaryLevelInfo(Long parentId) {
        if (Objects.isNull(parentId)) {
            return AjaxResult.error("上级区域id不能为空");
        }
        return communityInfoService.getSubordinateAreaInfo(parentId);
    }
}