package com.dcsoft.system.vehicle.controller;

import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.vehicle.domain.CarPark;
import com.dcsoft.system.vehicle.domain.CarParkItem;
import com.dcsoft.system.vehicle.service.ICarParkItemService;
import com.dcsoft.system.vehicle.service.ICarParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 车场信息Controller
 *
 * @author nichun
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/carPark")
public class CarParkController extends BaseController
{
    @Autowired
    private ICarParkService carParkService;

    @Autowired
    private ICarParkItemService carParkItemService;

    /**
     * 查询车场信息列表
     */
    @RequiresPermissions("system:carPark:list")
    @GetMapping("/list")
    public TableDataInfo list(CarPark carPark)
    {
        startPage();
        List<CarPark> list = carParkService.selectCarParkList(carPark);
        return getDataTable(list);
    }

    /**
     * 查询车场信息详情
     */
    @RequiresPermissions("system:carPark:list")
    @GetMapping("/getCarParkSelect/{id}")
    public AjaxResult getCarParkSelect(@PathVariable("id") Long id)
    {
        List<CarPark> list = carParkService.getCarParkSelect(id);
        return AjaxResult.success(list);
    }

    /**
     * 导出车场信息列表
     */
    @RequiresPermissions("system:carPark:export")
    @Log(title = "车场信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarPark carPark)
    {
        List<CarPark> list = carParkService.selectCarParkList(carPark);
        ExcelUtil<CarPark> util = new ExcelUtil<CarPark>(CarPark.class);
        util.exportExcel(response, list, "车场信息数据");
    }

    /**
     * 获取车场信息详细信息
     */
    @RequiresPermissions("system:carPark:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CarPark park =carParkService.selectCarParkById(id);
        CarParkItem item=new CarParkItem();
        item.setParkId(id);
        List<CarParkItem> itemList=carParkItemService.selectCarParkItemList(item);
        park.setItemList(itemList);
        return success(park);
    }

    /**
     * 新增车场信息
     */
    @RequiresPermissions("system:carPark:add")
    @Log(title = "车场信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarPark carPark)
    {
        int i=carParkService.insertCarPark(carPark);
        List<CarParkItem> itemList=carPark.getItemList();
        for(CarParkItem item:itemList){
            item.setParkId(carPark.getId());
            carParkItemService.insertCarParkItem(item);
        }
        return toAjax(i);
    }

    /**
     * 修改车场信息
     */
    @RequiresPermissions("system:carPark:edit")
    @Log(title = "车场信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarPark carPark)
    {
        //先删除关联子项再进行增加
        //删除子项存在的注册人员信息
        carParkItemService.deleteParkItemByParkId(carPark.getId());
        List<CarParkItem> itemList=carPark.getItemList();
        for(CarParkItem item:itemList){
            item.setParkId(carPark.getId());
            carParkItemService.insertCarParkItem(item);
        }
        return toAjax(carParkService.updateCarPark(carPark));
    }

    /**
     * 删除车场信息
     */
    @RequiresPermissions("system:carPark:remove")
    @Log(title = "车场信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            carParkItemService.deleteParkItemByParkId(id);
        }
        return toAjax(carParkService.deleteCarParkByIds(ids));
    }
}
