package com.dxhy.core.controller;

import com.dxhy.api.model.config.Tax;
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.jxpt.baseService.sign.service.fpqs.ISmDataUploadService;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class SmDataUploadController extends AbstractController {

    @Resource
    private ISmDataUploadService dataUploadService;


    @Value("${jxpt.url}")
    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();
            List<Tax> orgList = getUserInfo().getOrg();
            String userSh = orgList.size()==0?"":orgList.get(0).getTaxno();
            String company = getUserInfo().getCompany();
            // 根据用户id 获取 识别配置
            String scanType = dataUploadService.getScanType(getUserInfo().getCompany());
            Map<String, String> scanInfo = new HashMap<>(3);
            String serial = userAccout + "-" + userSh + "-"+ company + "-"+ 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";
        }

    }
}
