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

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.BbfpzhDao;
import com.dxhy.core.model.BbfpzhcxRespVO;
import com.dxhy.core.service.export.FpzhcxService;
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.List;
import java.util.Map;

/**
 * 发票认证综合查询
 *
 * @author user
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service("corefpzhcxService")
@Slf4j

public class FpzhcxServiceImpl extends MpBaseServiceImpl<BbfpzhDao, BbfpzhcxRespVO> implements FpzhcxService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        try {
            List<String> gfshList = (List<String>)pramsMap.get("gfsh");
            if (gfshList.size() == 0) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("hjsl", "0");
                json.put("hjje", "0.00");
                json.put("hjse", "0.00");
                json.put("total", "0");
                json.put("datalist", new ArrayList<BbfpzhcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<BbfpzhcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<BbfpzhcxRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                // 用于页面返回list对象
                if (pageList != null && pageList.size() > 0) {
                    // List<DictdetaModel> glztModels = dictdetaServcice.selectDictdeta("GLZT");
                    // List<DictdetaModel> jsztModels = dictdetaServcice.selectDictdeta("settlementStatus");
                    // List<DictdetaModel> fpztModels = dictdetaServcice.selectDictdeta("INVOICE_STATUS");
                    // List<DictdetaModel> rzztModels = dictdetaServcice.selectDictdeta("rzhYesorno");
                    // List<DictdetaModel> rzclztModels = dictdetaServcice.selectDictdeta("authStatus");
                    // List<DictdetaModel> qsztModels = dictdetaServcice.selectDictdeta("qsStatus");
                    // List<DictdetaModel> qsfsModels = dictdetaServcice.selectDictdeta("qsType");
                    // List<DictdetaModel> rzlxModels = dictdetaServcice.selectDictdeta("BDK_STATUS");
                    // List<DictdetaModel> models = dictdetaServcice.selectDictdeta("DKZT");
                    // List<DictdetaModel> fplxModels = dictdetaServcice.selectDictdeta("INVOICE_TYPE");
                    int i = 0;
                    for (BbfpzhcxRespVO vo : pageList) {
                        i++;
                        vo.setXh(String.valueOf((curr - 1) * size + i));
                        // 发票类型
                        if (StringUtils.isNotBlank(vo.getInvoiceAmount())) {
                            vo.setInvoiceAmount(MathUtil.round(vo.getInvoiceAmount()));
                        } else {
                            vo.setInvoiceAmount("");
                        }
                        if (StringUtils.isNotBlank(vo.getTaxAmount())) {
                            vo.setTaxAmount(MathUtil.round(vo.getTaxAmount()));
                        } else {
                            vo.setTaxAmount("");
                        }

                        if (!"01".equals(vo.getInvoiceType()) && !"03".equals(vo.getInvoiceType())
                            && !"08".equals(vo.getInvoiceType()) && !"14".equals(vo.getInvoiceType())
                            && !"31".equals(vo.getInvoiceType())
                                && !"283".equals(vo.getInvoiceType())
                                && !"161".equals(vo.getInvoiceType())
                                && !"183".equals(vo.getInvoiceType())
                                && !"185".equals(vo.getInvoiceType())
                                && !("32".equals(vo.getInvoiceType())&&"08".equals(vo.getLqTdyslxDm()))
                                && !"2".equals(vo.getYwlx())
                        ) {
                            vo.setDkTaxAmount("");
                        } else {
                            if (StringUtils.isNotBlank(vo.getDkTaxAmount())) {
                                vo.setDkTaxAmount(MathUtil.round(vo.getDkTaxAmount()));
                            } else {
                                if(StringUtils.isNotEmpty(vo.getTaxAmount())){
                                    vo.setDkTaxAmount(MathUtil.round(vo.getTaxAmount()));
                                }
                            }
                        }
                        // 字典转换
                        vo.setGlzt(dictdetaServcice.queryDictName(DictConstant.GLZT, vo.getGlzt()));
                        vo.setSettlementStatus(
                            dictdetaServcice.queryDictName(DictConstant.SETTLEMENT_STATUS, vo.getSettlementStatus()));
                        vo.setInvoiceStatus(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, vo.getInvoiceStatus()));
                        vo.setRzzt(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, vo.getRzzt()));
                        vo.setInAccountStatus(
                            dictdetaServcice.queryDictName(DictConstant.DK_STATUS, vo.getInAccountStatus()));
                        vo.setInvoiceType(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, vo.getInvoiceType()));
                        vo.setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, vo.getQszt()));
                        vo.setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, vo.getQsfs()));
                        vo.setRzclzt(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, vo.getRzclzt()));
                        vo.setBdkStatus(dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, vo.getBdkStatus()));

                        if (StringUtils.isNotBlank(vo.getExportMark())) {
                            vo.setExportMark(
                                "0".equals(vo.getExportMark()) ? "否" : "1".equals(vo.getExportMark()) ? "是" : "");
                        } else {
                            vo.setExportMark("");
                        }
                        if (StringUtils.isNotBlank(vo.getTotalAmount())) {
                            vo.setTotalAmount(MathUtil.round(vo.getTotalAmount()));
                        } else {
                            vo.setTotalAmount("");
                        }

                        //特定要素
                        if(StringUtils.isBlank(vo.getLqTdyslxDm())){
                            vo.setLqTdyslxDm("非特定业务");
                        }else{
                            vo.setLqTdyslxDm(dictdetaServcice.queryDictName(DictConstant.TDYS_LXDM, vo.getLqTdyslxDm()));
                        }

                        //入账状态
                        //已入账  02 05  未入账  01 03 04
                        String lqRzzt = vo.getLqRzzt();
                        if("02".equals(lqRzzt)||"05".equals(lqRzzt)){
                            vo.setLqRzzt("已入账");
                        }else{
                            vo.setLqRzzt("未入账");
                        }

                    }
                    json.put("datalist", pageList);
                }
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

}
