package com.steerinfo.appservice.appmeterworkcaractual.service.impl;

import com.steerinfo.appservice.appmeterbasecustomersupplier.mapper.AppMeterBaseCustomerSupplierMapper;
import com.steerinfo.appservice.appmeterbasecustomersupplier.model.AppMeterBaseCustomerSupplier;
import com.steerinfo.appservice.appmeterworkcaractual.mapper.AppMeterWorkCarActualMapper;
import com.steerinfo.appservice.appmeterworkcaractual.model.AppMeterWorkCarActual;
import com.steerinfo.appservice.appmeterworkcaractual.service.IAppMeterWorkCarActualService;
import com.steerinfo.appservice.dto.AppMeterWorkCarActualDemo;
import com.steerinfo.appservice.dto.LoadLinkDemo;
import com.steerinfo.appservice.dto.companyDemo;
import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.baseinfo.meterbasecarriercustomer.mapper.MeterBaseCarrierCustomerMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.baseinfo.meterbaseunloadtime.mapper.MeterBaseUnloadTimeMapper;
import com.steerinfo.baseinfo.meterbaseunloadtime.model.MeterBaseUnloadTime;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkprelink.mapper.MeterWorkPreLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.model.MeterWorkPreLink;
import com.steerinfo.meterwork.meterworkprelink.service.IMeterWorkPreLinkService;
import com.steerinfo.pretrack.constants.DbConstants;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.pretrack.pretrackscale.service.IPreTrackScaleService;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * MeterWorkCarActual服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-09-17 11:03
 * 类描述
 * 修订历史：
 * 日期：2020-09-17
 * 作者：generator
 * 参考：
 * 描述：MeterWorkCarActual服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "AppMeterWorkCarActualService")
@Primary
public class AppMeterWorkCarActualServiceImpl extends BaseServiceImpl<AppMeterWorkCarActual, String> implements IAppMeterWorkCarActualService {


    @Autowired
    IMeterWorkPreLinkService meterWorkPreLinkService;

    @Autowired
    IPreTrackScaleService preTrackScaleService;

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private AppMeterWorkCarActualMapper appMeterWorkCarActualMapper;

    @Resource
    private AppMeterBaseCustomerSupplierMapper appMeterBaseCustomerSupplierMapper;

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    private MeterBaseMatterInfoMapper meterBaseMatterInfoMapper;

    @Resource
    private MeterWorkPreLinkMapper meterWorkPreLinkMapper;

    @Resource
    private MeterBaseUnloadTimeMapper meterBaseUnloadTimeMapper;

    @Resource
    private MeterBaseCarrierCustomerMapper meterBaseCarrierCustomerMapper;

    @Override
    protected IBaseMapper<AppMeterWorkCarActual, String> getMapper() {
        return appMeterWorkCarActualMapper;
    }

    @Override
    public PageList<AppMeterWorkCarActualDemo> queryForApp(Map<String, Object> parameters, Integer pageNum, Integer pageSize) {
        if (parameters == null || parameters.size() < 1) {
            return null;
        }
        Object customerSupplierNoObj = parameters.get("customerSupplierNo");
        if (customerSupplierNoObj == null) {
            return null;
        }
        String no = customerSupplierNoObj.toString();
        AppMeterBaseCustomerSupplier mbcs = appMeterBaseCustomerSupplierMapper.selectByPrimaryKey(no);
        List<String> meterTypeNos = new ArrayList<>();
        String customerSupplierNo = null;
        String receivingUintNo = null;

        String matterNo = "";
        String meterTypeNo = "";
        String startTime = "";
        String endTime = "";
        String forwardingUnitName = "";
        String receivingUintName = "";
        String loadPointName = "";
        Object matterNoObj = parameters.get("matterNo");
        if (matterNoObj != null) {
            matterNo = matterNoObj.toString();
        }

        Object meterTypeNoObj = parameters.get("meterTypeNo");
        if (meterTypeNoObj != null) {
            meterTypeNo = meterTypeNoObj.toString();
        }

        Object startTimeObj = parameters.get("startTime");
        if (startTimeObj != null) {
            startTime = startTimeObj.toString();
        }

        Object endTimeObj = parameters.get("endTime");
        if (endTimeObj != null) {
            endTime = endTimeObj.toString();
        }
        Object forwardingUnitNameObj = parameters.get("forwardingUnitName");
        if (forwardingUnitNameObj != null) {
            forwardingUnitName = forwardingUnitNameObj.toString();
        }
        Object receivingUintNameObj = parameters.get("receivingUintName");
        if (receivingUintNameObj != null) {
            receivingUintName = receivingUintNameObj.toString();
        }
        Object loadPointNameObj = parameters.get("loadPointName");
        if (loadPointNameObj != null) {
            loadPointName = loadPointNameObj.toString();
        }
        switch (mbcs.getCustomerSupplierType()) {
            case "0": //客户
                meterTypeNos.add("001006002");
                receivingUintNo = no;
                break;
            case "1": //供应商
                meterTypeNos.add("001006001");
                customerSupplierNo = no;
                break;
            case "2": //客户&供应商
                meterTypeNos.add("001006001");
                customerSupplierNo = no;
                meterTypeNos.add("001006002");
                receivingUintNo = no;
                break;
            case "3"://货代
                return selectSumHD(no, matterNo, meterTypeNo, startTime, endTime, pageNum, pageSize, forwardingUnitName, receivingUintName, loadPointName);
            default:
                return null;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActualDemo> list = selectSumForApp(meterTypeNos, customerSupplierNo, receivingUintNo, matterNo, meterTypeNo, startTime, endTime, forwardingUnitName, receivingUintName, loadPointName);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    private PageList<AppMeterWorkCarActualDemo> selectSumHD(String no, String matterNo, String meterTypeNo, String startTime, String endTime
            , Integer pageNum, Integer pageSize, String forwardingUnitName, String receivingUintName, String loadPointName) {
        //获取货代对应的供应商
        List<String> ids = meterBaseCarrierCustomerMapper.selectHD(no);

        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(matterNo)) {
            map.put("matterNo", matterNo);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            map.put("endTime", endTime);
        }
        if (StringUtils.isNotEmpty(meterTypeNo)) {
            map.put("meterTypeNo", meterTypeNo);
        }
        if (StringUtils.isNotEmpty(forwardingUnitName)) {
            map.put("forwardingUnitName", forwardingUnitName);
        }
        if (StringUtils.isNotEmpty(receivingUintName)) {
            map.put("receivingUintName", receivingUintName);
        }
        if (StringUtils.isNotEmpty(loadPointName)) {
            map.put("loadPointName", loadPointName);
        }
        if (ids.size() > 0) {
            map.put("ids", ids);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActualDemo> rows = appMeterWorkCarActualMapper.selectSumHD(map);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    private PageList<AppMeterWorkCarActual> selectActualHD(String forwardingUnitName, String receivingUintName, String loadPointName, String no, String matterNo
            , String meterTypeNo, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        //获取货代对应的供应商
        List<String> ids = meterBaseCarrierCustomerMapper.selectHD(no);

        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(forwardingUnitName)) {
            map.put("forwardingUnitName", forwardingUnitName);
        }
        if (StringUtils.isNotEmpty(receivingUintName)) {
            map.put("receivingUintName", receivingUintName);
        }
        if (StringUtils.isNotEmpty(loadPointName)) {
            map.put("loadPointName", loadPointName);
        }
        if (StringUtils.isNotEmpty(matterNo)) {
            map.put("matterNo", matterNo);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            map.put("endTime", endTime);
        }
        if (StringUtils.isNotEmpty(meterTypeNo)) {
            map.put("meterTypeNo", meterTypeNo);
        }
        if (ids.size() > 0) {
            map.put("ids", ids);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActual> rows = appMeterWorkCarActualMapper.selectActualHD(map);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }



/*  //供应商
    public List<AppMeterWorkCarActualFirstDto> getPer1(String customerSupplierNo){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("meterTypeNo", "001006001");//采购进厂
        obj.put("customerSupplierNo", customerSupplierNo);
        List<AppMeterWorkCarActualFirstDto> rows = appMeterWorkCarActualFirstMapper.selectByCus(obj);
        return rows;
    }

    //客户
    public List<AppMeterWorkCarActualFirstDto> getPer2(String customerSupplierNo){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("meterTypeNo", "001006002");//销售出厂
        obj.put("receivingUintNo", customerSupplierNo );
        List<AppMeterWorkCarActualFirstDto> rows = appMeterWorkCarActualFirstMapper.selectByRec(obj);
        return rows;
    }*/

    private List<AppMeterWorkCarActualDemo> selectSumForApp(List<String> meterTypeNos, String customerSupplierNo,
                                                            String receivingUintNo, String matterNo, String meterTypeNo,
                                                            String startTime, String endTime, String forwardingUnitName,
                                                            String receivingUintName, String loadPointName) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(meterTypeNo)) {
            List<String> meterTypeNoES = new ArrayList<>();
            meterTypeNoES.add(meterTypeNo);
            map.put("meterTypeNos", meterTypeNoES);
        } else {
            if (meterTypeNos != null || meterTypeNos.size() > 0) {
                map.put("meterTypeNos", meterTypeNos);
            } else {
                return null;
            }
        }
        if (StringUtils.isNotEmpty(customerSupplierNo)) {
            map.put("customerSupplierNo", customerSupplierNo);
        }
        if (StringUtils.isNotEmpty(receivingUintNo)) {
            map.put("receivingUintNo", receivingUintNo);
        }
        if (StringUtils.isNotEmpty(matterNo)) {
            map.put("matterNo", matterNo);
        }

        if (StringUtils.isNotEmpty(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            map.put("endTime", endTime);
        }
        if (StringUtils.isNotEmpty(forwardingUnitName)) {
            map.put("forwardingUnitName", forwardingUnitName);
        }
        if (StringUtils.isNotEmpty(receivingUintName)) {
            map.put("receivingUintName", receivingUintName);
        }
        if (StringUtils.isNotEmpty(loadPointName)) {
            map.put("loadPointName", loadPointName);
        }
        return appMeterWorkCarActualMapper.selectSumForApp(map);
    }


    //获取计量类型
    @Override
    public List<ComBaseInfo> selectMeterType(HashMap parmas) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("pBaseCode", "001006");//计量类型
        obj.put("validFlag", "1");//有效
        List<ComBaseInfo> list = comBaseInfoMapper.selectByParameters(obj);
        return list;
    }

    //获取物料
    @Override
    public List<MeterBaseMatterInfo> selectMatter(HashMap parmas) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("validFlag", "1");//有效
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.selectByParameters(obj);
        return list;
    }


    @Override
    public PageList<AppMeterWorkCarActual> selectNet(Map<String, Object> parameters, Integer pageNum, Integer pageSize) {
        if (parameters == null || parameters.size() < 1) {
            return null;
        }
        Object customerSupplierNoObj = parameters.get("customerSupplierNo");
        if (customerSupplierNoObj == null) {
            return null;
        }
        String no = customerSupplierNoObj.toString();
        AppMeterBaseCustomerSupplier mbcs = appMeterBaseCustomerSupplierMapper.selectByPrimaryKey(no);
        List<String> meterTypeNos = new ArrayList<>();
        String customerSupplierNo = null;
        String receivingUintNo = null;

        String matterNo = "";
        String meterTypeNo = "";
        String startTime = "";
        String endTime = "";
        String forwardingUnitName = "";
        String receivingUintName = "";
        String loadPointName = "";

        Object matterNoObj = parameters.get("matterNo");
        if (matterNoObj != null) {
            matterNo = matterNoObj.toString();
        }

        Object meterTypeNoObj = parameters.get("meterTypeNo");
        if (meterTypeNoObj != null) {
            meterTypeNo = meterTypeNoObj.toString();
        }

        Object startTimeObj = parameters.get("startTime");
        if (startTimeObj != null) {
            startTime = startTimeObj.toString();
        }

        Object endTimeObj = parameters.get("endTime");
        if (endTimeObj != null) {
            endTime = endTimeObj.toString();
        }

        Object forwardingUnitNameObj = parameters.get("forwardingUnitName");
        if (forwardingUnitNameObj != null) {
            forwardingUnitName = forwardingUnitNameObj.toString();
        }

        Object receivingUintNameObj = parameters.get("receivingUintName");
        if (receivingUintNameObj != null) {
            receivingUintName = receivingUintNameObj.toString();
        }

        Object loadPointNameObj = parameters.get("loadPointName");
        if (loadPointNameObj != null) {
            loadPointName = loadPointNameObj.toString();
        }

        switch (mbcs.getCustomerSupplierType()) {
            case "0": //客户
                meterTypeNos.add("001006002");
                receivingUintNo = no;
                break;
            case "1": //供应商
                meterTypeNos.add("001006001");
                customerSupplierNo = no;
                break;
            case "2": //客户&供应商
                meterTypeNos.add("001006001");
                customerSupplierNo = no;
                meterTypeNos.add("001006002");
                receivingUintNo = no;
                break;
            case "3"://货代
                return selectActualHD(forwardingUnitName, receivingUintName, loadPointName, no, matterNo, meterTypeNo, startTime, endTime, pageNum, pageSize);
            default:
                return null;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActual> list = selectTime(meterTypeNos, forwardingUnitName, receivingUintName, loadPointName, customerSupplierNo, receivingUintNo, matterNo, meterTypeNo, startTime, endTime);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    private List<AppMeterWorkCarActual> selectTime(List<String> meterTypeNos, String forwardingUnitName, String receivingUintName, String loadPointName
            , String customerSupplierNo, String receivingUintNo, String matterNo, String meterTypeNo, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(meterTypeNo)) {
            List<String> meterTypeNoES = new ArrayList<>();
            meterTypeNoES.add(meterTypeNo);
            map.put("meterTypeNos", meterTypeNoES);
        } else {
            if (meterTypeNos != null || meterTypeNos.size() > 0) {
                map.put("meterTypeNos", meterTypeNos);
            } else {
                return null;
            }
        }
        if (StringUtils.isNotEmpty(forwardingUnitName)) {
            map.put("forwardingUnitName", forwardingUnitName);
        }
        if (StringUtils.isNotEmpty(receivingUintName)) {
            map.put("receivingUintName", receivingUintName);
        }
        if (StringUtils.isNotEmpty(loadPointName)) {
            map.put("loadPointName", loadPointName);
        }
        if (StringUtils.isNotEmpty(customerSupplierNo)) {
            map.put("customerSupplierNo", customerSupplierNo);
        }
        if (StringUtils.isNotEmpty(receivingUintNo)) {
            map.put("receivingUintNo", receivingUintNo);
        }
        if (StringUtils.isNotEmpty(matterNo)) {
            map.put("matterNo", matterNo);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            map.put("endTime", endTime);
        }
        return appMeterWorkCarActualMapper.selectNet(map);
    }

    @Override
    public PageList<AppMeterWorkCarActualDemo> selectSumForAppByCar(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActualDemo> list = appMeterWorkCarActualMapper.selectSumForAppByCar(parmas);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    public PageList<AppMeterWorkCarActual> selectNetByCar(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AppMeterWorkCarActual> list = appMeterWorkCarActualMapper.selectNetByCar(parmas);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }


    /**
     * 待卸货数据查询
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageList<MeterWorkCarActualFirst> selectForLoad(HashMap parmas, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("linkCode", "001021004");//卸货确认
        map.put("valueFlag", parmas.get("valueFlag").toString());
        List<MeterWorkPreLink> mwpls = meterWorkPreLinkMapper.selectByParameters(map);
        map.put("createManNo", parmas.get("createManNo").toString());
        if (mwpls.size() <= 0) {
            PageList pageInfo = new PageList();
            return pageInfo;
        }
        ArrayList<String> ids = new ArrayList<>();
        for (MeterWorkPreLink mwpl : mwpls) {
            ids.add(mwpl.getPredictionNo());
        }
        //2021年3月1日 杨秀东修改为1个sql;2021年3月8日杨秀东对状态为进行区分
        if ("1".equals(parmas.get("valueFlag").toString())) {
            PageHelper.startPage(pageNum, pageSize);
            List<MeterWorkCarActualFirst> list = meterWorkCarActualFirstMapper.selectForAppLoadFirst(map);
            PageList pageInfo = new PageList(list);
            return pageInfo;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirst> list = meterWorkCarActualFirstMapper.selectForAppLoad(map);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    public PageList<LoadLinkDemo> selectLoadLinkDemo(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LoadLinkDemo> list = appMeterWorkCarActualMapper.selectLoadLinkDemo(parmas);
        PageList pageInfo = new PageList(list);
        Double sum = 0.0;
        if (list.size() > 0) {
            for (LoadLinkDemo list1 : list) {
                sum = sum + list1.getCountWeight();
            }
            for (LoadLinkDemo list1 : list) {
                list1.setCountWeight(sum);
                list1.setCarNum(pageInfo.getTotal());
            }
        }
        return pageInfo;
    }

    @Override
    public List<companyDemo> selectReAndFo(HashMap parmas) {
        Object flagObj = parmas.get("flag");
        if (flagObj == null) {
            return null;
        }
        String flag = flagObj.toString();
        List<companyDemo> list = new ArrayList<>();
        //发货单位
        if (flag.equals("1")) {
            Map<String, Object> map = new HashMap<>();
            map.put("no", "FORWARDING_UNIT_NO");
            map.put("name", "FORWARDING_UNIT_NAME");
            list = appMeterWorkCarActualMapper.selectReAndFo(map);
        }
        //收货单位
        if (flag.equals("0")) {
            Map<String, Object> map = new HashMap<>();
            map.put("no", "RECEIVING_UINT_NO");
            map.put("name", "RECEIVING_UINT_NAME");
            list = appMeterWorkCarActualMapper.selectReAndFo(map);
        }
        return list;
    }

    /**
     * 装卸货确认
     *
     * @param model
     * @param type
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String UpdateForUnLoad(MeterWorkCarActualFirst model, String type) {
        if (StringUtils.isEmpty(model.getUpdateManNo()) || StringUtils.isEmpty(model.getUpdateManName())) {
            throw new MarkerMetException(500, "操作人及操作人编码不能为空！");
        }
        if (StringUtils.isEmpty(model.getPredictionNo())) {
            throw new MarkerMetException(500, "预报编号不允许为空！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("predictionNo", model.getPredictionNo());
        if ("卸货".equals(type)) {
            map.put("linkCode", "001021004");
        } else {
            map.put("linkCode", "001021007");
        }
        map.put("valueFlag", "0");
        if (StringUtils.isNotEmpty(model.getUpdateManNo())) {
            map.put("updateManNo", model.getUpdateManNo());
        }
        if (StringUtils.isNotEmpty(model.getUpdateManName())) {
            map.put("updateManName", model.getUpdateManName());
        }
        appMeterWorkCarActualMapper.updateForLink(map);
        return model.getActualFirstNo();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateForUnLoadRecover(MeterWorkCarActualFirst model, String type) {
        if (StringUtils.isEmpty(model.getUpdateManNo()) || StringUtils.isEmpty(model.getUpdateManName())) {
            throw new MarkerMetException(500, "操作人及操作人编码不能为空！");
        }
        if (StringUtils.isEmpty(model.getPredictionNo())) {
            throw new MarkerMetException(500, "预报编号不允许为空！");
        }
        List<MeterWorkPreLink> mwpls = meterWorkPreLinkService.getNextFlag(
                model.getPredictionNo(), type.equals("卸货") ? "001021004" : "001021007");
        if (ListUtils.isNotEmpty(mwpls) && mwpls.get(0).getValueFlag().equals("1")) {
            throw new MarkerMetException(500, "下一管环节" + mwpls.get(0).getLinkName() + "已完成，无法撤销");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("predictionNo", model.getPredictionNo());
        if ("卸货".equals(type)) {
            map.put("linkCode", "001021004");
        } else {
            map.put("linkCode", "001021007");
        }
        map.put("valueFlag", "1");

        String no = appMeterWorkCarActualMapper.selectForLinkUnLoad(map);
        MeterWorkPreLink meterWorkPreLink = meterWorkPreLinkMapper.selectByPrimaryKey(no);
        meterWorkPreLink.setValueFlag("0");
        meterWorkPreLink.setUpdateManName(model.getUpdateManName());
        meterWorkPreLink.setUpdateManNo(model.getUpdateManNo());
        meterWorkPreLink.setUpdateTime(new Date());
        int num = meterWorkPreLinkMapper.updateByPrimaryKeySelective(meterWorkPreLink);
        return num;
    }

    /**
     * 待卸货数据查询与已卸货数据查询
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageList<MeterWorkCarActualFirst> selectToBeDidUnLoad(HashMap parmas, Integer pageNum, Integer pageSize) {
        Object loadPointNo = parmas.get("loadPointNo");
        if (loadPointNo != null && StringUtils.isNotEmpty(loadPointNo.toString())) {
            List<String> loadPointNos;
            loadPointNos = Arrays.asList(loadPointNo.toString().replace("，", ",").split(","));
            parmas.put("loadPointNos", loadPointNos);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActual> list = appMeterWorkCarActualMapper.selectToBeDidUnLoad(parmas);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    /**
     * 待装货数据查询与已装货数据查询
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageList<MeterWorkCarActualFirst> selectToBeDidLoad(HashMap parmas, Integer pageNum, Integer pageSize) {
        Object loadPointNo = parmas.get("loadPointNo");
        if (loadPointNo != null && StringUtils.isNotEmpty(loadPointNo.toString())) {
            List<String> loadPointNos;
            loadPointNos = Arrays.asList(loadPointNo.toString().replace("，", ",").split(","));
            parmas.put("loadPointNos", loadPointNos);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActual> list = appMeterWorkCarActualMapper.selectToBeDidLoad(parmas);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateLoadPoint(MeterWorkCarActualFirst model) {
        if (model.getLoadPointNo() == null || model.getLoadPointNo().equals("")) {
            throw new MarkerMetException(500, "卸货点不能为空");
        }
        MeterWorkCarActualFirst meterWorkCarActualFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
        meterWorkCarActualFirst.setLoadPointNo(model.getLoadPointNo());
        meterWorkCarActualFirst.setLoadPointName(model.getLoadPointName());
        meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(meterWorkCarActualFirst);
        return model.getActualFirstNo();
    }

    @Override
    @Transactional
    public String judgeDuration(MeterWorkCarActualFirst model) {
        Map<String, Object> map = new HashMap<>();
        map.put("baseSpotNo", model.getBaseSpotNo());
        map.put("loadPointNo", model.getLoadPointNo());
        List<MeterBaseUnloadTime> meterBaseUnloadTimes = meterBaseUnloadTimeMapper.selectByParameters(map);
        if (meterBaseUnloadTimes.size() > 0) {
            long lDate1 = model.getCreateTime().getTime() / 1000;
            long lDate2 = System.currentTimeMillis() / 1000;
            long times = Math.round(lDate2 - lDate1);
            if (times / 60 > meterBaseUnloadTimes.get(0).getLoadTime()) {
                throw new MarkerMetException(500, "计量与卸货间隔时间超过最大值，是否重新计量");
            }
        }
        return model.getActualFirstNo();
    }

    @Override
    @Transactional
    public String abolishData(MeterWorkCarActualFirst model) {
        //修改预环节的数据(将一次计量修改为未使用)
        Map<String, Object> map = new HashMap<>();
        map.put("predictionNo", model.getPredictionNo());
        map.put("linkCode", "001021003");
        map.put("valueFlag", "1");
        int num = appMeterWorkCarActualMapper.abolishDataForLink(map);
        //将计量数据作废
        model.setValueFlag("0");
        meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(model);
        return model.getActualFirstNo();
    }

    /**
     * 卸货实绩查询-查询结净表的数据
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageList<LoadLinkDemo> selectLoadLinkActual(HashMap parmas, Integer pageNum, Integer pageSize) {
        String carNo = "";
        String startTime = "";
        String endTime = "";
        String matterNo = "";
        String forwardingUnitNo = "";
        String receivingUintNo = "";
        String loadPointName = "";
        String createManNo = "";


        Object createManNoObj = parmas.get("createManNo");
        if (createManNoObj != null) {
            createManNo = createManNoObj.toString();
        }
        Object matterNoObj = parmas.get("matterNo");
        if (matterNoObj != null) {
            matterNo = matterNoObj.toString();
        }
        Object receivingUintNoObj = parmas.get("receivingUintNo");
        if (receivingUintNoObj != null) {
            receivingUintNo = receivingUintNoObj.toString();
        }
        Object forwardingUnitNoObj = parmas.get("forwardingUnitNo");
        if (forwardingUnitNoObj != null) {
            forwardingUnitNo = forwardingUnitNoObj.toString();
        }
        Object carNoObj = parmas.get("carNo");
        if (carNoObj != null) {
            carNo = carNoObj.toString();
        }
        Object startTimeObj = parmas.get("startTime");
        if (startTimeObj != null) {
            startTime = startTimeObj.toString();
        }
        Object endTimeObj = parmas.get("endTime");
        if (endTimeObj != null) {
            endTime = endTimeObj.toString();
        }

        Object loadPointNameObj = parmas.get("loadPointName");
        if (loadPointNameObj != null) {
            loadPointName = loadPointNameObj.toString();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("matterNo", matterNo);
        map.put("forwardingUnitNo", forwardingUnitNo);
        map.put("receivingUintNo", receivingUintNo);
        map.put("loadPointName", loadPointName);
        map.put("createManNo", createManNo);

        PageHelper.startPage(pageNum, pageSize);
        List<LoadLinkDemo> list = appMeterWorkCarActualMapper.selectLoadLinkActual(map);
        PageList pageInfo = new PageList(list);
        Double sum = 0.0;
        if (list.size() > 0) {
            for (LoadLinkDemo list1 : list) {
                sum = sum + list1.getCountWeight();
            }
            for (LoadLinkDemo list1 : list) {
                list1.setCountWeight(sum);
                list1.setCarNum(pageInfo.getTotal());
            }
        }
        return pageInfo;
    }

    /**
     * 能否进行出厂
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String judgeOutDoor(PreTrackScale model) {
        if (model == null || StringUtils.isEmpty(model.getPredictionNo()) || StringUtils.isEmpty(model.getCarNo())) {
            return "禁止出厂！预报编号及车号不允许为空！";
        }
        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        if (scale == null || StringUtils.isEmpty(scale.getPredictionNo())) {
            return "禁止出厂！预报中的车号与传入车号不一致！";
        }
        if (DbConstants.NOT.equals(scale.getValueFlag())) {
            return "禁止出厂！预报未进行计量操作且不是作废的状态不允许出厂！";
        }
        MeterWorkCarActualFirst actualFirst = new MeterWorkCarActualFirst();
        actualFirst.setCarNo(model.getCarNo());
        actualFirst.setValueFlag("1");
        List<MeterWorkCarActualFirst> firstList = meterWorkCarActualFirstMapper.selectByParameters(FieldsCollector.beanToMap(actualFirst));
        if (ListUtils.isNotEmpty(firstList)) {
            return "禁止出厂！车号存在【 "+firstList.size()+" 】条有效计量数据，请先完成计量流程或者作废计量数据再出厂！";
        }
        return "允许出厂！";
    }
}
