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 com.steerinfo.meterwork.meterworkznlc.service.IMeterWorkZnlcService;
import com.steerinfo.meterwork.meterworkznlc.service.impl.MeterWorkZnlcService;
import com.steerinfo.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 {

    @Autowired
    MeterWorkZnlcService meterWorkZnlcService;

    @Resource
    MeterBaseCarMapper meterBaseCarMapper;

    @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 = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        rm.setMessage("操作成功！！");
        if(StringUtils.isEmpty(weightVoucherNo)) {
            rm.setData(null);
            return rm;
        }

        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        map.put("predictionNo", weightVoucherNo);
        if(meterWorkZnlcService.getNewActual(rm, map, param)) return rm;
        else if (meterWorkZnlcService.getOldActual(rm, map, param)) return rm;

        return rm;
    }

}
