package com.dxhy.core.controller;

import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.util.InvoiceUtil;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import com.dxhy.core.job.service.InvoiceInterfaceService;
import com.dxhy.core.job.service.SupplierRiskService;
import com.dxhy.core.service.singletable.CoreInvoiceQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 发票查验模块接口类
 *
 * @author user
 */

@RestController
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class InvoiceQueryController extends AbstractController {

    @Resource
    private CoreInvoiceQueryService coreInvoiceQueryService;

    @Resource
    SupplierRiskService supplierRiskService;

    @Resource
    InvoiceInterfaceService invoiceInterfaceService;

    /**
     * 扫码查验发票
     *
     * @return
     *
     */
    @PostMapping("/smcy/info")
    @ResponseBody
    @SysLog("发票扫码查验")
    public ResponseEntity<?> smQueryInvoice(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        log.info("传入的参数为:{}",pramsMap);
        String userid = getUserName();
        pramsMap.put("userid", userid);
        String dbName = getUserInfo().getDbName();
        pramsMap.put("dbName", dbName);

        pramsMap.put("company", getUserInfo().getCompany());
        List<String> gfshAll = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        //去除里面的空字符串
        gfshAll = gfshAll.stream().filter(str -> !str.trim().isEmpty()).collect(Collectors.toList());
        log.info("gfshAll={}",gfshAll);
        if (gfshAll.size() > 0) {
            pramsMap.put("taxNo", gfshAll.get(0));

        }
        InvoiceUtil iu = new InvoiceUtil(pramsMap.get("invoiceCode"));
//        if(FplxEnum.QDZZP.getFplxDm().equals(pramsMap.get("invoiceType"))
//                || FplxEnum.QDPP.getFplxDm().equals(pramsMap.get("invoiceType"))){
//            pramsMap.put("invoiceType", pramsMap.get("invoiceType"));
//        }else {
//            if (pramsMap.get("invoiceCode").isEmpty()) {
//
//                return ResponseEntity.ok(R.error("发票代码不能为空！"));
//            }
//            pramsMap.put("invoiceType", iu.getFplxdm());
//        }

        if (pramsMap.get("invoiceNo").isEmpty()) {
            return ResponseEntity.ok(R.error("发票号码不能为空！"));
        }

        if (pramsMap.get("saveHistory") == null || pramsMap.get("saveHistory").isEmpty()) {
            pramsMap.put("saveHistory", "Y");
        }

        if (!pramsMap.get("invoiceDate").isEmpty()) {
            iu.setKprq(pramsMap.get("invoiceDate"));
            if (!iu.kprqValid(null)) {
                pramsMap.put("invoiceDate", "开票日期格式错误！");
            }
        } else {
            return ResponseEntity.ok(R.error("开票日期不能为空！"));
        }

        if (FplxEnum.ZP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.JDC.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.DZZP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.QDZZP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.QDPP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.JDCDZ.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.HKDZFP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.TLKP.getFplxDm().equals(pramsMap.get("invoiceType"))
                || FplxEnum.ZZDZ.getFplxDm().equals(pramsMap.get("invoiceType"))) {
            if (pramsMap.get("invoiceAmount").isEmpty()) {
                return ResponseEntity.ok(R.error("金额不能为空！"));
            }
        } else {
//            if (pramsMap.get("invoiceCode").isEmpty()) {
//
//                return ResponseEntity.ok(R.error("发票代码不能为空！"));
//            }
            pramsMap.put("invoiceType", iu.getFplxdm());
            if (pramsMap.get("checkCode").isEmpty()) {
                return ResponseEntity.ok(R.error("校验码不能为空！"));
            }
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", coreInvoiceQueryService.smQueryInvoice(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 扫码查验发票
     *
     * @return
     *
     */
    @PostMapping("/smcy/supplier")
    @ResponseBody
    @SysLog("供应商风险")
    public String supplierRisk(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return "";
            }
        }
        data.put("gfsh", gfshList);
        data.put("taxNo", pramsMap.get("taxNo"));
        data.put("taxName", pramsMap.get("taxName"));
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        data.put("currentTime", date);

        String riskSupplierVOS = supplierRiskService.selectRiskSupplier2(data);
        return riskSupplierVOS;

    }

}
