package com.steerinfo.meterwork.meterworkznlc.controller;


import com.steerinfo.baseinfo.meterbasecar.mapper.MeterBaseCarMapper;
import com.steerinfo.baseinfo.meterbasecar.model.MeterBaseCar;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
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 io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/${api.version}/MeterWorkZnlc")
public class MeterWorkZnlcController extends BaseRESTfulController {
    @Resource
    MeterBaseCarMapper meterBaseCarMapper;
    @Resource
    MeterWorkCarActualMapper meterWorkCarActualMapper;
    @Resource
    MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @ApiOperation(value="根据车号返回是否允许计量", notes="是否允许计量")
    @PostMapping(value = "/HasPermissionGivenByJL")
    public RESTfulResult HasPermissionGivenByJL(@RequestBody String conveyanceNo) {
        RESTfulResult rm = success();
        rm.setCode("200");
        rm.setMessage("操作成功！！");
        rm.setData(true);
        return rm;
    }

    @ApiOperation(value="发送计量车辆就位信息", notes="发送计量车辆就位信息")
    @PostMapping(value = "/SendTruckInPlaceToJL")
    public RESTfulResult SendTruckInPlaceToJL(@RequestBody HashMap model){
        RESTfulResult rm = success();
        rm.setCode("200");
        rm.setMessage("操作成功！！");
        rm.setData(true);
        return rm;
    }

    @ApiOperation(value="根据车号查询车辆信息", notes="车辆是否备案")
    @PostMapping(value = "/IsRecorded")
    public RESTfulResult IsRecorded(@RequestBody String conveyanceNo) {
        RESTfulResult rm = success();
        rm.setCode("200");
        rm.setMessage("操作成功！！");

        if(com.steerinfo.util.StringUtils.isEmpty(conveyanceNo)) {
            rm.setData(false);
            return rm;
        }

        HashMap<String, Object> map = new HashMap();
        map.put("carNo", conveyanceNo);
        List<MeterBaseCar> list = meterBaseCarMapper.selectByParameters(map);
        rm.setData(list.size() >= 1 ? true : false);
        return rm;
    }

    @ApiOperation(value="根据检斤号查询称重信息", notes="根据检斤号查询称重信息")
    @PostMapping(value = "/GetWeightFromJL")
    public RESTfulResult GetWeightFromJL(@RequestBody String weightVoucherNo) {
        RESTfulResult rm = success();
        rm.setCode("200");
        rm.setMessage("操作成功！！");

        if(com.steerinfo.util.StringUtils.isEmpty(weightVoucherNo)) {
            rm.setData(null);
            return rm;
        }

        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        map.put("predictionNo", weightVoucherNo);

        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());    //过磅量
            param.put("TareWeight", actualList.get(0).getTareWeight());     //空车重
            param.put("NetWeight", actualList.get(0).getNetWeight());      //过磅净重
            param.put("MeteringTime", actualList.get(0).getNetTime());   //计量时刻
            param.put("MetringPerson", actualList.get(0).getNetManName());  //计量人
            rm.setData(param);
            return rm;
        }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());    //过磅量
                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 rm;
            }
        }
        rm.setData(null);
        return rm;
    }

}
