package cn.iocoder.yudao.module.jx.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.core.controller.BaseController;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCar;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolCarService;
import cn.iocoder.yudao.module.jx.utils.ExcelUtil;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆信息Controller
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/drivingSchool/system/car")
public class DriveSchoolCarController extends BaseController
{
    @Autowired
    private IDriveSchoolCarService driveSchoolCarService;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询车辆信息列表
     */
    @GetMapping("/list")
    public CommonResult<IPage<?>> list(DriveSchoolCar driveSchoolCar)
    {
        driveSchoolCar.setPageNum(1);
        driveSchoolCar.setPageSize(10);
        Page<DriveSchoolCar> page = new Page<>(driveSchoolCar.getPageNum(), driveSchoolCar.getPageSize());
        IPage<DriveSchoolCar> driveSchoolCarIPage = driveSchoolCarService.selectDriveSchoolCarList(driveSchoolCar, page);
        return CommonResult.success(driveSchoolCarIPage);
        //return getDataTable(list);
    }

    /**
     * 查询车辆信息列表
     */
    @GetMapping("/listAll")
    public CommonResult listAll(DriveSchoolCar driveSchoolCar)
    {
        List<DriveSchoolCar> list = driveSchoolCarService.selectDriveSchoolCarListAll(driveSchoolCar);
        return CommonResult.success(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, DriveSchoolCar driveSchoolCar)
    {
        List<DriveSchoolCar> list = driveSchoolCarService.selectDriveSchoolCarListAll(driveSchoolCar);
        ExcelUtil<DriveSchoolCar> util = new ExcelUtil<DriveSchoolCar>(DriveSchoolCar.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(driveSchoolCarService.selectDriveSchoolCarById(id));
    }

    /**
     * 新增车辆信息
     */
    @PostMapping
    public CommonResult add(@RequestBody DriveSchoolCar driveSchoolCar)
    {
        return toAjax(driveSchoolCarService.insertDriveSchoolCar(driveSchoolCar));
    }

    /**
     * 修改车辆信息
     */
    @PutMapping
    public CommonResult edit(@RequestBody DriveSchoolCar driveSchoolCar)
    {
        return toAjax(driveSchoolCarService.updateDriveSchoolCar(driveSchoolCar));
    }

    /**
     * 删除车辆信息
     */
	@DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toAjax(driveSchoolCarService.deleteDriveSchoolCarByIds(ids));
    }

    @PostMapping("/importData")
    public CommonResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<DriveSchoolCar> util = new ExcelUtil<DriveSchoolCar>(DriveSchoolCar.class);
        List<DriveSchoolCar> carsList = util.importExcel(file.getInputStream());
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        String operName = user.getNickname();
        String message = driveSchoolCarService.importCars(carsList, updateSupport, operName);
        return success(message);
    }
}
