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

import com.steerinfo.appservice.appmeterbasecarlogin.mapper.AppMeterBaseCarLoginMapper;
import com.steerinfo.appservice.appmeterbasecarlogin.model.AppMeterBaseCarLogin;
import com.steerinfo.appservice.appmeterbasecustomersupplier.mapper.AppMeterBaseCustomerSupplierMapper;
import com.steerinfo.appservice.appmeterbasecustomersupplier.model.AppMeterBaseCustomerSupplier;
import com.steerinfo.appservice.apppretrackscale.mapper.AppPreTrackScaleMapper;
import com.steerinfo.appservice.apppretrackscale.model.AppPreTrackScale;
import com.steerinfo.appservice.apppretrackscale.service.IAppPreTrackScaleService;
import com.steerinfo.appservice.dto.AppPreTrackScaleDto;
import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.meterbasecarblacelist.mapper.MeterBaseCarBlacelistMapper;
import com.steerinfo.baseinfo.meterbasecarriercustomer.mapper.MeterBaseCarrierCustomerMapper;
import com.steerinfo.baseinfo.meterbasecarrierunitcar.mapper.MeterBaseCarrierUnitCarMapper;
import com.steerinfo.baseinfo.meterbasecussupcar.mapper.MeterBaseCusSupCarMapper;
import com.steerinfo.baseinfo.meterbasecussupcar.model.MeterBaseCusSupCar;
import com.steerinfo.baseinfo.meterbasecustomersupplier.mapper.MeterBaseCustomerSupplierMapper;
import com.steerinfo.baseinfo.meterbasecustomersupplier.model.MeterBaseCustomerSupplier;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
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.mapper.MeterWorkCarActualMapper;
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.impl.MeterWorkPreLinkServiceImpl;
import com.steerinfo.meterwork.meterworkpreprocess.mapper.MeterWorkPreProcessMapper;
import com.steerinfo.pretrack.constants.DbConstants;
import com.steerinfo.pretrack.pretracknotice.mapper.PreTrackNoticeMapper;
import com.steerinfo.pretrack.pretracknotice.model.PreTrackNotice;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.pretrack.pretrackscale.service.impl.PreTrackScaleServiceImpl;
import com.steerinfo.util.DoChinFilters;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.BeanUtils;
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.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

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

    @Resource
    private MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private MeterBaseCustomerSupplierMapper meterBaseCustomerSupplierMapper;

    @Resource
    private AppPreTrackScaleMapper apppreTrackScaleMapper;

    @Resource
    private MeterWorkPreLinkMapper meterWorkPreLinkMapper;

    @Resource
    private MeterWorkPreProcessMapper meterWorkPreProcessMapper;

    @Resource
    private MeterBaseMatterInfoMapper meterBaseMatterInfoMapper;

    @Resource
    private AppMeterBaseCustomerSupplierMapper appmeterBaseCustomerSupplierMapper;

    @Resource
    private MeterBaseCarBlacelistMapper meterBaseCarBlacelistMapper;

    @Resource
    private MeterBaseCusSupCarMapper meterBaseCusSupCarMapper;

    @Resource
    private MeterBaseCarrierUnitCarMapper meterBaseCarrierUnitCarMapper;

    @Resource
    private PreTrackNoticeMapper preTrackNoticeMapper;

    @Autowired
    public MeterWorkPreLinkServiceImpl meterWorkPreLinkServiceImpl;

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Autowired
    private PreTrackScaleServiceImpl preTrackScaleService;

    @Autowired
    private MeterBaseCarrierCustomerMapper meterBaseCarrierCustomerMapper;

    @Autowired
    private ComBaseInfoMapper comBaseInfoMapper;

    /**
     * 车辆登陆表
     */
    @Autowired
    private AppMeterBaseCarLoginMapper appMeterBaseCarLoginMapper;

    @Override
    protected IBaseMapper<AppPreTrackScale, String> getMapper() {
        return apppreTrackScaleMapper;
    }

    //根据客商编号查询预报信息
    @Override
    @Transactional
    public PageList<AppPreTrackScale> doQueryApp(HashMap 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;
        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 selectPreHD(no, pageNum, pageSize);
            default:
                return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("meterTypeNos", meterTypeNos);
        map.put("customerSupplierNo", customerSupplierNo);
        map.put("receivingUintNo", receivingUintNo);

        PageHelper.startPage(pageNum, pageSize);
        List<AppPreTrackScale> list = apppreTrackScaleMapper.selectPreTrackScaleByCompany(map);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    public PageList<AppPreTrackScale> doQueryAppByCar(HashMap parmas, Integer pageNum, Integer pageSize) {
        Object carNoObj = parmas.get("carNo");
        if (carNoObj == null || "".equals(carNoObj)) {
            return null;
        }
        String carNo = carNoObj.toString();
        if (parmas.get("memo") != null && parmas.get("memo").equals("1")) {
            PageHelper.startPage(pageNum, pageSize);
            List<AppPreTrackScale> rows = apppreTrackScaleMapper.selectPreTrackScaleByCarNoAll(carNo);
            PageList pageInfo = new PageList(rows);
            return pageInfo;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<AppPreTrackScale> rows = apppreTrackScaleMapper.selectPreTrackScaleByCarNo(carNo);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }


    @Override
    public MeterWorkCarActualFirst scanAndDecryptFrist(String str, String str1) {
        MeterWorkCarActualFirst mwcaf = meterWorkCarActualFirstMapper.selectByPrimaryKey(str);
        BigDecimal bd = new BigDecimal(str1);
        if (mwcaf.getMeterWeight().compareTo(bd) == 0) {
            return mwcaf;
        } else {
            return null;
        }
    }

    @Override
    public MeterWorkCarActual scanAndDecryptActual(String str, String str1) {
        MeterWorkCarActual mwca = meterWorkCarActualMapper.selectByPrimaryKey(str);
        BigDecimal bd = new BigDecimal(str1);
        if (mwca.getNetWeight().compareTo(bd) == 0) {
            return mwca;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public PreTrackScale allocationApp(PreTrackScale model) {
        //直接调用预报的修改方法.(匹配过程相当于修改)
        if (model == null) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        if (model.getValueFlag().equals("2") || model.getValueFlag().equals("3") || model.getValueFlag().equals("4")) {
            throw new MarkerMetException(500, "该状态不允许修改!!");
        }
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        if (StringUtils.isNotEmpty(pts.getCarNo())) {
            List<MeterWorkPreLink> meterWorkPreLink = meterWorkPreLinkServiceImpl.getNextFlag(pts.getPredictionNo(), DbConstants.APPOINTMENT);
            if (meterWorkPreLink.size() > 0) {
                if (meterWorkPreLink.get(0).getValueFlag().equals("1")) {
                    throw new MarkerMetException(500, "下一步环节已执行，无法修改!!");
                }
            }
            //对预流程、预环节进行删除
            preTrackScaleService.canceleAllocation(pts);
        }
        preTrackScaleMapper.deleteByPrimaryKey(pts.getPredictionNo());
        preTrackScaleService.checkandaddApp(model);
        return model;
    }


    @Override
    @Transactional
    public PreTrackScale addByNotice(PreTrackScale model) {
        if (model == null) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String no = preTrackScaleMapper.getNewID(sdf.format(new Date()));
        model.setPredictionNo(sdf.format(new Date()) + no);
        model.setCreateTime(new Date());

        //新增一条预报
        model = preTrackScaleService.checkandadd(model);
        return model;
    }

    /**
     * APP预报查询接口
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @Transactional
    public PageList<PreTrackNotice> doQueryNoticeApp(HashMap parmas, Integer pageNum, Integer pageSize) {
        Object customerSupplierNoObj = parmas.get("customerSupplierNo");
        if (customerSupplierNoObj == null) {
            return null;
        }
        String customerSupplierNo = customerSupplierNoObj.toString();
        MeterBaseCustomerSupplier meterBaseCustomerSupplier = meterBaseCustomerSupplierMapper.selectByPrimaryKey(customerSupplierNo);
        List<String> ids = new ArrayList<>();
        if (meterBaseCustomerSupplier.getCustomerSupplierType().equals("3")) {
            ids = meterBaseCarrierCustomerMapper.selectHD(customerSupplierNo);
        } else {
            ids.add(customerSupplierNo);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<PreTrackNotice> rows = preTrackNoticeMapper.selectByCustomer(ids);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    /**
     * 通过预报：创建接口
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PreTrackScale addFroInterface(AppPreTrackScaleDto model) {
        if (model == null || StringUtils.isEmpty(model.getCarNo()) || StringUtils.isEmpty(model.getPredictionSource())) {
            throw new MarkerMetException(500, "车号、数据来源不允许为空!!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String no = preTrackScaleMapper.getNewID(sdf.format(new Date()));
        model.setPredictionNo(sdf.format(new Date()) + no);
        model.setCreateTime(new Date());

        PreTrackScale scale = new PreTrackScale();
        BeanUtils.copyProperties(model, scale);

        //新增一条预报
        if ("非计量".equals(scale.getMeterTypeName())) {
            scale = preTrackScaleService.checkandaddNotMetter(scale);
        } else {
            scale = preTrackScaleService.checkandadd(scale);
        }
        return scale;
    }

    /**
     * 通过预报接口，添加车辆、车辆及供应商匹配表、物料、收货单位、发货单位基础信息
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppPreTrackScaleDto addBaseFroInterface(AppPreTrackScaleDto model) {
        //进行基础的数据核查
        check(model);
        model.setCarNo(model.getCarNo().toUpperCase());

        //=====================物资表处理：开始======================
        MeterBaseMatterInfo matterInfo = new MeterBaseMatterInfo();
        matterInfo.setMatterName(model.getMatterName());
        List<MeterBaseMatterInfo> matterInfoList = meterBaseMatterInfoMapper.selectByParameters(FieldsCollector.beanToMap(matterInfo));
        if (ListUtils.isEmpty(matterInfoList)) {
            throw new MarkerMetException(500, "物资【" + model.getMatterName() + "】未找到对应的编码，请先维护物资基础数据!!");
            /*
            String matterNo = meterBaseMatterInfoMapper.GetNewID();
            matterInfo.setMatterNo(matterNo);
            matterInfo.setMatterName(model.getMatterName());
            matterInfo.setCreateTime(new Date());
            //有效
            matterInfo.setValidFlag("1");
            matterInfo.setCreateTime(new Date());
            matterInfo.setCreateManNo(model.getCreateManNo());
            matterInfo.setCreateManName(model.getCreateManName() + "" + model.getPredictionSource());
            meterBaseMatterInfoMapper.insert(matterInfo);
             */
        } else {
            model.setMatterNo(matterInfoList.get(0).getMatterNo());
        }
        //=====================物资表处理：开始======================

        //=====================发货单位表处理：开始======================
        MeterBaseCustomerSupplier forwardingUnit = new MeterBaseCustomerSupplier();
        forwardingUnit.setCustomerSupplierName(model.getForwardingUnitName().trim());
        List<MeterBaseCustomerSupplier> forwardingUnitList = meterBaseCustomerSupplierMapper.selectByParameters(FieldsCollector.beanToMap(forwardingUnit));
        if (ListUtils.isEmpty(forwardingUnitList)) {
            throw new MarkerMetException(500, "发货单位【" + model.getForwardingUnitName() + "】未找到对应的编码，请先维护发货单位数据!!");
            /*
            String customerNo = meterBaseCustomerSupplierMapper.GetNewID();
            forwardingUnit.setCustomerSupplierNo(customerNo);
            forwardingUnit.setCustomerSupplierPassword("123456");
            forwardingUnit.setCustomerSupplierLogin(customerNo);
            //客商类型（0:客户；1：供应商；2：客户/供应商；3：货代）;
            forwardingUnit.setCustomerSupplierType("2");
            forwardingUnit.setValidFlag("0");
            forwardingUnit.setCreateTime(new Date());
            forwardingUnit.setCreateManNo(model.getCreateManNo());
            forwardingUnit.setCreateManName(model.getCreateManName() + "" + model.getPredictionSource());
            meterBaseCustomerSupplierMapper.insertSelective(forwardingUnit);
            */
        } else {
            model.setForwardingUnitNo(forwardingUnitList.get(0).getCustomerSupplierNo());
        }
        //=====================发货单位表处理：结束======================


        //=====================收货单位表处理：开始======================
        MeterBaseCustomerSupplier receivingUnit = new MeterBaseCustomerSupplier();
        receivingUnit.setCustomerSupplierName(model.getForwardingUnitName().trim());
        List<MeterBaseCustomerSupplier> receivingUnitList = meterBaseCustomerSupplierMapper.selectByParameters(FieldsCollector.beanToMap(receivingUnit));
        if (ListUtils.isEmpty(receivingUnitList)) {
            throw new MarkerMetException(500, "收货单位【" + model.getForwardingUnitName() + "】未找到对应的编码，请先维护收货单位数据!!");
            /*
            String customerNo = meterBaseCustomerSupplierMapper.GetNewID();
            receivingUnit.setCustomerSupplierNo(customerNo);
            receivingUnit.setCustomerSupplierPassword("123456");
            receivingUnit.setCustomerSupplierLogin(customerNo);
            //客商类型（0:客户；1：供应商；2：客户/供应商；3：货代）;
            receivingUnit.setCustomerSupplierType("2");
            receivingUnit.setValidFlag("0");
            receivingUnit.setCreateTime(new Date());
            receivingUnit.setCreateManNo(model.getCreateManNo());
            receivingUnit.setCreateManName(model.getCreateManName() + "" + model.getPredictionSource());
            meterBaseCustomerSupplierMapper.insertSelective(forwardingUnit);
             */
        } else {
            model.setForwardingUnitNo(receivingUnitList.get(0).getCustomerSupplierNo());
        }
        //=====================收货单位表处理：结束======================
        if (StringUtils.isNotEmpty(model.getForwardingUnitNo()) && StringUtils.isNotEmpty(model.getForwardingUnitName())) {
            if ("001006002".equals(model.getMeterTypeNo()) || "001006005".equals(model.getMeterTypeNo())) {
                model.setCustomerSupplierNo(model.getReceivingUintNo());
                model.setCustomerSupplierName(model.getReceivingUintName());
            } else {
                model.setCustomerSupplierNo(model.getForwardingUnitNo());
                model.setCustomerSupplierName(model.getForwardingUnitName());
            }
        }

        //=====================车辆登陆表处理：开始======================
        AppMeterBaseCarLogin carLogin = appMeterBaseCarLoginMapper.selectByPrimaryKey(model.getCarNo());
        if (carLogin == null || StringUtils.isEmpty(carLogin.getCarNo())) {
            AppMeterBaseCarLogin carLoginInsert = new AppMeterBaseCarLogin();
            carLoginInsert.setCarNo(model.getCarNo());
            carLoginInsert.setPassWord("123456");
            carLoginInsert.setCreateManNo(model.getCreateManNo());
            carLoginInsert.setCreateManName(model.getCreateManName());
            carLoginInsert.setCreateTime(new Date());
            carLoginInsert.setMemo(model.getPredictionSource());
            appMeterBaseCarLoginMapper.insertSelective(carLoginInsert);
        }
        //=====================车辆登陆表处理：结束======================

        //=====================车辆用户表处理：开始======================
        MeterBaseCusSupCar cusSupCarSelect = new MeterBaseCusSupCar();
        cusSupCarSelect.setCusSupCarNo(model.getCarNo());
        cusSupCarSelect.setCustomerSupplierNo(model.getCustomerSupplierNo());
        List<MeterBaseCusSupCar> cusSupCarList = meterBaseCusSupCarMapper.selectByParameters(FieldsCollector.beanToMap(cusSupCarSelect));
        if (ListUtils.isEmpty(cusSupCarList)) {
            String maxID = comBaseInfoMapper.getConfigID("meter_base_cus_sup_car", "PK_no");
            BeanUtils.copyProperties(model, cusSupCarSelect);
            cusSupCarSelect.setPkNo(maxID);
            cusSupCarSelect.setValidFlag("0");
            cusSupCarSelect.setCreateTime(new Date());
            cusSupCarSelect.setCusSupCarNo(model.getCarNo());
            cusSupCarSelect.setMnemonicCode(model.getTrailerNo());
            meterBaseCusSupCarMapper.insertSelective(cusSupCarSelect);
        }
        //=====================车辆用户表处理：结束======================
        return model;
    }


    /**
     * 检查数据是否符合要求
     *
     * @param model
     * @throws MarkerMetException
     */
    private void check(AppPreTrackScaleDto model) throws MarkerMetException {
        if (model == null) {
            throw new MarkerMetException(500, "参数不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getPredictionType())) {
            throw new MarkerMetException(500, "预报类型(0：批次预报；1：单次预报；2：联运预报)不允许为空!!");
        }
        if (model.getPredictionType().length() > 1) {
            throw new MarkerMetException(500, "预报类型(0：批次预报；1：单次预报；2：联运预报)只允许传编码!!");
        }
        if (StringUtils.isEmpty(model.getUsedType())) {
            throw new MarkerMetException(500, "预报使用类型（可用类型（0：次数；1：时间；2：总量）不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getCarNo())) {
            throw new MarkerMetException(500, "车号不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getCreateManNo()) || StringUtils.isEmpty(model.getCreateManName())) {
            throw new MarkerMetException(500, "创建人名称及编码不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getMatterNo())) {
            throw new MarkerMetException(500, "物料编号不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getMatterName())) {
            throw new MarkerMetException(500, "物料名称不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getMeterTypeNo())) {
            throw new MarkerMetException(500, "业务类型编号不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getMeterTypeName())) {
            throw new MarkerMetException(500, "业务类型名称不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getForwardingUnitName())) {
            throw new MarkerMetException(500, "发货单位名称不允许为空!!");
        }
        if (StringUtils.isEmpty(model.getReceivingUintName())) {
            throw new MarkerMetException(500, "收货单位名称不允许为空!!");
        }
        if ("连铸坯".equals(model.getMatterName()) && model.getReceivingUintName().equals(model.getForwardingUnitName())) {
            throw new MarkerMetException(500, "物资名称为【连铸坯】，收货单位与发货单位相同，不允许做预报!!");
        }
        //预报信息制作界面中如检斤类型为厂际调拨，收发货单位为同一区域时，预报信息不允许提交成功。
        if ("001006009".equals(model.getMeterTypeNo()) || "001006006".equals(model.getMeterTypeNo())) {
            if (model.getForwardingUnitName().contains("东区") && model.getReceivingUintName().contains("东区")) {
                throw new MarkerMetException(500, "厂际调拨业务，收发货单位均包含“东区”，不允许做预报!!");
            }
            if (model.getForwardingUnitName().contains("西区") && model.getReceivingUintName().contains("西区")) {
                throw new MarkerMetException(500, "厂际调拨业务，收发货单位均包含“西区”，不允许做预报!!");
            }
        }
        if (StringUtils.isEmpty(model.getDriverName())) {
            throw new MarkerMetException(500, "司机姓名不允许为空!!");
        }
    }

    //货代登录时，获取与该货代对应的供应商下所有预报
    private PageList<AppPreTrackScale> selectPreHD(String customerSupplierNo, Integer pageNum, Integer pageSize) {
        List<String> ids = meterBaseCarrierCustomerMapper.selectHD(customerSupplierNo);
        PageHelper.startPage(pageNum, pageSize);
//        List<AppPreTrackScale> rows = apppreTrackScaleMapper.selectPreHD(ids);
        List<AppPreTrackScale> rows = apppreTrackScaleMapper.selectPreHDandCreate(customerSupplierNo, ids);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    /**
     * 非计量进出厂
     *
     * @param model
     * @param str
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String noMeterDoor(PreTrackScale model, String str) {
        if (model == null || StringUtils.isEmpty(model.getPredictionNo()) || StringUtils.isEmpty(model.getCarNo())) {
            return "禁止" + str + "！预报编号及车号不允许为空！";
        }
        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        if (scale == null || StringUtils.isEmpty(scale.getPredictionNo())) {
            return "禁止" + str + "！预报中的车号与传入车号不一致！";
        }
        if (StringUtils.isEmpty(scale.getMeterTypeNo()) || !"001006007".equals(scale.getMeterTypeNo())) {
            return "禁止" + str + "！预报类型不是“非计量”！";
        }
        switch (str) {
            case "出厂":
                if (!DbConstants.IN.equals(scale.getValueFlag()) && !DbConstants.DROP.equals(scale.getValueFlag())) {
                    return "禁止" + str + "！非计量预报不是使用中或者作废的状态！";
                }
                model.setValueFlag("2");
                break;
            case "进厂":
                if (!DbConstants.NOT.equals(scale.getValueFlag())) {
                    return "禁止" + str + "！非计量预报不是未使用状态！";
                }
                model.setValueFlag("4");
                break;
            default:
                return "禁止" + str + "！参数异常！";
        }
        model.setUpdateTime(new Date());
        model.setUpdateManNo("dil");
        model.setUpdateManName("物流" + str);
        preTrackScaleMapper.updateByPrimaryKeySelective(model);
        return "允许" + str;
    }
}
