package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.area.service.AreaValidator;
import com.ruoyi.bst.areaSub.domain.AreaSub;
import com.ruoyi.bst.areaSub.domain.AreaSubQuery;
import com.ruoyi.bst.areaSub.domain.AreaSubVO;
import com.ruoyi.bst.areaSub.service.AreaSubService;
import com.ruoyi.bst.areaSub.service.AreaSubValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 子区域Controller
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/bst/areaSub")
public class AreaSubController extends BaseController
{

    @Autowired
    private AreaSubService areaSubService;

    @Autowired
    private AreaValidator areaValidator;

    @Autowired
    private AreaSubValidator areaSubValidator;

    /**
     * 查询子区域列表
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:list')")
    @GetMapping("/list")
    public TableDataInfo list(AreaSubQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<AreaSubVO> list = areaSubService.selectAreaSubList(query);
        return getDataTable(list);
    }

    /**
     * 根据运营区ID查询子区域列表
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:list')")
    @GetMapping("/listByAreaId")
    public AjaxResult listByAreaId(AreaSubQuery query)
    {
        query.setScope(true);
        if (query.getAreaId() == null) {
            return error("运营区ID不允许为空");
        }
        List<AreaSubVO> list = areaSubService.selectAreaSubList(query);
        return success(list);
    }

    /**
     * 导出子区域列表
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:export')")
    @Log(title = "子区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AreaSubQuery query)
    {
        query.setScope(true);
        List<AreaSubVO> list = areaSubService.selectAreaSubList(query);
        ExcelUtil<AreaSubVO> util = new ExcelUtil<AreaSubVO>(AreaSubVO.class);
        util.exportExcel(response, list, "子区域数据");
    }

    /**
     * 获取子区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(areaSubService.selectAreaSubById(id, true));
    }

    /**
     * 新增子区域
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:add')")
    @Log(title = "新增子区域", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.AREA_SUB)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) AreaSub areaSub)
    {
        if (areaSub.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canAddSubArea(areaSub.getAreaId()),
             "您无权在ID为%s的运营区下新增子区域", areaSub.getAreaId());
        }
        return toAjax(areaSubService.insertAreaSub(areaSub));
    }

    /**
     * 修改子区域
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:edit')")
    @Log(title = "修改子区域", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.AREA_SUB)
    @PutMapping
    public AjaxResult edit(@RequestBody AreaSub areaSub)
    {
        if (areaSub.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canEdit(areaSub.getAreaId()),
             "您无权在ID为%s的运营区下修改子区域", areaSub.getAreaId());
        }
        ServiceUtil.assertion(!areaSubValidator.canEdit(areaSub.getId()),
         "您无权修改ID为%s的子区域", areaSub.getId());
        return toAjax(areaSubService.updateAreaSub(areaSub));
    }

    /**
     * 删除子区域
     */
    @PreAuthorize("@ss.hasPermi('bst:areaSub:remove')")
    @Log(title = "删除子区域", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.AREA_SUB)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        ServiceUtil.assertion(!areaSubValidator.canDeleteAll(ids),
         "您无权删除ID为%s的子区域", ids);
        return toAjax(areaSubService.logicDel(ids));
    }
}
