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.CarCharge;
import com.dcsoft.system.vehicle.domain.CarChargeItem;
import com.dcsoft.system.vehicle.service.ICarChargeItemService;
import com.dcsoft.system.vehicle.service.ICarChargeService;
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 2024-01-19
 */
@RestController
@RequestMapping("/carCharge")
public class CarChargeController extends BaseController
{
    @Autowired
    private ICarChargeService carChargeService;

    @Autowired
    private ICarChargeItemService carChargeItemService;

    /**
     * 查询收费配置列表
     */
    @RequiresPermissions("system:carCharge:list")
    @GetMapping("/list")
    public TableDataInfo list(CarCharge carCharge)
    {
        startPage();
        List<CarCharge> list = carChargeService.selectCarChargeList(carCharge);
        return getDataTable(list);
    }

    /**
     * 导出收费配置列表
     */
    @RequiresPermissions("system:carCharge:export")
    @Log(title = "收费配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarCharge carCharge)
    {
        List<CarCharge> list = carChargeService.selectCarChargeList(carCharge);
        ExcelUtil<CarCharge> util = new ExcelUtil<CarCharge>(CarCharge.class);
        util.exportExcel(response, list, "收费配置数据");
    }

    /**
     * 获取收费配置详细信息
     */
    @RequiresPermissions("system:carCharge:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CarCharge carCharge =carChargeService.selectCarChargeById(id);
        CarChargeItem item=new CarChargeItem();
        item.setChargeId(id);
        List<CarChargeItem> itemList=carChargeItemService.selectCarChargeItemList(item);
        carCharge.setItemList(itemList);
        return success(carCharge);
    }

    /**
     * 新增收费配置
     */
    @RequiresPermissions("system:carCharge:add")
    @Log(title = "收费配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarCharge carCharge)
    {
        int i=carChargeService.insertCarCharge(carCharge);
        List<CarChargeItem> itemList=carCharge.getItemList();
        for(CarChargeItem item:itemList){
            item.setChargeId(carCharge.getId());
            carChargeItemService.insertCarChargeItem(item);
        }
        return toAjax(i);
    }

    /**
     * 修改收费配置
     */
    @RequiresPermissions("system:carCharge:edit")
    @Log(title = "收费配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarCharge carCharge)
    {
        carChargeItemService.deleteChargeItemByChargeId(carCharge.getId());
        List<CarChargeItem> itemList=carCharge.getItemList();
        for(CarChargeItem item:itemList){
            item.setChargeId(carCharge.getId());
            carChargeItemService.insertCarChargeItem(item);
        }
        return toAjax(carChargeService.updateCarCharge(carCharge));
    }

    /**
     * 删除收费配置
     */
    @RequiresPermissions("system:carCharge:remove")
    @Log(title = "收费配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            carChargeItemService.deleteChargeItemByChargeId(id);
        }
        return toAjax(carChargeService.deleteCarChargeByIds(ids));
    }
}
