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.model.domain.ModelQuery;
import com.ruoyi.bst.model.domain.ModelVO;
import com.ruoyi.bst.model.service.ModelAssembler;
import com.ruoyi.bst.model.service.ModelService;
import com.ruoyi.bst.model.service.ModelValidator;
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-18
 */
@RestController
@RequestMapping("/bst/model")
public class ModelController extends BaseController
{

    @Autowired
    private ModelService modelService;

    @Autowired
    private UserValidator userValidator;

    @Autowired
    private ModelAssembler modelAssembler;

    @Autowired
    private ModelValidator modelValidator;

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

    /**
     * 查询车辆型号列表
     */
    @PreAuthorize("@ss.hasPermi('bst:model:list')")
    @PostMapping("/listByIds")
    public AjaxResult listByIds(@RequestBody List<Long> ids) {
        List<ModelVO> list = modelService.selectModelListByIds(ids, true);
        return success(list);
    }

    /**
     * 导出车辆型号列表
     */
    @PreAuthorize("@ss.hasPermi('bst:model:export')")
    @Log(title = "车辆型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelQuery query)
    {
        query.setScope(true);
        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 = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ModelVO model = modelService.selectModelById(id, true);
        List<ModelVO> list = Collections.singletonList(model);
        modelAssembler.assembleSuitIds(list);
        return success(model);
    }

    /**
     * 新增车辆型号
     */
    @PreAuthorize("@ss.hasPermi('bst:model:add')")
    @Log(title = "新增车辆型号", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.MODEL)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) ModelVO data)
    {
        // 若不能操作他人，则设置为当前用户
        if (!userValidator.canView(data.getUserId())) {
            data.setUserId(getUserId());
        }
        return toAjax(modelService.insertModel(data));
    }

    /**
     * 修改车辆型号
     */
    @PreAuthorize("@ss.hasPermi('bst:model:edit')")
    @Log(title = "修改车辆型号", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.MODEL)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) ModelVO data)
    {
        ServiceUtil.assertion(!modelValidator.canEdit(data.getId()),
        "当前用户无权限修改ID为%s的型号", data.getId());
        // 若不能操作他人，则不修改用户ID
        if (!userValidator.canView(data.getUserId())) {
            data.setUserId(null);
        }
        return toAjax(modelService.updateModel(data));
    }

    /**
     * 删除车辆型号
     */
    @PreAuthorize("@ss.hasPermi('bst:model:remove')")
    @Log(title = "删除车辆型号", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.MODEL)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        ServiceUtil.assertion(!modelValidator.canDeleteAll(ids),
        "当前用户无权限删除ID为%s的型号", ids);
        return toAjax(modelService.logicDel(ids));
    }
}
