package com.dxhy.core.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.ExceptionUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UUIDUtils;
import com.dxhy.common.utils.R;
import com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.service.dataupload.ISmDataUploadService;

import lombok.extern.slf4j.Slf4j;

/**
 * 扫描仪识别数据上传接口
 *
 * @author user
 */
@RestController
@RequestMapping("scan")
@Slf4j

public class SmDataUploadController extends AbstractController {

    @Resource
    private ISmDataUploadService dataUploadService;

    @Resource
    private ScheduleJobService scheduleJobService;

    @Value("${url.websokiturl}")
    private String websokiturl;

    /**
     * 扫描数据上传接口
     *
     * @param scanData
     *            扫描数据
     * @return 返回
     *
     */
    @PostMapping("/data/upload")
    @ResponseBody
    public ResponseEntity<?> scanDataUpload(@RequestBody String scanData) {
        try {
            dataUploadService.saveScanData(scanData);
            return ResponseEntity.ok(R.ok());
        } catch (Exception e) {
            return ResponseEntity.ok(R.error(ExceptionUtil.getExceptionDeteil(e, 0)));
        }
    }

    /**
     * 扫描数据上传接口
     *
     * @return
     *
     */
    @GetMapping("/get/scaninfo")
    @ResponseBody
    public ResponseEntity<?> getScanInfo() {

        try {
            String dbName = getUserInfo().getDbName();
            if (StringHelper.isBlank(dbName)) {
                dbName = "business";
            }
            String userAccout = getLoginName();
            // 根据用户id 获取 识别配置
            String scanType = scheduleJobService.getUserScanType(Long.valueOf(getUserInfo().getId()));
            Map<String, String> scanInfo = new HashMap<>(3);
            String serial = userAccout + "-" + System.currentTimeMillis() + "-" + UUIDUtils.generateShortUuid()
                + UUIDUtils.generateShortUuid();
            scanInfo.put("Serial", serial);
            scanInfo.put("Database", dbName);
            scanInfo.put("websokiturl", websokiturl);
            scanInfo.put("scanType", scanType);
            return ResponseEntity.ok(R.ok().put("data", scanInfo));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(ExceptionUtil.getExceptionDeteil(e, 0)));
        }
    }

    /**
     * 发票图片上传方法
     *
     * @param file
     *            文件
     * @param database
     *            数据源
     * @return 返回结果
     */
    @ResponseBody
    @RequestMapping("/uploadImg")
    public String uploadImg(MultipartFile file, String database) {
        if (file != null) {
            return dataUploadService.uploadImg(file, database);
        } else {
            return "fail";
        }
    }

    /**
     * 海关缴款书图片上传方法
     *
     * @param file
     *            文件
     * @param database
     *            数据源
     * @return 返回结果
     */
    @ResponseBody
    @RequestMapping("/uploadCustomsImg")
    public String uploadCustomsImg(MultipartFile file, String database, String pch) {
        if (file != null) {
            return dataUploadService.uploadCustomsImg(file, database, pch);
        } else {
            return "fail";
        }

    }
}
