package com.ruoyi.web.controller.fleet.orderPlan;

import java.util.List;
import java.util.Map;

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.orderPlan.domain.Ftmsorderbills;
import com.ruoyi.orderPlan.domain.Ftmsorderbillsplans;
import com.ruoyi.orderPlan.service.IftmsorderbillsService;
import com.ruoyi.orderPlan.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;

    @Autowired
    private IftmsorderbillsService ftmsorderbillsService;

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

    /**
     * 查询车队派车列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:query')")
    @GetMapping("/query")
    public TableDataInfo query(Ftmsorderbills ftmsorderbills) {
        startPage();
        List<Map<String, Object>> list = ftmsorderbillsplansService.selectftmsorderbillsplansMapList(ftmsorderbills);
        return getDataTable(list);
    }

    /**
     * 查询车队派车列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillscars:query')")
    @GetMapping("/queryForStowage")
    public TableDataInfo queryForStowage(Ftmsorderbills ftmsorderbills) {
        startPage();
        List<Map<String, Object>> list = ftmsorderbillsplansService.selectftmsorderbillsplansMapListForStowage(ftmsorderbills);
        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");
    }

    /**
     * 列表通过id查看调度内容
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:query')")
    @GetMapping(value = "/scheduling/{id}")
    @RepeatSubmit
    public AjaxResult getScheduling(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("缺少查询条件，请确认");
        }
        Long billType = 1010L;
        return AjaxResult.success(ftmsorderbillsService.selectftmsorderbillsById(id, billType));
    }

    /**
     * 获取订单调度详细信息
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:query')")
    @GetMapping(value = "/{id}")
    @RepeatSubmit
    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 = "/schedulingChange")
    @RepeatSubmit
    public AjaxResult schedulingChange(@RequestBody Ftmsorderbillsplans plans) {
        if (StringUtils.isNull(plans) || StringUtils.isNull(plans.getId())) {
            return AjaxResult.error("未找到调度安排数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.schedulingChange(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("attachs") String attachs,
                           @RequestParam("fees") String fees,
                           @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, attachs, fees, plans, loginUser);
    }

    /**
     * 调度单条变更提交
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:edit')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/planChange")
    @RepeatSubmit
    public AjaxResult planChange(@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.insertSchedulingChange(tmsorder, plans, loginUser);
    }

    /**
     * 订单调度变更
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:add')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @GetMapping(value = "/change/{id}")
    @RepeatSubmit
    public AjaxResult add(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id) || id == 0) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsplansService.schedulingChanges(id, loginUser);
    }

    /**
     * 提交订单调度/变更提交
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbillsplans:add')")
    @Log(title = "订单调度", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tmsorder") String tmsorder,
                          @RequestParam("scntrs") String scntrs,
                          @RequestParam("attachs") String attachs,
                          @RequestParam("fees") String fees,
                          @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, attachs, fees, 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}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ftmsorderbillsplansService.deleteftmsorderbillsplansByIds(ids));
    }
}
