package com.xhpc.user.controller;

import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponse;
import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponseBody;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.user.domain.XhpcUserVehicle;
import com.xhpc.user.service.IXhpcUserVehicleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2023-10-19 17:26
 */
@RestController
@RequestMapping("/xhpc/userVehicle")
public class XhpcUserVehicleConeroller extends BaseController {

    public static final String accessKeyId = "LTAI5t6Nr7MdNjMN4xNvgo1W";
    public static final String accessKeySecret = "hXQopkhlny10aiBBEFia2dHu6RHCqV";

    @Resource
    IXhpcUserVehicleService xhpcUserVehicleService;

    @ApiOperation("添加车牌号")
    @PostMapping("/addUserVehicle")
    public R addUserVehicle(@RequestBody XhpcUserVehicle xhpcUserVehicle){
        return xhpcUserVehicleService.addUserVehicle(xhpcUserVehicle);
    }


    //小程序用户车牌列表
    @GetMapping("/getUserVehicleList")
    public TableDataInfo getUserVehicleList(HttpServletRequest request,Long appUserId,Integer source) {
        List<Map<String, Object>> list = xhpcUserVehicleService.getUserVehicleList(request,appUserId,source);
        return getDataTable(list);
    }




    //后端
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, String vehicleName, String phone,String chargingStationName) {
        List<Map<String, Object>> list = xhpcUserVehicleService.list(request,vehicleName,phone,chargingStationName);
        return getDataTable(list);
    }

    @ApiOperation("阿里云自动识别行驶证")
    @GetMapping("/addUserVehicleUrl")
    public R addUserVehicleUrl(String url,Long appUserId,Integer source){
        try {
            com.aliyun.ocr20191230.Client client = createClient(accessKeyId, accessKeySecret);
            URL urls = new URL(url);
            InputStream inputStream = urls.openConnection().getInputStream();
            com.aliyun.ocr20191230.models.RecognizeDrivingLicenseAdvanceRequest recognizeDrivingLicenseAdvanceRequest = new com.aliyun.ocr20191230.models.RecognizeDrivingLicenseAdvanceRequest()
                    .setImageURLObject(inputStream)
                    .setSide("face");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            RecognizeDrivingLicenseResponse response = client.recognizeDrivingLicenseAdvance(recognizeDrivingLicenseAdvanceRequest, runtime);
            if(response.getStatusCode()==200){
                RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataFaceResult faceResult = response.getBody().getData().getFaceResult();

                XhpcUserVehicle xhpcUserVehicle=new XhpcUserVehicle();
                xhpcUserVehicle.setOwner(faceResult.getOwner());
                xhpcUserVehicle.setVehicleName(faceResult.getPlateNumber());
                xhpcUserVehicle.setEngineNumber(faceResult.getEngineNumber());
                xhpcUserVehicle.setAddress(faceResult.getAddress());
                xhpcUserVehicle.setVehicleType(faceResult.getVehicleType());
                xhpcUserVehicle.setModel(faceResult.getModel());
                xhpcUserVehicle.setRegisterDate(faceResult.getRegisterDate());
                xhpcUserVehicle.setLssueDate(faceResult.getRegisterDate());
                xhpcUserVehicle.setUseCharacter(faceResult.getUseCharacter());
                xhpcUserVehicle.setVinSpecCode(faceResult.getVin());
                xhpcUserVehicle.setAppUserId(appUserId);
                xhpcUserVehicle.setSource(source);
                xhpcUserVehicle.setDelFlag(1);

                xhpcUserVehicleService.addUserVehicle(xhpcUserVehicle);

                Map<String,Object> map =new HashMap<>();
                map.put("vinSpecCode",faceResult.getVin());
                map.put("vehicleName",faceResult.getPlateNumber());
                map.put("vehicleId",xhpcUserVehicle.getVehicleId());
                return R.ok(map);
            }else{
                return R.fail("请重新上传行驶证");
            }
        }catch (Exception e){

        }
        return R.fail("请重新上传行驶证");
    }



    public static com.aliyun.ocr20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
        return new com.aliyun.ocr20191230.Client(config);
    }
}
