package com.muyu.car.controller;

import cn.hutool.core.util.RandomUtil;
import com.muyu.car.domain.dto.CarDTO;
import com.muyu.car.domain.vo.CarVO;
import com.muyu.car.service.SysCarCompanyService;
import com.muyu.car.service.SysCarService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author muyu
 */
@RestController
@RequestMapping("/manage")
@Tag(name = "SysCarController", description = "系统参数配置")
public class SysCarController extends BaseController {

    @Autowired
    private SysCarCompanyService sysCarCompanyService;
    @Autowired
    private SysCarService sysCarService;

    /**
     * 车辆列表2
     * @param carDTO
     * @return
     */
    @PostMapping("CarListShow")
    public Result<List<CarVO>> CarListShow(@RequestBody CarDTO carDTO){
        return Result.success(sysCarService.CarListShow(carDTO));
    }

    /**
     * 通过id查询
     * @param carId
     * @return
     */
    @PostMapping("CarListShowByCarId")
    public Result<List<CarVO>> CarListShowByCarId(@RequestParam("carId") Long carId){
        return Result.success(sysCarService.CarListShowByCarId(carId));
    }

    /**
     * 车辆列表1
     * @param carVO
     * @return
     */
    @PostMapping("CarList")
    @Operation(summary = "查询列表",description = "车辆管理列表")
    public Result<TableDataInfo<CarVO>> CarList(@RequestBody CarVO carVO){
        startPage();
        return getDataTable(sysCarService.carList(carVO));
    }


    /**
     * 车辆添加
     * @param carVO
     * @return
     */
    @PostMapping("insertCar")
    public Result insertCar(@RequestBody CarVO carVO){
        String key = RandomUtil.randomNumbers(9);
        // 随机生成车架号
        carVO.setCarFrame(key);
        // 随机生成VIN码
        String key2 = RandomUtil.randomNumbers(17);
        carVO.setCarVin(key2);
        //
        carVO.setUserId(SecurityUtils.getUserId());
        sysCarService.insertCar(carVO);
        return Result.success("车辆添加成功");
    }


    /**
     * 车辆修改
     * @param carVO
     * @return
     */
    @PostMapping("updataCar")
    public Result updataCar(@RequestBody CarVO carVO){
        sysCarService.updateById(carVO);
        return Result.success("车辆修改成功");
    }

    /**
     * 车辆删除
     * @param ids
     * @return
     */
    @PostMapping("deleteCar")
    public Result deleteCar(@RequestParam("ids") List<Long> ids){
        sysCarService.deleteCar(ids);
        return Result.success("车辆删除成功");
    }


}
