package com.muyu.car.controller;

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

import com.muyu.car.domain.SysCarMessage;
import com.muyu.car.domain.SysMessageType;
import com.muyu.car.domain.VO.SysMessageVO;
import com.muyu.car.service.ISysCarMessageService;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
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("/messageInfo")
public class SysCarMessageController extends BaseController
{
    @Resource
    private ISysCarMessageService sysCarMessageService;
    @Autowired
    private HttpSession session;

    static String TEST = "7E 56 45 48 49 43 4C 45 5F 4D 53 47 3A 56 49 4E 31 32 B6 A1 C0 F2 B3 E5 D9 A8 C1 B2 E3 F4 A6 D7 C9 F1 E0 A3 B5 C8 D4 E2 A1 F5 B6 C7 E8 D9 A0 E3 B2 C4 F5 D6 A8 C0 E1 F2 B7 D8 A4 E3 C2 B1 A5 D9 F6 C8 E7 A0 B2 C3 D1 F4 E5 A9 3F 2A 7B D9 1E C8 4D A3 6F 5B 9A 0C 3E 7D F2 8B 46 1A 5E 9F 2D 73 8C 4A B1 6C 5D E2 7E C4 39 0B AD 7C 1F 0E 3C 68 92 B4 5A 7F 6E 81 0D 4B A5 E3 F9 2E 8A 37 6D 14 5C 73 8E D2 04 9B 3A 6C F1 70 BF 29 5F 8C 43 61 24 5D 7A 9C 0A D5 1B 3D 6E F4 78 3E 5B";

    @RequiresPermissions("message:message:test")
    @GetMapping("/test")
    public Result test()
    {
        List<SysCarMessage> list = (List<SysCarMessage>) session.getAttribute("list");
        String[] test = TEST.split(" ");
        String[] results = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            SysCarMessage carMessage = list.get(i);
            int startIndex = Integer.parseInt(carMessage.getMessageStartIndex()) - 1;
            int endIndex = Integer.parseInt(carMessage.getMessageEndIndex());
            // 构建16进制字符串
            StringBuilder hexBuilder = new StringBuilder(endIndex - startIndex);
            for (int j = startIndex; j < endIndex; j++) {
                hexBuilder.append(test[j]);
            }
            // 转换为字符串
            String hex = hexBuilder.toString();
            int len = hex.length();
            char[] result = new char[len / 2];
            for (int x = 0; x < len; x += 2) {
                int high = Character.digit(hex.charAt(x), 16);
                int low = Character.digit(hex.charAt(x + 1), 16);
                result[x / 2] = (char) ((high << 4) + low);
            }
            results[i] = new String(result);
        }
        return Result.success(results);
    }

    /**
     * 查询车辆报文记录列表
     */
    @RequiresPermissions("message:message:list")
    @GetMapping("/list")
    public Result<List<SysCarMessage>> list(SysCarMessage sysCarMessage)
    {
        List<SysCarMessage> list = sysCarMessageService.selectSysCarMessageList(sysCarMessage);
        session.setAttribute("list", list);
        return Result.success(list);
    }

    @RequiresPermissions("message:message:dobList")
    @GetMapping("/dobList")
    public Result<List<SysMessageVO>> dobList(SysMessageVO sysMessageVO)
    {
        List<SysMessageVO> list = sysCarMessageService.dobList(sysMessageVO);
        return Result.success(list);
    }


    /**
     * 导出车辆报文记录列表
     */
    @RequiresPermissions("message:message:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCarMessage sysCarMessage)
    {
        List<SysCarMessage> list = sysCarMessageService.selectSysCarMessageList(sysCarMessage);
        ExcelUtil<SysCarMessage> util = new ExcelUtil<SysCarMessage>(SysCarMessage.class);
        util.exportExcel(response, list, "车辆报文记录数据");
    }

    /**
     * 获取车辆报文记录详细信息
     */
    @RequiresPermissions("message:message:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysCarMessage>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarMessageService.selectSysCarMessageById(id));
    }

    /**
     * 新增车辆报文记录
     */
    @RequiresPermissions("message:message:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysCarMessage sysCarMessage)
    {
        if (sysCarMessageService.checkIdUnique(sysCarMessage)) {
            return error("新增 车辆报文记录 '" + sysCarMessage + "'失败，车辆报文记录已存在");
        }
        return toAjax(sysCarMessageService.save(sysCarMessage));
    }

    /**
     * 修改车辆报文记录
     */
    @RequiresPermissions("message:message:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysCarMessage sysCarMessage)
    {
        if (!sysCarMessageService.checkIdUnique(sysCarMessage)) {
            return error("修改 车辆报文记录 '" + sysCarMessage + "'失败，车辆报文记录不存在");
        }
        return toAjax(sysCarMessageService.updateById(sysCarMessage));
    }

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