package com.ruoyi.web.bst;

import java.util.Collections;
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.suit.domain.SuitQuery;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitAssembler;
import com.ruoyi.bst.suit.service.SuitService;
import com.ruoyi.bst.suit.service.SuitValidator;
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;
import com.ruoyi.system.user.service.UserValidator;

/**
 * 套餐Controller
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/bst/suit")
public class SuitController extends BaseController
{
    @Autowired
    private SuitService suitService;

    @Autowired
    private SuitValidator suitValidator;

    @Autowired
    private UserValidator userValidator;

    @Autowired
    private SuitAssembler suitAssembler;

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuitQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        suitAssembler.assembleModelIds(list);
        return getDataTable(list);
    }

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:list')")
    @PostMapping("/listByIds")
    public AjaxResult listByIds(@RequestBody List<Long> ids)
    {
        SuitQuery query = new SuitQuery();
        query.setIds(ids);
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        return success(list);
    }

    /**
     * 导出套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:export')")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuitQuery query)
    {
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        ExcelUtil<SuitVO> util = new ExcelUtil<SuitVO>(SuitVO.class);
        util.exportExcel(response, list, "套餐数据");
    }

    /**
     * 获取套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SuitVO suit = suitService.selectSuitById(id, true);
        suitAssembler.assembleModelIds(Collections.singletonList(suit));
        return success(suit);
    }

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:add')")
    @Log(title = "新增套餐", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.SUIT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) SuitVO suit)
    {
        // 判断能否操作他人的套餐，不能则设置为当前用户
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(getUserId());
        }
        return toAjax(suitService.insertSuit(suit));
    }

    /**
     * 修改套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:edit')")
    @Log(title = "修改套餐", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.SUIT)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) SuitVO suit) {

        ServiceUtil.assertion(!suitValidator.canEdit(suit.getId()),
        "您无权修改ID为%s的套餐", suit.getId());
        // 若不能操作他人的套餐，则不修改用户ID
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(null);
        }
        return toAjax(suitService.updateSuit(suit));
    }

    /**
     * 删除套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:remove')")
    @Log(title = "删除套餐", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.SUIT)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(suitService.deleteSuitByIds(ids));
    }
}
