package com.ruoyi.web.controller.fleet;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.orderManagement.domain.Ftmsorderbillscars;
import com.ruoyi.orderManagement.service.IftmsorderbillscarsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车队派车Controller
 *
 * @author ruoyi
 * @date 2021-02-22
 */
@RestController
@RequestMapping("/fleet/ftmsorderbillscars")
public class ftmsorderbillscarsController extends BaseController {
    @Autowired
    private IftmsorderbillscarsService ftmsorderbillscarsService;

    /**
     * 查询车队派车列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Ftmsorderbillscars ftmsorderbillscars) {
        startPage();
        List<Ftmsorderbillscars> list = ftmsorderbillscarsService.selectftmsorderbillscarsList(ftmsorderbillscars);
        return getDataTable(list);
    }

    /**
     * 导出车队派车列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:export')")
    @Log(title = "车队派车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Ftmsorderbillscars ftmsorderbillscars) {
        List<Ftmsorderbillscars> list = ftmsorderbillscarsService.selectftmsorderbillscarsList(ftmsorderbillscars);
        ExcelUtil<Ftmsorderbillscars> util = new ExcelUtil<Ftmsorderbillscars>(Ftmsorderbillscars.class);
        return util.exportExcel(list, "ftmsorderbillscars");
    }

    /**
     * 根据调度安排id查询车队派车详细信息
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("未找到查询条件，请确认");
        }
        return AjaxResult.success(ftmsorderbillscarsService.selectftmsorderbillscarsById(id));
    }


    /**
     * 单条车辆安排提交接口
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:add')")
    @Log(title = "车队派车", businessType = BusinessType.INSERT)
    @PostMapping(value = "/vehicleArrangementAdd")
    @RepeatSubmit
    public AjaxResult vehicleArrangementAdd(@RequestBody Ftmsorderbillscars ftmsorderbillscars) {
        if (StringUtils.isNull(ftmsorderbillscars.getId()) || StringUtils.isNull(ftmsorderbillscars.getPId())) {
            return AjaxResult.error("请先保存后再提交");
        }
        String status = "add";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillscarsService.vehicleArrangement(status, ftmsorderbillscars, loginUser);
    }

    /**
     * 单条车辆安排撤销接口
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:add')")
    @PostMapping(value = "/vehicleArrangementRevoke")
    @RepeatSubmit
    public AjaxResult vehicleArrangementRevoke(@RequestBody Ftmsorderbillscars ftmsorderbillscars) {
        if (StringUtils.isNull(ftmsorderbillscars.getId()) || StringUtils.isNull(ftmsorderbillscars.getPId())) {
            return AjaxResult.error("请先保存后再提交");
        }
        String status = "Revoke";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillscarsService.vehicleArrangement(status, ftmsorderbillscars, loginUser);
    }

    /**
     * 单条车辆安排删除接口
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:add')")
    @PostMapping(value = "/vehicleArrangementRemove")
    @RepeatSubmit
    public AjaxResult vehicleArrangementRemove(@RequestBody Ftmsorderbillscars ftmsorderbillscars) {
        if (StringUtils.isNull(ftmsorderbillscars.getId()) || StringUtils.isNull(ftmsorderbillscars.getPId())) {
            return AjaxResult.error("请先保存后再提交");
        }
        String status = "Remove";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillscarsService.vehicleArrangement(status, ftmsorderbillscars, loginUser);
    }

    /**
     * 暂存车队派车
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:add')")
    @Log(title = "车队派车", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    @RepeatSubmit
    public AjaxResult save(@RequestParam("tmsorder") String tmsorder,
                           @RequestParam("attachs") String attachs,
                           @RequestParam("fees") String fees,
                           @RequestParam("cars") String cars) {
        if (StringUtils.isNull(tmsorder) || "{}".equals(tmsorder)) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillscarsService.saveftmsorderbillscars(tmsorder, attachs, fees, cars, loginUser);
    }

    /**
     * 提交车队派车
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:add')")
    @Log(title = "车队派车", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tmsorder") String tmsorder,
                          @RequestParam("attachs") String attachs,
                          @RequestParam("fees") String fees,
                          @RequestParam("cars") String cars) {
        if (StringUtils.isNull(tmsorder) || "{}".equals(tmsorder)) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        if (StringUtils.isNull(cars) || "[]".equals(cars)) {
            return AjaxResult.error("未找到车辆安排数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillscarsService.insertftmsorderbillscars(tmsorder, attachs, fees, cars, loginUser);
    }

    /**
     * 修改车队派车
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:edit')")
    @Log(title = "车队派车", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Ftmsorderbillscars ftmsorderbillscars) {
        return toAjax(ftmsorderbillscarsService.updateftmsorderbillscars(ftmsorderbillscars));
    }

    /**
     * 删除车队派车
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:remove')")
    @Log(title = "车队派车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ftmsorderbillscarsService.deleteftmsorderbillscarsByIds(ids));
    }
}
