package com.dxhy.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
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.utils.R;
import com.dxhy.core.dao.CoreSysEnterpriseConfigCoreDao;
import com.dxhy.core.service.singletable.CoreInvoiceQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

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

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

    @Resource
    private CoreInvoiceQueryService coreInvoiceQueryService;

    @Resource
    private CoreSysEnterpriseConfigCoreDao coreSysEnterpriseConfigCoreDao;

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

        pramsMap.put("company", getUserInfo().getCompany());

        List<SysEnterpriseConfigEntity> sysEnterpriseConfigEntities = coreSysEnterpriseConfigCoreDao.selectList(new LambdaQueryWrapper<SysEnterpriseConfigEntity>()
                .eq(SysEnterpriseConfigEntity::getJrfs, "0")
                .eq(SysEnterpriseConfigEntity::getCompany, getUserInfo().getCompany())
        );
        if (sysEnterpriseConfigEntities.size() > 0) {
            pramsMap.put("taxNo", sysEnterpriseConfigEntities.get(0).getNsrsbh());
        }

        //List<String> gfshAll = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
//        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) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
