package com.dxhy.core.service.export.impl;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.invoice.TDxRecordInvoice;
import com.dxhy.api.model.sign.TDxInvoice;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.CoreTDxInvoiceDao;
import com.dxhy.core.dao.CoreTDxRecordInvoiceDao;
import com.dxhy.core.model.SignQueryVO;
import com.dxhy.core.service.export.IFpqsService;
import com.dxhy.core.service.singletable.CoreInvoiceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 发票签收业务逻辑处理实现类
 *
 * @author user
 */

@Service("corefpqsService")
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class FpqsServiceImpl extends MpBaseServiceImpl<CoreTDxRecordInvoiceDao, TDxRecordInvoice> implements IFpqsService {

    @Resource
    private CoreInvoiceService invoiceSmService;

    @Resource
    private CoreTDxInvoiceDao invoiceDao;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectQsfpList(Map<String, Object> pramsMap, int curr, int size) {

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject json = new JSONObject();

        PageHelper.startPage(curr, size);
        List<TDxInvoice> pageList = new ArrayList<>();
        if (pramsMap.get("gxStatus") != null && !"".equals(pramsMap.get("gxStatus"))) {
            pageList = invoiceDao.selectQsfpListJrtt(pramsMap);
        } else {
            pageList = invoiceSmService.selectQsfpList(pramsMap);
        }
        PageInfo<TDxInvoice> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());

        List<TDxInvoice> resultList = pageInfo.getList();
        List<SignQueryVO> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                SignQueryVO vo = null;
                if (pramsMap.get("authStatus") != null && !"".equals(pramsMap.get("authStatus"))) {
                    vo = this.convertToQslbViewTt(resultList.get(i), (curr - 1) * size + i + 1);
                } else {
                    vo = this.convertToQslbView(resultList.get(i), (curr - 1) * size + i + 1);
                }
                returnList.add(vo);
            }
        }
        json.put("datalist", returnList);

        return json;
    }

    /**
     * 今日头条
     *
     * @param tDxInvoice
     * @param xh
     * @return
     */
    private SignQueryVO convertToQslbViewTt(TDxInvoice tDxInvoice, int xh) {
        SignQueryVO vo = new SignQueryVO();
        vo.setXh(String.valueOf(xh));
        String qsStatus = tDxInvoice.getQsStatus();
        String qsType = tDxInvoice.getQsType();
        Date invoiceDate = tDxInvoice.getInvoiceDate();
        String businessName = tDxInvoice.getDeptName();
        vo.setNotes(tDxInvoice.getNotes());
        vo.setVoucherNumber(tDxInvoice.getVoucherNumber());
        vo.setQsDate(DateUtils.dateToStr(tDxInvoice.getQsDate()));
        if (FpqsjgEnum.WQS.getQsjgDm().equals(qsStatus)) {
            vo.setQsStatus(FpqsjgEnum.WQS.getQsjgMcSmb(FpqsjgEnum.WQS.getIndex()));
        } else {
            vo.setQsStatus(FpqsjgEnum.YQS.getQsjgMcSmb(FpqsjgEnum.YQS.getIndex()));
        }
        vo.setUserName(tDxInvoice.getUserName());
//        if (QsfsEnum.APPQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.APPQS.getQsfsMc(QsfsEnum.APPQS.getIndex()));
//        } else if (QsfsEnum.DRQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.DRQS.getQsfsMc(QsfsEnum.DRQS.getIndex()));
//        } else if (QsfsEnum.PDFQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.PDFQS.getQsfsMc(QsfsEnum.PDFQS.getIndex()));
//        } else if (QsfsEnum.SGQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.SGQS.getQsfsMc(QsfsEnum.SGQS.getIndex()));
//        } else if (QsfsEnum.SMQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.SMQS.getQsfsMc(QsfsEnum.SMQS.getIndex()));
//        } else if (QsfsEnum.SMYQS.getQsfsDm().equals(qsType)) {
//            vo.setQsType(QsfsEnum.SMYQS.getQsfsMc(QsfsEnum.SMYQS.getIndex()));
//        }
        vo.setQsType(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,qsType));
        vo.setInvoiceCode(tDxInvoice.getInvoiceCode());
        vo.setInvoiceNo(tDxInvoice.getInvoiceNo());
        vo.setEleInvoiceNo(tDxInvoice.getEleInvoiceNo());
        // 发票类型
        vo.setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, tDxInvoice.getInvoiceType()));
        vo.setInvoiceDate((invoiceDate != null ? DateUtils.dateToStr(tDxInvoice.getInvoiceDate()) : ""));
        vo.setSettlementNo(tDxInvoice.getSettlementNo());
        vo.setSettleSource(tDxInvoice.getSettleSource());
//        if ("0".equals(tDxInvoice.getSettlementStatus())) {
//            vo.setSettlementStatus("未结算");
//        } else if ("1".equals(tDxInvoice.getSettlementStatus())) {
//            vo.setSettlementStatus("已结算");
//        }
        vo.setSettlementStatus(dictdetaServcice.queryDictName(DictConstant.SETTLEMENT_STATUS,tDxInvoice.getSettlementStatus()));
        if ("0".equals(tDxInvoice.getAuthStatus()) || "6".equals(tDxInvoice.getAuthStatus())
            || StringUtils.isBlank(tDxInvoice.getAuthStatus())) {
            vo.setAuthStatus("未勾选");
        } else {
            vo.setAuthStatus("已勾选");
        }
        vo.setGfName(tDxInvoice.getGfName());
        vo.setGfTaxNo(tDxInvoice.getGfTaxNo());
        vo.setXfName(tDxInvoice.getXfName());
        vo.setXfTaxNo(tDxInvoice.getXfTaxNo());
        vo.setInvoiceAmount(((tDxInvoice.getInvoiceAmount() == null) ? ""
            : MathUtil.round(tDxInvoice.getInvoiceAmount().toPlainString())));
        vo.setTaxAmount(
            ((tDxInvoice.getTaxAmount() == null) ? "" : MathUtil.round(tDxInvoice.getTaxAmount().toPlainString())));
        vo.setTotalAmount(
            ((tDxInvoice.getTotalAmount() == null ? "" : MathUtil.round(tDxInvoice.getTotalAmount().toPlainString()))));
        vo.setCreateDate(tDxInvoice.getCreateDate() != null ? DateUtils.dateToStrLong(tDxInvoice.getCreateDate()) : "");
        vo.setRemark(tDxInvoice.getRemark());

        return vo;
    }

    /**
     * 转换成扫描查询结果显示
     *
     * @param tDxInvoice
     * @param xh
     * @return
     *
     */
    private SignQueryVO convertToQslbView(TDxInvoice tDxInvoice, int xh) {
//        List<DictdetaModel> qsfsModels = dictdetaServcice.selectDictdeta("qsType");
//        List<DictdetaModel> qsztModels = dictdetaServcice.selectDictdeta("qsStatus");
//        List<DictdetaModel> fplxModels = dictdetaServcice.selectDictdeta("INVOICE_TYPE");
        SignQueryVO vo = new SignQueryVO();
        vo.setXh(String.valueOf(xh));
        String qsStatus = tDxInvoice.getQsStatus();
        String qsType = tDxInvoice.getQsType();
        Date invoiceDate = tDxInvoice.getInvoiceDate();
        String businessName = tDxInvoice.getDeptName();
        vo.setNotes(tDxInvoice.getNotes());
        vo.setVoucherNumber(tDxInvoice.getVoucherNumber());
        vo.setQsDate(DateUtils.dateToStr(tDxInvoice.getQsDate()));

        vo.setUserName(tDxInvoice.getUserName());
        //字典转换
        vo.setQsType(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,qsType));
        vo.setQsStatus(dictdetaServcice.queryDictName(DictConstant.QS_RESULTS,qsStatus));
        vo.setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,tDxInvoice.getInvoiceType()));
        vo.setSettlementStatus(dictdetaServcice.queryDictName(DictConstant.SETTLEMENT_STATUS,tDxInvoice.getSettlementStatus()));


        vo.setInvoiceCode(tDxInvoice.getInvoiceCode());
        vo.setInvoiceNo(tDxInvoice.getInvoiceNo());
        vo.setEleInvoiceNo(tDxInvoice.getEleInvoiceNo());

        vo.setInvoiceDate((invoiceDate != null ? DateUtils.dateToStr(tDxInvoice.getInvoiceDate()) : ""));
        vo.setSettlementNo(tDxInvoice.getSettlementNo());
        vo.setSettleSource(tDxInvoice.getSettleSource());

        if ("0".equals(tDxInvoice.getAuthStatus()) || "6".equals(tDxInvoice.getAuthStatus())) {
            vo.setAuthStatus("未勾选");
        } else {
            vo.setAuthStatus("已勾选");
        }
        vo.setGfName(tDxInvoice.getGfName());
        vo.setGfTaxNo(tDxInvoice.getGfTaxNo());
        vo.setXfName(tDxInvoice.getXfName());
        vo.setXfTaxNo(tDxInvoice.getXfTaxNo());
        vo.setInvoiceAmount(((tDxInvoice.getInvoiceAmount() == null) ? ""
            : MathUtil.round(tDxInvoice.getInvoiceAmount().toPlainString())));
        vo.setTaxAmount(
            ((tDxInvoice.getTaxAmount() == null) ? "" : MathUtil.round(tDxInvoice.getTaxAmount().toPlainString())));
        vo.setTotalAmount(
            ((tDxInvoice.getTotalAmount() == null ? "" : MathUtil.round(tDxInvoice.getTotalAmount().toPlainString()))));
        vo.setCreateDate(tDxInvoice.getCreateDate() != null ? DateUtils.dateToStrLong(tDxInvoice.getCreateDate()) : "");
        vo.setRemark(tDxInvoice.getRemark());
        vo.setBusinessName(businessName);
        return vo;
    }
}
