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.BbfpzhmxDao;
import com.dxhy.core.model.BbfpzhmxcxRespVO;
import com.dxhy.core.service.export.FpzhcxmxService;
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
 */

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

public class FpzhcxmxServiceImpl extends MpBaseServiceImpl<BbfpzhmxDao, BbfpzhmxcxRespVO> implements FpzhcxmxService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @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.00");
                json.put("hjse", "0.00");
                json.put("total", "0");
                json.put("datalist", new ArrayList<BbfpzhmxcxRespVO>());
            } else {
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

                PageHelper.startPage(curr, size);
                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[32];
                            dataArray[0] = String.valueOf((curr - 1) * size + j + 1);
                            dataArray[1] = dictdetaServcice.queryDictName(DictConstant.DK_STATUS,pageList.get(j).getInAccountStatus());
                            dataArray[2] = pageList.get(j).getInvoiceCode();
                            dataArray[3] = pageList.get(j).getInvoiceNo();
                            dataArray[4] = pageList.get(j).getEleInvoiceNo();
                            // 发票类型
                            dataArray[5] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,pageList.get(j).getInvoiceType());
                            if(StringUtils.isBlank(pageList.get(j).getLqTdyslxDm())){
                                dataArray[6] = "非特定业务";
                            }else{
                                dataArray[6] = dictdetaServcice.queryDictName(DictConstant.TDYS_LXDM,pageList.get(j).getLqTdyslxDm());
                            }
                            dataArray[7] = pageList.get(j).getInvoiceDate();
                            dataArray[8] = pageList.get(j).getGfsh();
                            dataArray[9] = pageList.get(j).getGfmc();
                            dataArray[10] = pageList.get(j).getGfdzdh();
                            dataArray[11] = pageList.get(j).getGfkhhzh();
                            dataArray[12] = pageList.get(j).getXfsh();
                            dataArray[13] = pageList.get(j).getXfmc();
                            dataArray[14] = pageList.get(j).getXfdzdh();
                            dataArray[15] = pageList.get(j).getXfkhhzh();
                            if (StringUtils.isNotBlank(pageList.get(j).getInvoiceAmount())) {
                                dataArray[16] = MathUtil.round(pageList.get(j).getInvoiceAmount());
                            } else {
                                dataArray[16] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getTaxAmount())) {
                                dataArray[17] = MathUtil.round(pageList.get(j).getTaxAmount());
                            } else {
                                dataArray[17] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getJshj())) {
                                dataArray[18] = MathUtil.round(pageList.get(j).getJshj());
                            }else{
                                dataArray[18] = "";
                            }
                            dataArray[19] = pageList.get(j).getHlmc();
                            dataArray[20] = pageList.get(j).getGgxh();
                            dataArray[21] = pageList.get(j).getDw();
                            dataArray[22] = pageList.get(j).getNum();
                            dataArray[23] = pageList.get(j).getDj();
                            if (StringUtils.isNotBlank(pageList.get(j).getJe())) {
                                dataArray[24] = MathUtil.round(pageList.get(j).getJe());
                            } else {
                                dataArray[24] = "";
                            }
                            if (StringUtils.isNotBlank(pageList.get(j).getSe())) {
                                try {
                                    dataArray[25] = MathUtil.round(pageList.get(j).getSe());
                                } catch (Exception e) {
                                    dataArray[25] = pageList.get(j).getSe();
                                }
                            } else {
                                dataArray[25] = "";
                            }
                            dataArray[26] = pageList.get(j).getSl();
                            dataArray[27] = pageList.get(j).getCph();
                            dataArray[28] = pageList.get(j).getLx();
                            dataArray[29] = pageList.get(j).getTxrqq();
                            dataArray[30] = pageList.get(j).getTxrqz();
                            dataArray[31] = pageList.get(j).getBz();
                            returnList.add(dataArray);

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

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

        return json;
    }

}
