package com.dxhy.base.service.tdxrecordinvoice;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

import javax.annotation.Resource;

import com.dxhy.base.utils.InvoiceTypeUtils;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.enums.FpztEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.base.dao.BaseFprzDao;
import com.dxhy.base.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.base.dao.BaseTDxTaxCurrentDao;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.base.entity.BaseTDxRecordInvoice;
import com.dxhy.base.entity.BaseTDxTaxCurrent;
import com.dxhy.base.model.BaseFpsmrzRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.vo.Tax;

/**
 * 发票扫码认证业务实现类
 *
 * @author zhanghong
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaLowerCamelCaseVariableNaming",
        "AlibabaCollectionInitShouldAssignCapacity"})
@Service("baseIFpsmrzService")
public class BaseFpsmrzServiceImpl extends MpBaseServiceImpl<BaseFprzDao, BaseFpsmrzRespVO>
        implements BaseIFpsmrzService {
    @Resource
    private BaseTDxRecordInvoiceDao baseTDxRecordInvoiceDao;

    @Resource
    private BaseTDxTaxCurrentDao baseTDxTaxCurrentDao;
    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String selectBySmrzCxtj(Map<String, Object> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        String company = (String) pramsMap.get("company");
        String menuId = (String) pramsMap.get("menuId");
        JSONObject json = tabulateRedisService.getTabulateMenu(company, menuId);
        QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        String fpdm = (String) pramsMap.get("fpdm");
        String uuid = (StringUtils.isBlank(fpdm) ? "" : fpdm) + (String) pramsMap.get("fphm");
        queryWrapper.eq("uuid", uuid);
        BaseTDxRecordInvoice rdxRecordInvoice = baseTDxRecordInvoiceDao.selectOne(queryWrapper);
        String qsflag = (String) pramsMap.get("qsflag");
        List<Object> returnList = new ArrayList<>();
        String invoiceType = (String) pramsMap.get("invoiceType");
        BaseFpsmrzRespVO rzlb;
        if (rdxRecordInvoice != null) {
            rzlb = this.convertToRzlbView(rdxRecordInvoice);
            rzlb.setSelect(true);
            rzlb.setFpdm(rdxRecordInvoice.getInvoiceCode());
            rzlb.setFphm(rdxRecordInvoice.getInvoiceNo());
            rzlb.setEleInvoiceNo(rdxRecordInvoice.getEleInvoiceNo());
            String kprq = (String) pramsMap.get("kprq");
            String je = (String) pramsMap.get("je");
            String jym = (String) pramsMap.get("jym");
            rzlb.setKprq(kprq);
            rzlb.setJe(je);
            rzlb.setJym(jym);
            rzlb.setInvoiceType(rdxRecordInvoice.getInvoiceType());
            boolean krz = InvoiceTypeUtils.isKrz(rdxRecordInvoice.getInvoiceType(), rdxRecordInvoice.getLqTdyslxdm());
            if (!krz) {
                rzlb.setBkrzts("发票类型有误");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            List<String> list = (List<String>) pramsMap.get("gfsh");
            List<Tax> taxList = (List<Tax>) pramsMap.get("taxList");
            List<String> taxnoList = new ArrayList<>();
            if (taxList != null && taxList.size() > 0) {
                for (Tax tax : taxList) {
                    String upgradeStatus = tax.getUpgradeStatus();
                    if ("1".equals(upgradeStatus)) {
                        taxnoList.add(tax.getTaxno());
                    }
                }
            }
            if (taxnoList.contains(rdxRecordInvoice.getGfTaxNo())) {
                rzlb.setUpgradeStatus("1");
                if (rdxRecordInvoice.getDkTaxAmount() == null) {
                    rzlb.setDkse(MathUtil.round(rdxRecordInvoice.getTaxAmount().toPlainString()));
                } else {
                    rzlb.setDkse(MathUtil.round(rdxRecordInvoice.getDkTaxAmount().toPlainString()));
                }
            } else {
                rzlb.setUpgradeStatus("0");
                if (rdxRecordInvoice.getDkTaxAmount() == null) {
                    rzlb.setDkse(AmountFormatUtil
                            .fmtMicrometer(MathUtil.round(rdxRecordInvoice.getTaxAmount().toPlainString())));
                } else {
                    rzlb.setDkse(AmountFormatUtil
                            .fmtMicrometer(MathUtil.round(rdxRecordInvoice.getDkTaxAmount().toPlainString())));
                }
            }
            QueryWrapper<BaseTDxTaxCurrent> queryCurrent = new QueryWrapper<>();
            queryCurrent.eq("taxno", rdxRecordInvoice.getGfTaxNo());

            BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(queryCurrent);
            if (list == null || !list.contains(rdxRecordInvoice.getGfTaxNo())) {
                rzlb.setBkrzts("无该发票购方税号权限");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if (current != null && current.getCurrentTaxPeriod() != null) {
                rzlb.setDqskssq(current.getCurrentTaxPeriod());
            } else {
                rzlb.setBkrzts("无该发票税号属期信息");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if ("1".equals(qsflag) && !"1".equals(rdxRecordInvoice.getQsStatus())) {
                rzlb.setBkrzts("该发票未签收");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            Map<String, Object> curMap = new HashMap<>();
            List<String> gfshList = new ArrayList<>();
            gfshList.add(rdxRecordInvoice.getGfTaxNo());
            curMap.put("gfsh", gfshList);
            List<BaseTDxDkCount> curList = baseTDxRecordInvoiceDao.selectDksh(curMap);
            for (BaseTDxDkCount cur : curList) {
                String skssq = cur.getSkssq();
                String year = skssq.substring(0, 4);
                String mon = skssq.substring(4);
                LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(mon), 1);
                LocalDate localDate1 = localDate.plusMonths(1);
                LocalDate localDate2 = LocalDate.now();
                if (!"0".equals(cur.getTjStatus()) && !"3".equals(cur.getTjStatus())
                        && !localDate2.isBefore(localDate1)) {
                    rzlb.setBkrzts("该发票的购方税号已申请抵扣统计");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                }
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String invoiceDate = sdf.format(rdxRecordInvoice.getInvoiceDate());
            if (!kprq.equals(invoiceDate)) {
                rzlb.setBkrzts("开票日期与底账数据不一致");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if (StringUtils.isNotBlank(je)) {
                je = MathUtil.round(je);
                if (!je
                        .equals(rdxRecordInvoice.getInvoiceAmount().setScale(2, BigDecimal.ROUND_DOWN).toPlainString())) {
                    rzlb.setBkrzts("金额与底账数据不一致");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                }
                rzlb.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(je)));
            } else {
                rzlb.setJe(AmountFormatUtil
                        .fmtMicrometer(MathUtil.round(rdxRecordInvoice.getInvoiceAmount().toPlainString())));
            }
            if (StringUtils.isNotBlank(jym)) {
                if (jym.equals(rdxRecordInvoice.getCheckCode())) {
                    rzlb.setJym(rdxRecordInvoice.getCheckCode());
                }
            }

//            if ("1".equals(rdxRecordInvoice.getInvoiceStatus())) {
//                rzlb.setBkrzts("失控发票");
//                rzlb.setSelect(false);
//                returnList.add(rzlb);
//                json.put("datalist", returnList);
//                return super.responseResult(json);
//            } else if ("2".equals(rdxRecordInvoice.getInvoiceStatus())) {
//                rzlb.setBkrzts("作废发票");
//                rzlb.setSelect(false);
//                returnList.add(rzlb);
//                json.put("datalist", returnList);
//                return super.responseResult(json);
//            } else if ("3".equals(rdxRecordInvoice.getInvoiceStatus())) {
//                rzlb.setBkrzts("红冲发票");
//                rzlb.setSelect(false);
//                returnList.add(rzlb);
//                json.put("datalist", returnList);
//                return super.responseResult(json);
//            } else if ("4".equals(rdxRecordInvoice.getInvoiceStatus())) {
//                rzlb.setBkrzts("异常发票");
//                rzlb.setSelect(false);
//                returnList.add(rzlb);
//                json.put("datalist", returnList);
//                return super.responseResult(json);
//            }
            String invoiceStatus = "07";
            if (!invoiceStatus.contains(rdxRecordInvoice.getInvoiceStatus())) {
                for (FpztEnum e : FpztEnum.values()) {
                    if (rdxRecordInvoice.getInvoiceStatus().equals(e.getFpztDm())) {
                        rzlb.setBkrzts(e.getFpztMc(e.getIndex()) + "发票");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        json.put("datalist", returnList);
                        return super.responseResult(json);
                    }
                }
            }
            if ("1".equals(rdxRecordInvoice.getRzhYesorno())) {
                rzlb.setBkrzts("该发票已认证抵扣");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if (current.getSelectStartDate() != null && rdxRecordInvoice.getInvoiceDate() != null) {
                if (rdxRecordInvoice.getInvoiceDate()
                        .compareTo(DateUtils.strToDatePeriod(current.getSelectStartDate())) < 0) {
                    rzlb.setBkrzts("发票逾期");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                }
            }
            if (StringUtils.isNotBlank(current.getSelectEndDate())) {
                if (rdxRecordInvoice.getInvoiceDate()
                        .compareTo(DateUtils.strToDatePeriod(current.getSelectEndDate())) > 0) {
                    rzlb.setBkrzts("发票未到期");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                }
            }
            if ("1".equals(current.getLatchStatus())) {
                rzlb.setBkrzts("该发票购方税号已锁定");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if ("1".equals(rdxRecordInvoice.getBdkStatus())) {
                if ("1".equals(rdxRecordInvoice.getAuthStatus())) {
                    rzlb.setBkrzts("该发票已勾选未确认");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                } else if ("2".equals(rdxRecordInvoice.getAuthStatus())) {
                    rzlb.setBkrzts("该发票已确认");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                } else if ("3".equals(rdxRecordInvoice.getAuthStatus())) {
                    rzlb.setBkrzts("该发票已发送认证");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                } else if ("4".equals(rdxRecordInvoice.getAuthStatus())) {
                    rzlb.setBkrzts("该发票已认证抵扣");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    json.put("datalist", returnList);
                    return super.responseResult(json);
                }
            }

            if ("2".equals(rdxRecordInvoice.getBdkStatus()) && "1234".contains(rdxRecordInvoice.getAuthStatus())) {
                rzlb.setBkrzts("已认证不抵扣");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if ("3".equals(rdxRecordInvoice.getBdkStatus()) && "1234".contains(rdxRecordInvoice.getAuthStatus())) {
                rzlb.setBkrzts("已做退税认证");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }
            if ("1".equals(rdxRecordInvoice.getExportMark())) {
                rzlb.setBkrzts("该发票为出口转内销发票");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                json.put("datalist", returnList);
                return super.responseResult(json);
            }

        } else {
            rzlb = new BaseFpsmrzRespVO();
            if (pramsMap.get("fpdm") != null)
                rzlb.setFpdm(pramsMap.get("fpdm").toString());
            rzlb.setFphm(pramsMap.get("fphm").toString());
            rzlb.setKprq(pramsMap.get("kprq").toString());
            rzlb.setJe(pramsMap.get("je") != null ? MathUtil.round(pramsMap.get("je").toString()) : "");
            rzlb.setJym(pramsMap.get("jym") != null ? pramsMap.get("jym").toString() : "");
            rzlb.setBkrzts("无底账信息");
            rzlb.setSelect(false);
            rzlb.setUpgradeStatus("0");
            rzlb.setInvoiceType(invoiceType);
        }
        returnList.add(rzlb);
        json.put("datalist", returnList);
        return super.responseResult(json);

    }

    /**
     * 转换结果数据为页面可展示表格
     *
     * @param ri 待转换对象
     * @return 返回结果
     */
    private BaseFpsmrzRespVO convertToRzlbView(BaseTDxRecordInvoice ri) {
        BaseFpsmrzRespVO rzlb = new BaseFpsmrzRespVO();
        rzlb.setId(ri.getId() + "");

        rzlb.setXh(String.valueOf(1));
        rzlb.setBkrzts("");
        if (StringUtils.isNotBlank(ri.getCheckCode()) && ri.getCheckCode().length() > 6) {
            rzlb.setJym(ri.getCheckCode().substring(ri.getCheckCode().length() - 6));
        }
        rzlb.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(ri.getTaxAmount().toPlainString())));
        if (ri.getDkTaxAmount() == null) {
            rzlb.setDkse(MathUtil.round(ri.getTaxAmount().toPlainString()));
        } else {
            rzlb.setDkse(MathUtil.round(ri.getDkTaxAmount().toPlainString()));

        }
        return rzlb;
    }

}
