package com.muyu.car.controller;

import java.util.Arrays;
import java.util.List;

import com.muyu.car.domain.SysMessageType;
import com.muyu.car.service.ISysMessageTypeService;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
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.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 车辆报文类型Controller
 *
 * @author muyu
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/messageType")
public class SysMessageTypeController extends BaseController
{
    @Resource
    private ISysMessageTypeService sysMessageTypeService;

    /**
     * 查询车辆报文类型列表
     */
    @RequiresPermissions("message:messageType:list")
    @GetMapping("/list")
    public Result<List<SysMessageType>> list(SysMessageType sysMessageType)
    {
        List<SysMessageType> list = sysMessageTypeService.selectSysMessageTypeList(sysMessageType);
        return Result.success(list);
    }

    /**
     * 导出车辆报文类型列表
     */
    @RequiresPermissions("message:messageType:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMessageType sysMessageType)
    {
        List<SysMessageType> list = sysMessageTypeService.selectSysMessageTypeList(sysMessageType);
        ExcelUtil<SysMessageType> util = new ExcelUtil<SysMessageType>(SysMessageType.class);
        util.exportExcel(response, list, "车辆报文类型数据");
    }

    /**
     * 获取车辆报文类型详细信息
     */
    @RequiresPermissions("message:messageType:query")
    @GetMapping(value = "/{messageCode}")
    public Result<List<SysMessageType>> getInfo(@PathVariable("messageCode") String[] messageCodes)
    {
        SysMessageType[] list = new SysMessageType[messageCodes.length]; // 确保数组大小
        for (int i = 0; i < messageCodes.length; i++) {
            list[i] = sysMessageTypeService.selectSysMessageTypeByMessageCode(messageCodes[i]);
        }
        return success(list);
    }

    /**
     * 新增车辆报文类型
     */
    @RequiresPermissions("message:messageType:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysMessageType sysMessageType)
    {
        if (sysMessageTypeService.checkIdUnique(sysMessageType)) {
            return error("新增 车辆报文类型 '" + sysMessageType + "'失败，车辆报文类型已存在");
        }

        return toAjax(sysMessageTypeService.save(sysMessageType));
    }

    /**
     * 修改车辆报文类型
     */
    @RequiresPermissions("message:messageType:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysMessageType sysMessageType)
    {
        if (!sysMessageTypeService.checkIdUnique(sysMessageType)) {
            return error("修改 车辆报文类型 '" + sysMessageType + "'失败，车辆报文类型不存在");
        }

        return toAjax(sysMessageTypeService.updateById(sysMessageType));
    }

    /**
     * 删除车辆报文类型
     */
    @RequiresPermissions("message:messageType:remove")
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysMessageTypeService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
