package com.ruoyi.web.controller.ss;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.suit.domain.SuitBO;
import com.ruoyi.ss.suit.domain.SuitQuery;
import com.ruoyi.ss.suit.domain.SuitVO;
import com.ruoyi.ss.suit.service.SuitAssembler;
import com.ruoyi.ss.suit.service.SuitValidator;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.suit.service.SuitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 套餐Controller
 *
 * @author ruoyi
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/ss/suit")
public class SuitController extends BaseController
{
    @Autowired
    private SuitService suitService;

    @Autowired
    private SuitValidator suitValidator;

    @Autowired
    private SuitAssembler suitAssembler;

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

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

    /**
     * 获取套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('ss:suit:query')")
    @GetMapping(value = "/{suitId}")
    public AjaxResult getInfo(@PathVariable("suitId") Long suitId)
    {
        SuitVO suit = suitService.selectSuitBySuitId(suitId);
        List<SuitVO> list = Collections.singletonList(suit);
        suitAssembler.assembleDeviceList(list);
        return success(suit);
    }

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('ss:suit:add')")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) SuitBO suit)
    {
        ServiceUtil.assertion(suitValidator.preCreateOrUpdate(suit));
        return toAjax(suitService.insertSuit(suit));
    }

    /**
     * 修改套餐
     */
    @PreAuthorize("@ss.hasPermi('ss:suit:edit')")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) SuitBO suit)
    {
        ServiceUtil.assertion(suitValidator.preCreateOrUpdate(suit));
        return toAjax(suitService.updateSuit(suit));
    }

    /**
     * 逻辑删除套餐
     */
    @PreAuthorize("@ss.hasPermi('ss:suit:remove')")
    @Log(title = "套餐", businessType = BusinessType.LOGIC_DELETE)
	@DeleteMapping("/{suitIds}")
    public AjaxResult remove(@PathVariable Long[] suitIds)
    {
        List<Long> ids = Arrays.asList(suitIds);
        ServiceUtil.assertion(suitValidator.preLogicDel(ids));
        return toAjax(suitService.logicDel(ids) >= 1);
    }
}
