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

import java.math.BigDecimal;
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.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.BbfpzhslDao;
import com.dxhy.core.model.BbfpzhslcxRespVO;
import com.dxhy.core.service.export.FpzhcxslService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 发票认证综合查询税率导出实现类
 *
 * @author jiaohongyang
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service("fpzhcxslService")
@Slf4j

public class FpzhcxslServiceImpl extends MpBaseServiceImpl<BbfpzhslDao, BbfpzhslcxRespVO> implements FpzhcxslService {
    @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");
            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<BbfpzhslcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<BbfpzhslcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);

                PageInfo<BbfpzhslcxRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());

                List<String[]> returnList = new ArrayList<>();
                // 用于页面返回list对象
                if (pageList.size() > 0) {

                    if (StringUtils.isNotBlank(inAccountStatus)) {
                        for (int i = 0; i < pageList.size(); i++) {
                            String[] dataArray = new String[8];
                            dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                            if ("1".equals(pageList.get(i).getInAccountStatus())){
                                dataArray[1] = "已入账";
                            }else {
                                dataArray[1] = "未入账";
                            }
//                            dataArray[1] = dictdetaServcice.queryDictName(DictConstant.DK_STATUS,pageList.get(i).getInAccountStatus());
                            dataArray[2] = pageList.get(i).getInvoiceCode();
                            dataArray[3] = pageList.get(i).getInvoiceNo();
                            // 发票类型
                            dataArray[4] =dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,pageList.get(i).getInvoiceType());
                            dataArray[5] = MathUtil.round(pageList.get(i).getInvoiceAmount());
                            try {

                                dataArray[6] = pageList.get(i).getTaxAmount() == null ? ""
                                    : MathUtil.round(pageList.get(i).getTaxAmount());
                            } catch (Exception e) {
                                dataArray[6] = "0.00";
                            }
                            String sl = pageList.get(i).getSl();
                            String tax = "";
                            if (StringUtils.isNotBlank(sl)) {
                                try {
                                    tax = new BigDecimal(sl).stripTrailingZeros().toPlainString();

                                } catch (Exception e) {
                                    tax = "0";
                                }
                            }
                            dataArray[7] = tax;
                            returnList.add(dataArray);
                        }
                    } else {
                        for (int i = 0; i < pageList.size(); i++) {
                            String[] dataArray = new String[7];
                            dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                            dataArray[1] = pageList.get(i).getInvoiceCode();
                            dataArray[2] = pageList.get(i).getInvoiceNo();
                            // 发票类型
                            dataArray[3] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,pageList.get(i).getInvoiceType());
                            dataArray[4] = MathUtil.round(pageList.get(i).getInvoiceAmount());
                            try {

                                dataArray[5] = pageList.get(i).getTaxAmount() == null ? ""
                                    : MathUtil.round(pageList.get(i).getTaxAmount());
                            } catch (Exception e) {
                                dataArray[5] = "0.00";
                            }
                            String sl = pageList.get(i).getSl();
                            String tax = "";
                            if (StringUtils.isNotBlank(sl)) {
                                try {
                                    tax = new BigDecimal(sl).stripTrailingZeros().toPlainString();

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

        return json;
    }

}
