package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BFoodProductLicenseHandle;
import com.heyu.api.baidu.request.certificate.BFoodProductLicenseRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.MapUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.certificate.FoodProductLicenseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/foodProductLicense")
@NotIntercept
public class FoodProductLicenseController extends BaseController {


    @Autowired
    private BFoodProductLicenseHandle bFoodProductLicenseHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BFoodProductLicenseRequest request) {
        FoodProductLicenseResp resp = new FoodProductLicenseResp();

        ApiR<Map> bR = bFoodProductLicenseHandle.handle(request);

        if (bR.isSuccess()) {
            Map<String, Object> data = bR.getData();


            resp.setDaySupervisionUnit(MapUtils.getByExpr(data, "words_result.日常监督管理机构"));
            resp.setDaySupervisionPerson(MapUtils.getByExpr(data, "words_result.日常监督管理人员"));
            resp.setIssuanceDate(MapUtils.getByExpr(data, "words_result.签发日期"));
            resp.setReportingPhoneNumber(MapUtils.getByExpr(data, "words_result.投诉举报电话等信息"));
            resp.setValidDateEnd(MapUtils.getByExpr(data, "words_result.有效期至"));
            resp.setLicenseNumber(MapUtils.getByExpr(data, "words_result.许可证编号"));
            resp.setLegalPerson(MapUtils.getByExpr(data, "words_result.法定代表人"));
            resp.setProductAddress(MapUtils.getByExpr(data, "words_result.社会信用代码"));
            resp.setAddress(MapUtils.getByExpr(data, "words_result.住所"));
            resp.setFoodType(MapUtils.getByExpr(data, "words_result.食品类别"));
            resp.setIssuingAuthority(MapUtils.getByExpr(data, "words_result.发证机关"));
            resp.setIssuingPerson(MapUtils.getByExpr(data, "words_result.签发人"));
            resp.setProductName(MapUtils.getByExpr(data, "words_result.生产者名称"));

            return R.ok().setData(resp);
        }
        return R.error();

    }

}
