package com.ruoyi.web.bst;

import com.ruoyi.bst.model.domain.Model;
import com.ruoyi.bst.model.domain.ModelQuery;
import com.ruoyi.bst.model.domain.ModelVO;
import com.ruoyi.bst.model.service.ModelConverter;
import com.ruoyi.bst.model.service.ModelService;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
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.utils.poi.ExcelUtil;
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.List;

/**
 * 型号列表Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/bst/model")
public class ModelController extends BaseController
{
    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelConverter modelConverter;

    /**
     * 查询型号列表列表
     */
    @PreAuthorize("@ss.hasPermi('bst:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelQuery query)
    {
        startPage();
        startOrderBy();
        query.setDeleted(false);
        List<ModelVO> list = modelService.selectModelList(query);
        return getDataTable(list);
    }

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

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

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

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

    /**
     * 删除型号列表
     */
    @PreAuthorize("@ss.hasPermi('bst:model:remove')")
    @Log(title = "型号列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(modelService.logicDel(ids));
    }
}
