package com.steerinfo.meterwork.meterworkznlc.service.impl;

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.datasource.DataSourceKey;
import com.steerinfo.framework.datasource.TargetDataSource;
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.meterworkznlc.mapper.MeterWorkZnlcMapper;
import com.steerinfo.meterwork.meterworkznlc.service.IMeterWorkZnlcService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @author Bourne.Cao
 * @date 2022-08-06 10:46
 */
@Service(value = "MeterWorkZnlcService")
public class MeterWorkZnlcService implements IMeterWorkZnlcService {

    @Resource
    private MeterWorkZnlcMapper meterWorkZnlcMapper;
    @Resource
    MeterWorkCarActualMapper meterWorkCarActualMapper;
    @Resource
    MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Override
    public Boolean getNewActual(RESTfulResult rm, HashMap<String, Object> map, HashMap<String, Object> param) {
        //根据委托号查询结净数据（新系统）
        List<MeterWorkCarActual> actualList = meterWorkCarActualMapper.selectByParameters(map);
        if(actualList.size() >= 1) {
            param.put("DeviceNo", actualList.get(0).getNetSpot3No());    //计量设备号
            param.put("WeightVoucherNo", actualList.get(0).getPredictionNo()); //检斤委托号
            param.put("ConveyanceNo", actualList.get(0).getCarNo());   //车牌号
            param.put("GrossWeight", actualList.get(0).getGrossWeight().divide(new BigDecimal(1000)));    //过磅量
            param.put("TareWeight", actualList.get(0).getTareWeight().divide(new BigDecimal(1000)));     //空车重
            param.put("NetWeight", actualList.get(0).getNetWeight().divide(new BigDecimal(1000)));      //过磅净重
            param.put("MeteringTime", actualList.get(0).getNetTime());   //计量时刻
            param.put("MetringPerson", actualList.get(0).getNetManName());  //计量人
            rm.setData(param);
            return true;
        }else {
            //根据委托号查询一次数据（新系统）
            List<MeterWorkCarActualFirst> firstList = meterWorkCarActualFirstMapper.selectByParameters(map);
            if(firstList.size() >= 1) {
                param.put("DeviceNo", firstList.get(0).getBaseSpotNo());    //计量设备号
                param.put("WeightVoucherNo", firstList.get(0).getPredictionNo()); //检斤委托号
                param.put("ConveyanceNo", firstList.get(0).getCarNo());   //车牌号
                param.put("GrossWeight", firstList.get(0).getMeterWeight().divide(new BigDecimal(1000)));    //过磅量
                param.put("TareWeight", null);     //空车重
                param.put("NetWeight", null);      //过磅净重
                param.put("MeteringTime", firstList.get(0).getCreateTime());   //计量时刻
                param.put("MetringPerson", firstList.get(0).getCreateManName());  //计量人

                rm.setData(param);
                return true;
            }
        }
        return false;
    }


    @Override
    @TargetDataSource(dataSourceKey = DataSourceKey.DB_SLAVE1)
    public Boolean getOldActual(RESTfulResult rm, HashMap<String, Object> map, HashMap<String, Object> param) {
        List<HashMap> oldActuals = meterWorkZnlcMapper.doQueryActualOld(map);
        if(oldActuals.size() >= 1) {
            param.put("DeviceNo", oldActuals.get(0).get("DEVICENO"));    //计量设备号
            param.put("WeightVoucherNo", oldActuals.get(0).get("WEIGHTVOUCHERNO")); //检斤委托号
            param.put("ConveyanceNo", oldActuals.get(0).get("CONVEYANCENO").toString().replaceAll("-", ""));   //车牌号
            param.put("GrossWeight", oldActuals.get(0).get("GROSSWEIGHT"));    //过磅量
            param.put("TareWeight", oldActuals.get(0).get("TAREWEIGHT"));     //空车重
            param.put("NetWeight", oldActuals.get(0).get("NETWEIGHT"));      //过磅净重
            param.put("MeteringTime", oldActuals.get(0).get("METERINGTIME"));   //计量时刻
            param.put("MetringPerson", oldActuals.get(0).get("METRINGPERSON"));  //计量人
            rm.setData(param);

            return true;
        }
        else {
            List<HashMap> oldFirsts = meterWorkZnlcMapper.doQueryFirstOld(map);
            if(oldFirsts.size() >= 1) {
                param.put("DeviceNo", oldFirsts.get(0).get("DEVICENO"));    //计量设备号
                param.put("WeightVoucherNo", oldFirsts.get(0).get("WEIGHTVOUCHERNO")); //检斤委托号
                param.put("ConveyanceNo", oldFirsts.get(0).get("CONVEYANCENO").toString().replaceAll("-", ""));   //车牌号
                param.put("GrossWeight", oldFirsts.get(0).get("GROSSWEIGHT"));    //过磅量
                param.put("TareWeight", null);     //空车重
                param.put("NetWeight", null);      //过磅净重
                param.put("MeteringTime", oldFirsts.get(0).get("METERINGTIME"));   //计量时刻
                param.put("MetringPerson", "老系统一次");  //计量人

                rm.setData(param);
                return true;
            }
        }
        return false;
    }
}
