package com.muyu.fault.controller;

import cn.hutool.core.date.DateTime;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.fault.common.FaultLog;
import com.muyu.fault.common.SysCarFault;
import com.muyu.fault.service.impl.FaultLogService;
import com.muyu.fault.service.impl.SysCarFaultService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/faultInfo")
public class SysCarFaultController extends BaseController
{
    @Resource
    private SysCarFaultService sysCarFaultService;
    @Autowired
    private FaultLogService faultLogService;

    /**
     * 查询车辆故障列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<SysCarFault>> list(SysCarFault sysCarFault)
    {
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        return success(list);
    }

    /**
     * 导出车辆故障列表
     */
    @RequestMapping(value = "export",method = RequestMethod.POST)
    public void export(HttpServletResponse response, SysCarFault sysCarFault)
    {
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        ExcelUtil<SysCarFault> util = new ExcelUtil<SysCarFault>(SysCarFault.class);
        util.exportExcel(response, list, "车辆故障数据");
    }

    /**
     * 获取车辆故障详细信息
     */
    @RequestMapping(value = "/getInfo/{id}",method = RequestMethod.GET)
    public Result<List<SysCarFault>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarFaultService.selectSysCarFaultById(id));
    }

    /**
     * 新增车辆故障
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result<Integer> add(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        String prefix = "GT"; // 随机码的前缀
        int minNumber = 1; // 随机数字的最小值
        int maxNumber = 999; // 随机数字的最大值
        Random random = new Random(); // 创建一个随机对象
        // 生成一个随机数字
        int number = random.nextInt(maxNumber - minNumber + 1) + minNumber;
        // 将数字格式化为三位字符串，不足三位前面补0
        String formattedNumber = String.format("%03d", number);
        // 生成一个随机字母
        String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String randomLetter = alphabets.charAt(random.nextInt(alphabets.length())) + "";
        // 拼接前缀、随机字母和随机数字
        String randomCode = prefix + randomLetter + formattedNumber;
        System.out.println(randomCode); // 输出随机码
        //赋值
        sysCarFault.setFaultCode(randomCode);
        sysCarFault.setCreateBy(SecurityUtils.getUsername());
        FaultLog faultLog = new FaultLog();
        faultLog.setFaultLogCodes(sysCarFault.getFaultCode());
        faultLog.setFaultLogVin(sysCarFault.getCarVin());
        DateTime startTime = DateTime.now(); // 获取当前时间作为开始时间
        faultLog.setFaultLogStartTime(startTime);
        faultLogService.save(faultLog);
        if (sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("新增 车辆故障 '" + sysCarFault + "'失败，车辆故障已存在");
        }
        return toAjax(sysCarFaultService.save(sysCarFault));
    }

    /**
     * 修改车辆故障
     */
    @RequestMapping(value = "edit",method = RequestMethod.PUT)
    public Result<Integer> edit(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        if (!sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("修改 车辆故障 '" + sysCarFault + "'失败，车辆故障不存在");
        }
        sysCarFault.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.updateById(sysCarFault));
    }

    /**
     * 删除车辆故障
     */
    @RequestMapping(value = "/remove/{ids}",method = RequestMethod.DELETE)
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysCarFaultService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
