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.areaJoin.domain.AreaJoin;
import com.ruoyi.bst.areaJoin.domain.AreaJoinQuery;
import com.ruoyi.bst.areaJoin.domain.AreaJoinVO;
import com.ruoyi.bst.areaJoin.service.AreaJoinConverter;
import com.ruoyi.bst.areaJoin.service.AreaJoinService;
import com.ruoyi.bst.areaJoin.service.AreaJoinValidator;
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.poi.ExcelUtil;

/**
 * 运营加盟Controller
 *
 * @author ruoyi
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/bst/areaJoin")
public class AreaJoinController extends BaseController
{
    @Autowired
    private AreaJoinService areaJoinService;

    @Autowired
    private AreaJoinValidator areaJoinValidator;

    @Autowired
    private AreaValidator areaValidator;

    @Autowired
    private AreaJoinConverter areaJoinConverter;

    /**
     * 查询运营加盟列表
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:list')")
    @GetMapping("/list")
    public TableDataInfo list(AreaJoinQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<AreaJoinVO> list = areaJoinService.selectAreaJoinList(query);
        return getDataTable(list);
    }

    /**
     * 导出运营加盟列表
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:export')")
    @Log(title = "运营加盟", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AreaJoinQuery query)
    {
        query.setScope(true);
        List<AreaJoinVO> list = areaJoinService.selectAreaJoinList(query);
        ExcelUtil<AreaJoinVO> util = new ExcelUtil<AreaJoinVO>(AreaJoinVO.class);
        util.exportExcel(response, list, "运营加盟数据");
    }

    /**
     * 获取运营加盟详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(areaJoinService.selectAreaJoinById(id, true));
    }

    /**
     * 新增运营加盟
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:add')")
    @Log(title = "新增运营加盟", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.AREA_JOIN)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) AreaJoinVO areaJoin)
    {
        // 判断能否选择当前运营区
        if (!areaValidator.canCheckForJoin(areaJoin.getAreaId())) {
            return AjaxResult.error("您无权选择ID为" + areaJoin.getAreaId() + "的运营区");
        }
        areaJoin.setCreateId(getUserId());
        AreaJoin po = areaJoinConverter.toPoByCreate(areaJoin);
        return toAjax(areaJoinService.insertAreaJoin(po));
    }

    /**
     * 修改运营加盟
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:edit')")
    @Log(title = "修改运营加盟", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.AREA_JOIN)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Create.class) AreaJoinVO areaJoin)
    {
        // 判断能否操作当前加盟数据
        if (!areaJoinValidator.canEdit(areaJoin.getId())) {
            return AjaxResult.error("您无权修改ID为" + areaJoin.getId() + "的数据");
        }
        // 判断能否选择当前运营区
        if (!areaValidator.canCheckForJoin(areaJoin.getAreaId())) {
            return AjaxResult.error("您无权选择ID为" + areaJoin.getAreaId() + "的运营区");
        }
        AreaJoin po = areaJoinConverter.toPoByUpdate(areaJoin);
        return toAjax(areaJoinService.updateAreaJoin(po));
    }

    /**
     * 删除运营加盟
     */
    @PreAuthorize("@ss.hasPermi('bst:areaJoin:remove')")
    @Log(title = "删除运营加盟", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.AREA_JOIN)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        // 判断能否删除当前加盟数据
        if (!areaJoinValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您无权删除ID为" + ids + "的数据");
        }
        return toAjax(areaJoinService.deleteAreaJoinByIds(ids.toArray(new Long[0])));
    }
}
