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.BbfpmxDao;
import com.dxhy.core.model.BbfpmxcxRespVO;
import com.dxhy.core.service.export.FpmxcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("corefpmxcxService")
@Slf4j
public class FpmxcxServiceImpl extends MpBaseServiceImpl<BbfpmxDao, BbfpmxcxRespVO> implements FpmxcxService {

    @Resource
    private DictdetaServcice dictdetaServcice;

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

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

                PageHelper.startPage(curr, size);
                List<BbfpmxcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<BbfpmxcxRespVO> 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) {
                        for (int j = 0; j < pageList.size(); j++) {
                            String[] dataArray = new String[29];
                            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());
                            dataArray[5] = pageList.get(j).getInvoiceDate();
                            dataArray[6] = pageList.get(j).getDetailNo();
                            dataArray[7] = pageList.get(j).getUnit();
                            dataArray[8] = pageList.get(j).getGoodsName();
                            dataArray[9] = pageList.get(j).getModel();
                            if(StringUtils.isNotBlank(pageList.get(j).getNum())){
                                dataArray[10] = MathUtil.round(pageList.get(j).getNum());
                            }else {
                                dataArray[10]="";
                            }
                            dataArray[11] = pageList.get(j).getUnitPrice();
                            dataArray[12] = pageList.get(j).getGfTaxNo();
                            dataArray[13] = pageList.get(j).getGfName();
                            dataArray[14] = pageList.get(j).getGfAddressAndPhone();
                            dataArray[15] = pageList.get(j).getGfBankAndNo();
                            dataArray[16] = pageList.get(j).getXfTaxNo();
                            dataArray[17] = pageList.get(j).getXfName();
                            dataArray[18] = pageList.get(j).getXfAddressAndPhone();
                            dataArray[19] = pageList.get(j).getXfBankAndNo();
                            BigDecimal detailAmount= null;
                            try {
                                detailAmount = new BigDecimal(pageList.get(j).getDetailAmount());
                            } catch (Exception e) {
                                detailAmount = new BigDecimal("0.00");
                            }
                            dataArray[20] = MathUtil.round(detailAmount.toPlainString());
                            if(StringUtils.isNotBlank(pageList.get(j).getTaxRate())){
                                dataArray[21] = pageList.get(j).getTaxRate();
                            }else{
                                dataArray[21] = "";
                            }

                            try {

                                if(StringUtils.isNotBlank(pageList.get(j).getTaxAmount())){
                                    BigDecimal taxAmount = new BigDecimal(pageList.get(j).getTaxAmount());
                                    BigDecimal totalAmount=detailAmount.add(taxAmount);
                                    dataArray[22] = MathUtil.round(taxAmount.toPlainString());
                                    dataArray[23] = MathUtil.round(totalAmount.toPlainString());
                                }else{
                                    dataArray[22] = "";
                                    dataArray[23] = MathUtil.round(detailAmount.toPlainString());
                                }
                            } catch (Exception e) {
                                dataArray[22] = pageList.get(j).getTaxAmount();
                                dataArray[23] = MathUtil.round(detailAmount.toPlainString());
                            }

                            dataArray[24] =  pageList.get(j).getCph();
                            dataArray[25] =  pageList.get(j).getLx();
                            dataArray[26] =  pageList.get(j).getTxrqq();
                            dataArray[27] =  pageList.get(j).getTxrqz();
                            dataArray[28] =  pageList.get(j).getRemark();
                            returnList.add(dataArray);
                        }
                }
                json.put("datalist", returnList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return json;
    }

    public static void main(String[] args) {
        BigDecimal taxAmount = new BigDecimal("");
        System.out.println(taxAmount);

    }


}
