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

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

import com.ruoyi.common.annotation.DataScope;
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.service.IftmsorderbillsService;
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/ftmsorderbills")
public class ftmsorderbillsController extends BaseController {
    @Autowired
    private IftmsorderbillsService ftmsorderbillsService;

    /**
     * 查询车队首页信息
     */
    @GetMapping(value = "/queryHomeInfo")
    public AjaxResult queryHomeInfo() {
        return ftmsorderbillsService.queryHomeInfo();
    }

    /**
     * 查询订单列表显示字段
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:query')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(Ftmsorderbills ftmsorderbills) {
        startPage();
        List<Map<String, Object>> mapList = ftmsorderbillsService.selectftmsorderbillsMapList(ftmsorderbills);
        return getDataTable(mapList);
    }

    /**
     * 查询订单主列表
     */
    @GetMapping("/query")
    public TableDataInfo query(Ftmsorderbills ftmsorderbills) {
        startPage();
        List<Ftmsorderbills> list = ftmsorderbillsService.selectftmsorderbillsList(ftmsorderbills);
        return getDataTable(list);
    }

    /**
     * 导出订单主列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:export')")
    @Log(title = "订单主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Ftmsorderbills ftmsorderbills) {
        List<Ftmsorderbills> list = ftmsorderbillsService.selectftmsorderbillsList(ftmsorderbills);
        ExcelUtil<Ftmsorderbills> util = new ExcelUtil<Ftmsorderbills>(Ftmsorderbills.class);
        return util.exportExcel(list, "ftmsorderbills");
    }

    /**
     * 获取订单主详细信息
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:query')")
    @GetMapping(value = "/{id}")
    @RepeatSubmit
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("缺少查询条件，请确认");
        }
        Long billType = 1000L;
        return AjaxResult.success(ftmsorderbillsService.selectftmsorderbillsById(id, billType));
    }

    /**
     * 计划取消/撤回
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:edit')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/planCancel")
    @RepeatSubmit
    public AjaxResult planCancel(@RequestBody Ftmsorderbills ftmsorderbills) {
        if (StringUtils.isNull(ftmsorderbills) || StringUtils.isNull(ftmsorderbills.getId())) {
            return AjaxResult.error("未找到计划主表数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsService.planCancel(ftmsorderbills, loginUser);
    }

    /**
     * 计划变更
     */
    @RepeatSubmit
    @PostMapping(value = "/planStatusChange")
    public AjaxResult planStatusChange(@RequestBody Ftmsorderbills ftmsorderbills) {
        return ftmsorderbillsService.planStatusChange(ftmsorderbills);
    }

    /**
     * 计划下达变更提交
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:edit')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/planChange")
    @RepeatSubmit
    public AjaxResult planChange(@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("未找到计划货量数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsService.insertPlanChange(tmsorder, scntrs, attachs, fees, plans, loginUser);
    }

    /**
     * 提交计划下单
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills: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("未找到计划主表数据，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return ftmsorderbillsService.insertftmsorderbills(tmsorder, scntrs, attachs, fees, plans, loginUser);
    }

    /**
     * 提交计划下单
     * @param id 订单ID
     * 20210707 dongyongwei
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:add')")
    @Log(title = "订单提交", businessType = BusinessType.INSERT)
    @GetMapping(value = "/submit")
    @RepeatSubmit
    public AjaxResult submit(Long id) {
        //更新状态为 2待调度
        ftmsorderbillsService.setFtmsorderbillsBillStatus(id,2L);
        return AjaxResult.success();
    }

    /**
     * 修改订单主
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:edit')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ftmsorderbills ftmsorderbills) {
        return toAjax(ftmsorderbillsService.updateftmsorderbills(ftmsorderbills));
    }

    /**
     * 删除订单主
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:remove')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (StringUtils.isNull(ids)) {
            return AjaxResult.error("未找到需要删除的数据，请确认");
        }
        return ftmsorderbillsService.deleteftmsorderbillsByIds(ids);
    }
}
