package com.ruoyi.orderPlan.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.WechatService;
import com.ruoyi.orderPlan.domain.*;
import com.ruoyi.orderPlan.mapper.*;
import com.ruoyi.orderPlan.service.IftmsorderbillsplansService;
import com.ruoyi.orderPlan.domain.Ftmsorderbillsfees;
import com.ruoyi.orderPlan.mapper.FtmsorderbillsfeesMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.warehouseBusiness.service.impl.BillnoSerialServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 订单调度Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-22
 */
@Service
public class FtmsorderbillsplansServiceImpl implements IftmsorderbillsplansService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private FtmsorderbillsMapper ftmsorderbillsMapper;

    @Autowired
    private BillnoSerialServiceImpl billnoSerialServiceImpl;

    @Autowired
    private FtmsorderbillsServiceImpl ftmsorderbillsService;

    @Autowired
    private FtmsorderbillsfeesMapper ftmsorderbillsfeesMapper;

    @Autowired
    private FtmsorderbillscarsMapper ftmsorderbillscarsMapper;

    @Autowired
    private FtmsorderbillscntrsMapper ftmsorderbillscntrsMapper;

    @Autowired
    private FtmsorderbillsplansMapper ftmsorderbillsplansMapper;

    @Autowired
    private ProjectBillsHistoryMapper projectBillsHistoryMapper;

    @Autowired
    private FtmsorderbillsattachsMapper ftmsorderbillsattachsMapper;

    /**
     * 查询订单调度列表
     *
     * @param ftmsorderbillsplans 订单调度
     * @return 订单调度
     */
    @Override
    public List<Ftmsorderbillsplans> selectftmsorderbillsplansList(Ftmsorderbillsplans ftmsorderbillsplans) {
        return ftmsorderbillsplansMapper.selectftmsorderbillsplansList(ftmsorderbillsplans);
    }

    /**
     * 查询订单调度
     *
     * @param id 订单调度ID
     * @return 订单调度
     */
    @Override
    public AjaxResult selectftmsorderbillsplansById(Long id) {
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillsplans tmsorderbillsplans = ftmsorderbillsplansMapper.selectftmsorderbillsplansById(id);
        if (StringUtils.isNull(tmsorderbillsplans)) {
            return AjaxResult.error("未找到调度信息，请确认");
        }
        map.put("plans", tmsorderbillsplans);
        Ftmsorderbills ftmsorderbills = ftmsorderbillsMapper.selectftmsorderbillsById(tmsorderbillsplans.getOrgId());
        map.put("tmsorder", ftmsorderbills);
        Ftmsorderbillscars tmsorderbillscars = new Ftmsorderbillscars();
        tmsorderbillscars.setpId(ftmsorderbills.getId());
        map.put("cars", ftmsorderbillscarsMapper.selectftmsorderbillscarsList(tmsorderbillscars));
        Ftmsorderbillsattachs ftmsorderbillsattachs = new Ftmsorderbillsattachs();
        ftmsorderbillsattachs.setPId(ftmsorderbills.getId());
        ftmsorderbillsattachs.setActId(1010L);
        map.put("attach", ftmsorderbillsattachsMapper.selectftmsorderbillsattachsList(ftmsorderbillsattachs));
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(ftmsorderbills.getId());
        ftmsorderbillsfees.setActId(1010L);
        map.put("fees", ftmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(ftmsorderbillsfees));
        return AjaxResult.success(map);
    }

    /**
     * 查询车队派车列表数据
     *
     * @param ftmsorderbills 参数
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectftmsorderbillsplansMapList(Ftmsorderbills ftmsorderbills) {
        return ftmsorderbillsplansMapper.selectftmsorderbillsplansMapList(ftmsorderbills);
    }

    /**
     * 查询车队派车列表数据(配载)
     *
     * @param ftmsorderbills 参数
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectftmsorderbillsplansMapListForStowage(Ftmsorderbills ftmsorderbills) {
        return ftmsorderbillsplansMapper.selectftmsorderbillsplansMapListForStowage(ftmsorderbills);
    }

    /**
     *  单条订单调度变更
     * @param plans        调度
     * @param loginUser    用户
     * @return  结果
     */
    @Override
    @Transactional
    public AjaxResult schedulingChange(Ftmsorderbillsplans plans, LoginUser loginUser) {
        Ftmsorderbillsplans ftmsorderbillsplans = ftmsorderbillsplansMapper.selectftmsorderbillsplansById(plans.getId());
        if (ftmsorderbillsplans.getBillStatus() == 3L) {
            return AjaxResult.error("该数据已撤销无法变更");
        }
        if (ftmsorderbillsplans.getBillStatus() < 6) {
            return AjaxResult.error("该数据未提交无法变更");
        }
        if (ftmsorderbillsplans.getBillStatus() > 6) {
            return AjaxResult.error("该数据已在车队派车操作无法变更");
        }
        return AjaxResult.success();
    }

    /**
     *  订单调度变更
     * @param id        主表id
     * @param loginUser 用户
     * @return  结果
     */
    @Override
    @Transactional
    public AjaxResult schedulingChanges(Long id, LoginUser loginUser) {
        Ftmsorderbills ftmsorderbills = ftmsorderbillsMapper.selectftmsorderbillsById(id);
        if (ftmsorderbills.getBillStatus() == 30L) {
            return AjaxResult.error("该数据已在计划下单撤销无法变更");
        }
        if (ftmsorderbills.getBillStatus() == 70L) {
            return AjaxResult.error("该数据已在调度安排撤销无法变更");
        }
        if (ftmsorderbills.getBillStatus() < 80L) {
            return AjaxResult.error("该数据未提交变更");
        }
        if (ftmsorderbills.getBillStatus() == 90L) {
            return AjaxResult.error("该数据已在变更中");
        }
        ftmsorderbills.setBillStatus(70L);
        ftmsorderbills.setUpdateTime(new Date());
        ftmsorderbills.setUpdateBy(loginUser.getUsername());
        if (ftmsorderbillsMapper.updateftmsorderbills(ftmsorderbills) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新调度安排变更状态失败，若多次失败请联系管理员");
        }
        return AjaxResult.success();
    }

    /**
     * 新增订单调度
     *
     * @param ftmsorderbillsplans 订单调度
     * @return 结果
     */
    @Override
    public int insertftmsorderbillsplans(Ftmsorderbillsplans ftmsorderbillsplans) {
        ftmsorderbillsplans.setCreateTime(DateUtils.getNowDate());
        return ftmsorderbillsplansMapper.insertftmsorderbillsplans(ftmsorderbillsplans);
    }

    /**
     * 修改订单调度
     *
     * @param ftmsorderbillsplans 订单调度
     * @return 结果
     */
    @Override
    public int updateftmsorderbillsplans(Ftmsorderbillsplans ftmsorderbillsplans) {
        ftmsorderbillsplans.setUpdateTime(DateUtils.getNowDate());
        return ftmsorderbillsplansMapper.updateftmsorderbillsplans(ftmsorderbillsplans);
    }

    /**
     * 批量删除订单调度
     *
     * @param ids 需要删除的订单调度ID
     * @return 结果
     */
    @Override
    public int deleteftmsorderbillsplansByIds(Long[] ids) {
        return ftmsorderbillsplansMapper.deleteftmsorderbillsplansByIds(ids);
    }

    /**
     * 删除订单调度信息
     *
     * @param id 订单调度ID
     * @return 结果
     */
    @Override
    public int deleteftmsorderbillsplansById(Long id) {
        return ftmsorderbillsplansMapper.deleteftmsorderbillsplansById(id);
    }

    /**
     * 单条订单调度提交
     *
     * @param tmsorder  计划表数据
     * @param plans     调度表数据
     * @param loginUser 操作人
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult schedulingSave(String tmsorder, String plans, LoginUser loginUser) {
        // 计划数据
        Ftmsorderbills tmsorderbills = JSONArray.parseObject(tmsorder, Ftmsorderbills.class);
        long pId = tmsorderbills.getId();
        String status = ftmsorderbillsService.objValidation(null, null, null, plans, null);
        if (StringUtils.isNotEmpty(status)) {
            return AjaxResult.error(status);
        }
        boolean planStatus = false;
        // 调拨数据
        Ftmsorderbillsplans tmsorderbillsplans = JSONArray.parseObject(plans, Ftmsorderbillsplans.class);
        if (StringUtils.isNotNull(tmsorderbillsplans.getId())) {
            planStatus = true;
            // 判断修改的货量是否低于派车货量
            Ftmsorderbillsplans ftmsorderbillsplans = ftmsorderbillsplansMapper.selectftmsorderbillsplansById(tmsorderbillsplans.getId());
            if (tmsorderbillsplans.getCntrQty() < ftmsorderbillsplans.getCarQty()) {
                return AjaxResult.error("调度安排货量不得低于已派车数量");
            }
        }
        // 箱型箱量数据
        Ftmsorderbillscntrs ftmsorderbillscntrs = ftmsorderbillscntrsMapper.selectftmsorderbillscntrsById(tmsorderbillsplans.getpId());
        if (ftmsorderbillscntrs.getCntrBlcQty() < tmsorderbillsplans.getCntrQty()) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("调度数量超出计划箱量不可提交");
        }
        tmsorderbillsplans.setOrgId(pId);
        tmsorderbillsplans.setBillStatus(6L);
        tmsorderbillsplans.setCreateTime(new Date());
        tmsorderbillsplans.setCreateBy(loginUser.getUsername());
        int insertftmsorderbillsplans = 0;
        if (!planStatus) {
            insertftmsorderbillsplans = ftmsorderbillsplansMapper.insertftmsorderbillsplans(tmsorderbillsplans);
        } else {
            insertftmsorderbillsplans = ftmsorderbillsplansMapper.updateftmsorderbillsplans(tmsorderbillsplans);
        }
        if (insertftmsorderbillsplans <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新调度数据失败，若多次失败请联系管理员");
        }
        // 计划下达数据
       /* Ftmsorderbills bills = ftmsorderbillsMapper.selectftmsorderbillsById(tmsorderbillsplans.getOrgId());
        bills.setPlanUserId(loginUser.getUser().getUserId());
        bills.setPlanUserName(loginUser.getUser().getNickName());
        bills.setQtyDisPatch(bills.getQtyDisPatch() + tmsorderbillsplans.getCntrQty());
        if (ftmsorderbillsMapper.updateftmsorderbills(bills) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新派车数据失败，若多次失败请联系管理员");
        }*/
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscntrs tmsorderbillscntrs = new Ftmsorderbillscntrs();
        tmsorderbillscntrs.setOrgId(tmsorderbillsplans.getOrgId());
        map.put("plan", tmsorderbillsplans);
        map.put("cntrs", ftmsorderbillscntrsMapper.selectftmsorderbillscntrsList(tmsorderbillscntrs));

        //新增逻辑20210707 dongyongwei,
        //更改订单状态为
        // 3调度中 计划量和调度量比较 计划量大于0 调度量<>0  计划量>调度量
        // 4待派车 计划量和调度量比较 计划量大于0 计划量=调度量
        ftmsorderbillsService.setFtmsorderbillsBillStatus(pId,3L);

        return AjaxResult.success(map);
    }

    /**
     * 单条订单调度变更提交
     *
     * @param tmsorder  机会主表
     * @param plans     调度
     * @param loginUser 用户
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertSchedulingChange(String tmsorder, String plans, LoginUser loginUser) {
        // 计划数据
        Ftmsorderbills tmsorderbills = JSONArray.parseObject(tmsorder, Ftmsorderbills.class);
        long pId = tmsorderbills.getId();
        String status = ftmsorderbillsService.objValidation(null, null, null, plans, null);
        if (StringUtils.isNotEmpty(status)) {
            return AjaxResult.error(status);
        }
        tmsorderbills.setBillStatus(60L);
        tmsorderbills.setUpdateTime(new Date());
        tmsorderbills.setUpdateBy(loginUser.getUsername());
        ftmsorderbillsMapper.updateftmsorderbills(tmsorderbills);
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(pId);
        ftmsorderbillsfees.setActId(1010L);
        ftmsorderbillsfeesMapper.deleteFTmsorderbillsfees(ftmsorderbillsfees);
        Ftmsorderbillsattachs ftmsorderbillsattachs = new Ftmsorderbillsattachs();
        ftmsorderbillsattachs.setPId(pId);
        ftmsorderbillsattachs.setActId(1010L);
        ftmsorderbillsattachsMapper.deleteftmsorderbillsattachs(ftmsorderbillsattachs);
        // 调拨数据
        Ftmsorderbillsplans tmsorderbillsplans = JSONArray.parseObject(plans, Ftmsorderbillsplans.class);
        // 判断修改的货量是否低于派车货量
        Ftmsorderbillsplans ftmsorderbillsplans = ftmsorderbillsplansMapper.selectftmsorderbillsplansById(tmsorderbillsplans.getId());
        if (tmsorderbillsplans.getCntrQty() < ftmsorderbillsplans.getCarQty()) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("调度安排货量不得低于已派车数量");
        }
        // 箱型箱量数据
        Ftmsorderbillscntrs tmsorderbillscntrs = ftmsorderbillscntrsMapper.selectftmsorderbillscntrsById(tmsorderbillsplans.getpId());
        if (StringUtils.isNull(tmsorderbillscntrs)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("未找到计划箱量数据");
        }
        if (!tmsorderbillscntrs.getCntrId().equals(tmsorderbillsplans.getCntrId())) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("调度安排箱型数据与计划箱量箱型数据不匹配");
        }
        long qty = tmsorderbillscntrs.getCntrPlanQty() - ftmsorderbillsplans.getCntrQty() + tmsorderbillsplans.getCntrQty();
        if (tmsorderbillscntrs.getCntrQty() < tmsorderbillsplans.getCntrQty()) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("调度数量超出计划箱量不可提交");
        }
        /*tmsorderbillscntrs.setCntrPlanQty(qty);
        if (ftmsorderbillscntrsMapper.updateftmsorderbillscntrs(tmsorderbillscntrs) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新派车数据失败，若多次失败请联系管理员");
        }*/
        // 计划下达数据
        /*Ftmsorderbills bills = ftmsorderbillsMapper.selectftmsorderbillsById(tmsorderbillsplans.getOrgId());
        long cntrQty = ftmsorderbillsplans.getCntrQty() - tmsorderbillsplans.getCntrQty();
        bills.setQtyDisPatch(bills.getQtyDisPatch() + cntrQty);
        if (ftmsorderbillsMapper.updateftmsorderbills(bills) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新派车数据失败，若多次失败请联系管理员");
        }*/
        tmsorderbillsplans.setBillStatus(6L);
        tmsorderbillsplans.setCreateTime(new Date());
        tmsorderbillsplans.setCreateBy(loginUser.getUsername());
        if (ftmsorderbillsplansMapper.updateftmsorderbillsplans(tmsorderbillsplans) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新调度数据失败，若多次失败请联系管理员");
        }
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscntrs scntrs = new Ftmsorderbillscntrs();
        scntrs.setOrgId(tmsorderbillsplans.getOrgId());
        map.put("plan", tmsorderbillsplans);
        map.put("cntrs", ftmsorderbillscntrsMapper.selectftmsorderbillscntrsList(scntrs));

        //新增逻辑20210707 dongyongwei,
        //更改订单状态为
        // 3调度中 计划量和调度量比较 计划量大于0 调度量<>0  计划量>调度量
        // 4待派车 计划量和调度量比较 计划量大于0 计划量=调度量
        ftmsorderbillsService.setFtmsorderbillsBillStatus(pId,3L);

        return AjaxResult.success(map);
    }

    /**
     * 单条订单调度撤销/删除
     *
     * @param plans     调度表数据
     * @param type      类型
     * @param loginUser 操作人
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult schedulingRevoke(Ftmsorderbillsplans plans, String type, LoginUser loginUser) {
        long pId = plans.getId();
        Ftmsorderbillsplans ftmsorderbillsplans = ftmsorderbillsplansMapper.selectftmsorderbillsplansById(plans.getId());
        Ftmsorderbillscars ftmsorderbillscars = new Ftmsorderbillscars();
        ftmsorderbillscars.setpId(pId);
        if (ftmsorderbillscarsMapper.selectftmsorderbillscarsCount(ftmsorderbillscars) > 0) {
            return AjaxResult.error("撤销失败，车队已派车");
        }
        // 删除操作
        if ("remove".equals(type)) {
            if (ftmsorderbillsplans.getBillStatus() == 6L) {
                return AjaxResult.error("操作失败，该单据已提交");
            }
            if (ftmsorderbillsplans.getCarQty() > 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("该调度安排已派车，无法删除");
            }
            ftmsorderbillsplansMapper.deleteftmsorderbillsplansById(pId);
            ftmsorderbillscarsMapper.deleteFollowUpBusinessByCarPId(pId);
            return AjaxResult.success();
        }
        if (ftmsorderbillsplans.getBillStatus() != 6) {
            return AjaxResult.error("操作失败，该单据未提交");
        }
        // 查询箱量
        /*Ftmsorderbillscntrs ftmsorderbillscntrs = ftmsorderbillscntrsMapper.selectftmsorderbillscntrsById(plans.getPId());
        long planQty = ftmsorderbillscntrs.getCntrPlanQty() - plans.getCntrQty();
        ftmsorderbillscntrs.setCntrPlanQty(planQty);
        if (ftmsorderbillscntrsMapper.updateftmsorderbillscntrs(ftmsorderbillscntrs) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新箱量数据失败，若多次出现请联系管理员");
        }
        // 计划下达数据
        Ftmsorderbills bills = ftmsorderbillsMapper.selectftmsorderbillsById(plans.getOrgId());
        bills.setQtyDisPatch(bills.getQtyDisPatch() + plans.getCntrQty());
        if (ftmsorderbillsMapper.updateftmsorderbills(bills) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新派车数据失败，若多次失败请联系管理员");
        }*/
        String planJson = JSON.toJSONString(ftmsorderbillsplans);
        Date time = new Date();
        // 调度备份流水号
        String billNo = billnoSerialServiceImpl.getSerialNumber("DDBF", time);
        ProjectBillsHistory projectBillsHistory = new ProjectBillsHistory();
        projectBillsHistory.setActId(1010L);
        projectBillsHistory.setProjectId(pId);
        projectBillsHistory.setSerialNumber(billNo);
        projectBillsHistory.setHistoryBody1(planJson);
        projectBillsHistory.setHistoryDate(new Date());
        projectBillsHistory.setHistoryUser(loginUser.getUser().getUserId());
        if (projectBillsHistoryMapper.insertProjectBillsHistory(projectBillsHistory) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新备份数据失败，若多次出现请联系管理员");
        }
        plans.setBillStatus(3L);
        plans.setUpdateTime(new Date());
        plans.setUpdateBy(loginUser.getUsername());
        if (ftmsorderbillsplansMapper.updateftmsorderbillsplans(plans) <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("更新调度数据失败，若多次出现请联系管理员");
        }
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscntrs tmsorderbillscntrs = new Ftmsorderbillscntrs();
        tmsorderbillscntrs.setOrgId(plans.getOrgId());
        tmsorderbillscntrs.setBillStatus(6L);
        map.put("plan", plans);
        map.put("cntrs", ftmsorderbillscntrsMapper.selectftmsorderbillscntrsList(tmsorderbillscntrs));

        //新增逻辑20210707 dongyongwei,
        //更改订单状态为
        // 3调度中 计划量和调度量比较 计划量大于0 调度量<>0  计划量>调度量
        // 4待派车 计划量和调度量比较 计划量大于0 计划量=调度量
        ftmsorderbillsService.setFtmsorderbillsBillStatus(pId,3L);

        return AjaxResult.success(map);
    }

    /**
     * 保存订单调度
     *
     * @param tmsorder  主表id
     * @param attachs   附件
     * @param fees      费用
     * @param plans     调度安排
     * @param loginUser 登录用户
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult saveFtmsorderbills(String tmsorder, String attachs, String fees, String plans, LoginUser loginUser) {
        Ftmsorderbills tmsorderbills = JSONArray.parseObject(tmsorder, Ftmsorderbills.class);
        long pId = tmsorderbills.getId();
        Ftmsorderbills ftmsorderbills = ftmsorderbillsMapper.selectftmsorderbillsById(pId);
        ftmsorderbills.setBillStatus(20L);
        ftmsorderbills.setUpdateTime(new Date());
        ftmsorderbills.setUpdateBy(loginUser.getUsername());
        ftmsorderbills.setPlanUserId(loginUser.getUser().getUserId());
        ftmsorderbills.setPlanUserName(loginUser.getUser().getNickName());
        ftmsorderbillsMapper.updateftmsorderbills(ftmsorderbills);
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(pId);
        ftmsorderbillsfees.setActId(1010L);
        ftmsorderbillsfeesMapper.deleteFTmsorderbillsfees(ftmsorderbillsfees);
        Ftmsorderbillsattachs ftmsorderbillsattachs = new Ftmsorderbillsattachs();
        ftmsorderbillsattachs.setPId(pId);
        ftmsorderbillsattachs.setActId(1010L);
        ftmsorderbillsattachsMapper.deleteftmsorderbillsattachs(ftmsorderbillsattachs);
        if (StringUtils.isNotNull(attachs) && !"[]".equals(attachs)) {
            JSONArray attachsJson = JSONArray.parseArray(attachs);
            List<Ftmsorderbillsattachs> tmsorderbillsattachsList = JSONObject.parseArray(attachsJson.toJSONString(), Ftmsorderbillsattachs.class);
            int line = 0;
            for (Ftmsorderbillsattachs t : tmsorderbillsattachsList) {
                line++;
                if (StringUtils.isEmpty(t.getAttachUrl())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("附件数据第" + line + "行缺少附件地址");
                }
                t.setPId(pId);
                t.setOrgId(pId);
                t.setActId(1010L);
                t.setBillStatus(2L);
                t.setCreateBy(loginUser.getUsername());
                t.setCreateTime(new Date());
                if (ftmsorderbillsattachsMapper.insertftmsorderbillsattachs(t) <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("更新附件数据失败，若多次失败请联系管理员");
                }
            }
        }
        if (StringUtils.isNotNull(fees) && !"[]".equals(fees)) {
            JSONArray feesJson = JSONArray.parseArray(fees);
            List<Ftmsorderbillsfees> tmsorderbillsfeesList = JSONObject.parseArray(feesJson.toJSONString(), Ftmsorderbillsfees.class);
            int line = 0;
            for (Ftmsorderbillsfees f : tmsorderbillsfeesList) {
                line++;
                if (StringUtils.isNull(f.getfCorpid()) || f.getfCorpid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少客户数据");
                }
                if (StringUtils.isNull(f.getfFeeid()) || f.getfFeeid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少费用数据");
                }
                if (StringUtils.isNull(f.getfFeeunitid()) || f.getfFeeunitid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少计价单位");
                }
                f.setfPid(pId);
                f.setOrgId(pId);
                f.setActId(1010L);
                f.setfStatus(2L);
                f.setCreateBy(loginUser.getUsername());
                f.setCreateTime(new Date());
                if (ftmsorderbillsfeesMapper.insertFTmsorderbillsfees(f) <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("更新费用数据失败，若多次失败请联系管理员");
                }
            }
        }
        // 调度安排
        if (StringUtils.isNotNull(plans) && !"[]".equals(plans)) {
            JSONArray planJson = JSONArray.parseArray(plans);
            List<Ftmsorderbillsplans> tmsorderbillsplansList = JSONObject.parseArray(planJson.toJSONString(), Ftmsorderbillsplans.class);
            int line = 0;
            for (Ftmsorderbillsplans p : tmsorderbillsplansList) {
                line++;
                Ftmsorderbillscntrs ftmsorderbillscntrs = ftmsorderbillscntrsMapper.selectftmsorderbillscntrsById(p.getpId());
                if (StringUtils.isNull(ftmsorderbillscntrs)) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("调度安排第" + line + "行未找到箱型箱量数据");
                }
                if (!ftmsorderbillscntrs.getCntrId().equals(p.getCntrId())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("调度安排第" + line + "行箱型数据与计划箱量箱型数据不匹配");
                }
                if (StringUtils.isNull(p.getCarcorPid())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("调度安排第" + line + "行未找到车队数据");
                }
                p.setOrgId(pId);
                if (StringUtils.isNull(p.getId())) {
                    p.setBillStatus(2L);
                    p.setCreateTime(new Date());
                    p.setCreateBy(loginUser.getUsername());
                    ftmsorderbillsplansMapper.insertftmsorderbillsplans(p);
                } else {
                    p.setUpdateTime(new Date());
                    p.setUpdateBy(loginUser.getUsername());
                    ftmsorderbillsplansMapper.updateftmsorderbillsplans(p);
                }
            }
        }

        //新增逻辑20210707 dongyongwei,
        //更改订单状态为
        // 3调度中 计划量和调度量比较 计划量大于0 调度量<>0  计划量>调度量
        // 4待派车 计划量和调度量比较 计划量大于0 计划量=调度量
        ftmsorderbillsService.setFtmsorderbillsBillStatus(pId,3L);

        return AjaxResult.success(ftmsorderbillsplansMapper.selectftmsorderbillsplansByOrgId(pId));
    }

    /**
     * 提交订单调度
     *
     * @param tmsorder  主表id
     * @param attachs   附件
     * @param fees      费用
     * @param plans     调度安排
     * @param loginUser 登录用户
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertftmsorderbills(String tmsorder, String attachs, String fees, String plans, LoginUser loginUser) {
        Ftmsorderbills tmsorderbills = JSONArray.parseObject(tmsorder, Ftmsorderbills.class);
        Ftmsorderbills ftmsorderbills = ftmsorderbillsMapper.selectftmsorderbillsById(tmsorderbills.getId());
        ftmsorderbills.setPlanUserId(loginUser.getUser().getUserId());
        ftmsorderbills.setTransUserId(loginUser.getUser().getUserId());
        ftmsorderbills.setPlanUserName(loginUser.getUser().getNickName());
        ftmsorderbills.setTransUserName(loginUser.getUser().getNickName());
        ftmsorderbills.setBillStatus(60L);
        ftmsorderbills.setUpdateTime(new Date());
        ftmsorderbills.setUpdateBy(loginUser.getUsername());
        ftmsorderbillsMapper.updateftmsorderbills(ftmsorderbills);
        long pId = ftmsorderbills.getId();
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(pId);
        ftmsorderbillsfees.setActId(1010L);
        ftmsorderbillsfeesMapper.deleteFTmsorderbillsfees(ftmsorderbillsfees);
        Ftmsorderbillsattachs ftmsorderbillsattachs = new Ftmsorderbillsattachs();
        ftmsorderbillsattachs.setPId(pId);
        ftmsorderbillsattachs.setActId(1010L);
        ftmsorderbillsattachsMapper.deleteftmsorderbillsattachs(ftmsorderbillsattachs);
        if (StringUtils.isNotNull(attachs) && !"[]".equals(attachs)) {
            JSONArray attachsJson = JSONArray.parseArray(attachs);
            List<Ftmsorderbillsattachs> tmsorderbillsattachsList = JSONObject.parseArray(attachsJson.toJSONString(), Ftmsorderbillsattachs.class);
            int line = 0;
            for (Ftmsorderbillsattachs t : tmsorderbillsattachsList) {
                line++;
                if (StringUtils.isEmpty(t.getAttachUrl())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("附件数据第" + line + "行缺少附件地址");
                }
                t.setPId(pId);
                t.setOrgId(pId);
                t.setActId(1010L);
                t.setBillStatus(6L);
                t.setCreateBy(loginUser.getUsername());
                t.setCreateTime(new Date());
                if (ftmsorderbillsattachsMapper.insertftmsorderbillsattachs(t) <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("更新附件数据失败，若多次失败请联系管理员");
                }
            }
        }
        if (StringUtils.isNotNull(fees) && !"[]".equals(fees)) {
            JSONArray feesJson = JSONArray.parseArray(fees);
            List<Ftmsorderbillsfees> tmsorderbillsfeesList = JSONObject.parseArray(feesJson.toJSONString(), Ftmsorderbillsfees.class);
            int line = 0;
            for (Ftmsorderbillsfees f : tmsorderbillsfeesList) {
                line++;
                if (StringUtils.isNull(f.getfCorpid()) || f.getfCorpid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少客户数据");
                }
                if (StringUtils.isNull(f.getfFeeid()) || f.getfFeeid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少费用数据");
                }
                if (StringUtils.isNull(f.getfFeeunitid()) || f.getfFeeunitid() == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("费用数据第" + line + "行缺少计价单位");
                }
                f.setfPid(pId);
                f.setOrgId(pId);
                f.setActId(1010L);
                f.setfStatus(6L);
                f.setCreateBy(loginUser.getUsername());
                f.setCreateTime(new Date());
                if (ftmsorderbillsfeesMapper.insertFTmsorderbillsfees(f) <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("更新费用数据失败，若多次失败请联系管理员");
                }
            }
        }
        // 调度安排
        JSONArray planJson = JSONArray.parseArray(plans);
        List<Ftmsorderbillsplans> tmsorderbillsplansList = JSONObject.parseArray(planJson.toJSONString(), Ftmsorderbillsplans.class);
        int line = 0;
        for (Ftmsorderbillsplans p : tmsorderbillsplansList) {
            line++;
            Ftmsorderbillscntrs ftmsorderbillscntrs = ftmsorderbillscntrsMapper.selectftmsorderbillscntrsById(p.getpId());
            if (StringUtils.isNull(ftmsorderbillscntrs)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("调度安排第" + line + "行未找到箱型箱量数据");
            }
            if (!ftmsorderbillscntrs.getCntrId().equals(p.getCntrId())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("调度安排第" + line + "行箱型数据与计划箱量箱型数据不匹配");
            }
            if (StringUtils.isNull(p.getCarcorPid())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("调度安排第" + line + "行未找到车队数据");
            }
        }

        //新增逻辑20210707 dongyongwei,
        //更改订单状态为
        // 3调度中 计划量和调度量比较 计划量大于0 调度量<>0  计划量>调度量
        // 4待派车 计划量和调度量比较 计划量大于0 计划量=调度量
        ftmsorderbillsService.setFtmsorderbillsBillStatus(pId,3L);

        return AjaxResult.success();
    }

}
