package com.muyu.enterprise.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.enterprise.domain.CarManage;
import com.muyu.enterprise.domain.dto.CarDTO;
import com.muyu.enterprise.domain.vo.CarVO;
import com.muyu.enterprise.service.CarCompanyService;
import com.muyu.enterprise.service.CarManageService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆管理--控制层
 * @ClassName CarManageController
 * @Description 车辆管理 Controller 层
 * @author  MingWei.Zong
 * @Date 2024/9/28 16:52
 */
@RestController
@RequestMapping("/carManage")
@Tag(name = "CarManageController", description = "车辆管理")
public class CarManageController extends BaseController {

    @Autowired
    private CarCompanyService sysCarCompanyService;
    @Autowired
    private CarManageService sysCarService;


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

    /**
     * 通过id查询
     * @param carId
     * @return
     */
    @PostMapping("CarManageShowByCarId")
    public Result CarManageShowByCarId(@RequestParam("carId") Long carId){
        return Result.success(sysCarService.getById(carId));
    }

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


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


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

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


}
