package com.ruoyi.web.controller.system;

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.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.model.domain.ModelBO;
import com.ruoyi.ss.model.domain.ModelQuery;
import com.ruoyi.ss.model.domain.ModelVO;
import com.ruoyi.ss.model.service.IModelService;
import com.ruoyi.ss.model.service.IModelValidator;
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.*;

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

/**
 * 型号列表Controller
 *
 * @author 邱贞招
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/system/model")
public class ModelController extends BaseController
{
    @Autowired
    private IModelService modelService;

    @Autowired
    private IModelValidator modelValidator;

    /**
     * 查询型号列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelQuery smModel)
    {
        startPage();
        return getDataTable(modelService.selectSmModelList(smModel));
    }

    /**
     * 导出型号列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:export')")
    @Log(title = "型号列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelQuery smModel)
    {
        List<ModelVO> list = modelService.selectSmModelList(smModel);
        ExcelUtil<ModelVO> util = new ExcelUtil<>(ModelVO.class);
        util.exportExcel(response, list, "型号列表数据");
    }

    /**
     * 获取型号列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:model:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId)
    {
        return success(modelService.selectSmModelByModelId(modelId));
    }

    /**
     * 新增型号列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:add')")
    @Log(title = "型号列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) ModelBO smModel)
    {
        smModel = smModel.filterInsert();
        return toAjax(modelService.insertSmModel(smModel));
    }

    /**
     * 修改型号列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:edit')")
    @Log(title = "型号列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) ModelBO smModel)
    {
        smModel = smModel.filterUpdate();
        return toAjax(modelService.updateSmModel(smModel));
    }

    /**
     * 删除型号列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "型号列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(modelService.deleteSmModelByModelIds(modelIds));
    }

    /**
     * 逻辑删除
     * @param modelIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "型号列表", businessType = BusinessType.LOGIC_DELETE)
    @DeleteMapping("/logic/{modelIds}")
    public AjaxResult logicDel(@PathVariable Long[] modelIds)
    {
        return toAjax(modelService.logicDelete(Arrays.asList(modelIds)));
    }
}
