package com.ruoyi.web.controller.fleet;

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

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.Ftmsorderbills;
import com.ruoyi.orderManagement.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;

    /**
     * 查询订单列表显示字段
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:query')")
    @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}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("缺少查询条件，请确认");
        }
        return AjaxResult.success(ftmsorderbillsService.selectftmsorderbillsById(id));
    }

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

    /**
     * 判断是否允许变更
     */
    @GetMapping(value = "/allowChanges/{id}")
    public boolean allowChanges(@PathVariable("id") Long id) {
        return ftmsorderbillsService.allowChanges(id);
    }

    /**
     * 计划下达变更提交
     */
    @PreAuthorize("@ss.hasPermi('fleet:ftmsorderbills:edit')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/planChange")
    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
    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);
    }

    /**
     * 修改订单主
     */
    @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}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ftmsorderbillsService.deleteftmsorderbillsByIds(ids));
    }
}
