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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
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.common.vo.DictdetaModel;
import com.dxhy.core.dao.BbfpzhmxDao;
import com.dxhy.core.dao.InvoiceDetailDao;
import com.dxhy.core.model.BbfpzhmxcxRespVO;
import com.dxhy.core.service.export.FpzhcxmxService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 发票认证综合查询
 *
 * @author
 */

@SuppressWarnings("AlibabaMethodTooLong")
@Service("fpzhcxmxService")
@Slf4j

public class FpzhcxmxServiceImpl extends MpBaseServiceImpl<BbfpzhmxDao, BbfpzhmxcxRespVO> implements FpzhcxmxService {

    @Resource
    private InvoiceDetailDao invoiceDetailDao;
    @Resource
    private DictdetaServcice dictdetaServcice;
    @Value("${dxhy.datesource.dxhy_admin:dxhy_admin}")
    private String dxhyAdmin;
    @Value("${dxhy.datasource.dxhy_tertiary:dxhy_tertiary}")
    private String dxhyTertiary;
    @Value("${dxhy.datasource.dxhy_detail:dxhy_detail}")
    private String dxhyDetail;

    @Override
    public JSONObject selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();

        try {

            List<String> gfshList = (List<String>)pramsMap.get("gfsh");
            String inAccountStatus = (String)pramsMap.get("inAccountStatus");
            if (gfshList.size() == 0) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("hjsl", "0");
                json.put("hjje", "0");
                json.put("hjse", "0");
                json.put("total", "0");
                json.put("datalist", new ArrayList<BbfpzhmxcxRespVO>());
            } else {
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

                PageHelper.startPage(curr, size);
                pramsMap.put("dxhyAdmin", dxhyAdmin);
                pramsMap.put("dxhyTertiary", dxhyTertiary);
                pramsMap.put("dxhyDetail", dxhyDetail);
                List<BbfpzhmxcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<BbfpzhmxcxRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());

                List<String[]> returnList = new ArrayList<>();
                if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
                    if (StringUtils.isNotBlank(inAccountStatus)) {
                        for (int j = 0; j < pageList.size(); j++) {
                            String[] dataArray = new String[30];
                            dataArray[0] = String.valueOf((curr - 1) * size + j + 1);
                            if (pageList.get(j).getInAccountStatus() != null && "1".equals(pageList.get(j).getInAccountStatus())){
                                dataArray[1] = "已入账";
                            }else {
                                dataArray[1] = "未入账";
                            }
                            dataArray[2] = pageList.get(j).getInvoiceCode();
                            dataArray[3] = pageList.get(j).getInvoiceNo();
                            // 发票类型
                            dataArray[4] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,pageList.get(j).getInvoiceType());
                            dataArray[5] = pageList.get(j).getInvoiceDate();
                            dataArray[6] = pageList.get(j).getGfsh();
                            dataArray[7] = pageList.get(j).getGfmc();
                            dataArray[8] = pageList.get(j).getGfdzdh();
                            dataArray[9] = pageList.get(j).getGfkhhzh();
                            dataArray[10] = pageList.get(j).getXfsh();
                            dataArray[11] = pageList.get(j).getXfmc();
                            dataArray[12] = pageList.get(j).getXfdzdh();
                            dataArray[13] = pageList.get(j).getXfkhhzh();
                            if (StringUtils.isNotBlank(pageList.get(j).getInvoiceAmount())) {
                                dataArray[14] = MathUtil.round(pageList.get(j).getInvoiceAmount());
                            } else {
                                dataArray[14] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getTaxAmount())) {
                                dataArray[15] = MathUtil.round(pageList.get(j).getTaxAmount());
                            } else {
                                dataArray[15] = "";
                            }
                            dataArray[16] = MathUtil.round(pageList.get(j).getJshj());
                            dataArray[17] = pageList.get(j).getHlmc();
                            dataArray[18] = pageList.get(j).getGgxh();
                            dataArray[19] = pageList.get(j).getDw();
                            dataArray[20] = pageList.get(j).getNum();
                            dataArray[21] = pageList.get(j).getDj();
                            if (StringUtils.isNotBlank(pageList.get(j).getJe())) {
                                dataArray[22] = MathUtil.round(pageList.get(j).getJe());
                            } else {
                                dataArray[22] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getSe())) {
                                try {
                                    dataArray[23] = MathUtil.round(pageList.get(j).getSe());
                                } catch (Exception e) {
                                    dataArray[23] = pageList.get(j).getSe();
                                }
                            } else {
                                dataArray[23] = "";
                            }
                            dataArray[24] = pageList.get(j).getSl();
                            dataArray[25] = pageList.get(j).getCph();
                            dataArray[26] = pageList.get(j).getLx();
                            dataArray[27] = pageList.get(j).getTxrqq();
                            dataArray[28] = pageList.get(j).getTxrqz();
                            dataArray[29] = pageList.get(j).getBz();
                            returnList.add(dataArray);

                        }
                    } else {
                        for (int j = 0; j < pageList.size(); j++) {
                            String[] dataArray = new String[30];
                            dataArray[0] = String.valueOf((curr - 1) * size + j + 1);
                            dataArray[1] = pageList.get(j).getInvoiceCode();
                            dataArray[2] = pageList.get(j).getInvoiceNo();
                            // 发票类型
                            dataArray[3] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,pageList.get(j).getInvoiceType());
                            dataArray[4] = pageList.get(j).getInvoiceDate();
                            dataArray[5] = pageList.get(j).getGfsh();
                            dataArray[6] = pageList.get(j).getGfmc();
                            dataArray[7] = pageList.get(j).getGfdzdh();
                            dataArray[8] = pageList.get(j).getGfkhhzh();
                            dataArray[9] = pageList.get(j).getXfsh();
                            dataArray[10] = pageList.get(j).getXfmc();
                            dataArray[11] = pageList.get(j).getXfdzdh();
                            dataArray[12] = pageList.get(j).getXfkhhzh();
                            if (StringUtils.isNotBlank(pageList.get(j).getInvoiceAmount())) {
                                dataArray[13] = MathUtil.round(pageList.get(j).getInvoiceAmount());
                            } else {
                                dataArray[13] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getTaxAmount())) {
                                dataArray[14] = MathUtil.round(pageList.get(j).getTaxAmount());
                            } else {
                                dataArray[14] = "";
                            }
                            dataArray[15] = MathUtil.round(pageList.get(j).getJshj());
                            dataArray[16] = pageList.get(j).getSkssq();
                            dataArray[17] = pageList.get(j).getHlmc();
                            dataArray[18] = pageList.get(j).getGgxh();
                            dataArray[19] = pageList.get(j).getDw();
                            dataArray[20] = pageList.get(j).getNum();
                            dataArray[21] = pageList.get(j).getDj();
                            if (StringUtils.isNotBlank(pageList.get(j).getJe())) {
                                dataArray[22] = MathUtil.round(pageList.get(j).getJe());
                            } else {
                                dataArray[22] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getSe())) {
                                try {
                                    dataArray[23] = MathUtil.round(pageList.get(j).getSe());
                                } catch (Exception e) {
                                    dataArray[23] = pageList.get(j).getSe();
                                }
                            } else {
                                dataArray[23] = "";
                            }
                            dataArray[24] = pageList.get(j).getSl();
                            dataArray[25] = pageList.get(j).getCph();
                            dataArray[26] = pageList.get(j).getLx();
                            dataArray[27] = pageList.get(j).getTxrqq();
                            dataArray[28] = pageList.get(j).getTxrqz();
                            dataArray[29] = pageList.get(j).getBz();
                            returnList.add(dataArray);

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

        return json;
    }

}
