package com.ruoyi.web.controller.ss;

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

import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.ss.model.domain.SmModelBO;
import com.ruoyi.ss.model.domain.SmModelQuery;
import com.ruoyi.ss.model.domain.SmModelVO;
import com.ruoyi.ss.model.service.ModelValidator;
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.model.service.ModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

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

    @Autowired
    private ModelValidator modelValidator;

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

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

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

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

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

    /**
     * 删除型号列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "型号列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(smModelService.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(smModelService.logicDelete(Arrays.asList(modelIds)));
    }


}
