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.Ftmsorderbillsplans;
import com.ruoyi.orderManagement.service.IftmsorderbillsplansService;
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/ftmsorderbillsplans")
public class ftmsorderbillsplansController extends BaseController {
    @Autowired
    private IftmsorderbillsplansService ftmsorderbillsplansService;

    /**
     * 查询订单调度列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Ftmsorderbillsplans ftmsorderbillsplans) {
        startPage();
        List<Ftmsorderbillsplans> list = ftmsorderbillsplansService.selectftmsorderbillsplansList(ftmsorderbillsplans);
        return getDataTable(list);
    }

    /**
     * 导出订单调度列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:export')")
    @Log(title = "订单调度", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Ftmsorderbillsplans ftmsorderbillsplans) {
        List<Ftmsorderbillsplans> list = ftmsorderbillsplansService.selectftmsorderbillsplansList(ftmsorderbillsplans);
        ExcelUtil<Ftmsorderbillsplans> util = new ExcelUtil<Ftmsorderbillsplans>(Ftmsorderbillsplans.class);
        return util.exportExcel(list, "ftmsorderbillsplans");
    }

    /**
     * 获取订单调度详细信息
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("缺少查询条件，请确认");
        }
        return ftmsorderbillsplansService.selectftmsorderbillsplansById(id);
    }

    /**
     * 单条订单调度提交
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:add')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping(value = "/schedulingSave")
    @RepeatSubmit
    public AjaxResult schedulingSave(@RequestParam("tmsorder") String tmsorder,
                                     @RequestParam("plans") String plans) {
        if (StringUtils.isNull(tmsorder) || "{}".equals(tmsorder)) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        if (StringUtils.isNull(plans) || "{}".equals(plans)) {
            return AjaxResult.error("未找到调度安排数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.schedulingSave(tmsorder, plans, loginUser);
    }

    /**
     * 单条订单调度撤销
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:edit')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping(value = "/schedulingRevoke")
    @RepeatSubmit
    public AjaxResult schedulingRevoke(@RequestBody Ftmsorderbillsplans plans) {
        if (StringUtils.isNull(plans) || StringUtils.isNull(plans.getId())) {
            return AjaxResult.error("未找到调度安排数据，请确认");
        }
        String type = "Revoke";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.schedulingRevoke(plans, type, loginUser);
    }

    /**
     * 单条订单调度删除
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:remove')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping(value = "/schedulingRemove")
    @RepeatSubmit
    public AjaxResult schedulingRemove(@RequestBody Ftmsorderbillsplans plans) {
        if (StringUtils.isNull(plans) || StringUtils.isNull(plans.getId())) {
            return AjaxResult.error("未找到调度安排数据，请确认");
        }
        String type = "Remove";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.schedulingRevoke(plans, type, loginUser);
    }

    /**
     * 保存订单调度
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:add')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    @RepeatSubmit
    public AjaxResult save(@RequestParam("tmsorder") String tmsorder,
                           @RequestParam("scntrs") String scntrs,
                           @RequestParam("attachs") String attachs,
                           @RequestParam("plans") String plans) {
        if (StringUtils.isNull(tmsorder) || "{}".equals(tmsorder)) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.saveFtmsorderbills(tmsorder, scntrs, attachs, plans, loginUser);
    }

    /**
     * 提交订单调度
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:add')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tmsorder") String tmsorder,
                          @RequestParam("scntrs") String scntrs,
                          @RequestParam("attachs") String attachs,
                          @RequestParam("plans") String plans) {
        if (StringUtils.isNull(tmsorder) || "{}".equals(tmsorder)) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        if (StringUtils.isNull(scntrs) || "[]".equals(scntrs)) {
            return AjaxResult.error("未找到计划箱量数据，请确认");
        }
        if (StringUtils.isNull(plans) || "{}".equals(plans)) {
            return AjaxResult.error("未找到调度数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.insertftmsorderbills(tmsorder, scntrs, attachs, plans, loginUser);
    }

    /**
     * 修改订单调度
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:edit')")
    @Log(title = "订单调度", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Ftmsorderbillsplans ftmsorderbillsplans) {
        return toAjax(ftmsorderbillsplansService.updateftmsorderbillsplans(ftmsorderbillsplans));
    }

    /**
     * 删除订单调度
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:remove')")
    @Log(title = "订单调度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ftmsorderbillsplansService.deleteftmsorderbillsplansByIds(ids));
    }
}
