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

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

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import org.apache.commons.lang.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.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.TDxCheckHistoryDetailDao;
import com.dxhy.core.entity.TDxCheckInvoiceDetail;
import com.dxhy.core.service.export.InvoiceCheckDetailService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 查验业务处理接口实现类
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("invoiceCheckDetailService")
@Slf4j

public class InvoiceCheckDetailServiceImpl extends MpBaseServiceImpl<TDxCheckHistoryDetailDao, TDxCheckInvoiceDetail>
    implements InvoiceCheckDetailService {

    @Override
    public JSONObject getCheckHistoryDetail(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = new JSONObject();

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<TDxCheckInvoiceDetail> pageList = super.baseMapper.selectCheckHistoryDetail(pramsMap);
        PageInfo<TDxCheckInvoiceDetail> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        List<TDxCheckInvoiceDetail> resultList = pageInfo.getList();

        List<String[]> returnList = new ArrayList<>();

        if (resultList != null && resultList.size() > 0) {

            for (int i = 0; i < resultList.size(); i++) {
                String[] dataArray = new String[33];
                dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                dataArray[1] = resultList.get(i).getFPDM();
                dataArray[2] = resultList.get(i).getFPHM();
                // 发票类型
                dataArray[3] = new InvoiceTypeUtils().invoiceTypeName(resultList.get(i).getFPZL());
//                dataArray[3] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE,resultList.get(i).getFPZL());
                dataArray[4] =
                    (resultList.get(i).getKPRQ() == null) ? "" : DateUtils.dateToStr(resultList.get(i).getKPRQ());
                dataArray[5] = StringHelper.trim(resultList.get(i).getGFMC(), "");
                dataArray[6] = StringHelper.trim(resultList.get(i).getGFSH(), "");
                dataArray[7] = StringHelper.trim(resultList.get(i).getXFMC(), "");
                dataArray[8] = StringHelper.trim(resultList.get(i).getXFSH(), "");
                if ("N".equals(resultList.get(i).getZFBZ()) || "0".equals(resultList.get(i).getZFBZ())) {
                    dataArray[9] = "正常";
                } else if ("Y".equals(resultList.get(i).getZFBZ()) || "2".equals(resultList.get(i).getZFBZ())) {
                    dataArray[9] = "作废";
                } else if ("3".equals(resultList.get(i).getZFBZ())) {
                    dataArray[9] = "红冲";
                } else {
                    dataArray[9] = "";
                }

                try {
                    dataArray[10] =
                            (StringUtils.isBlank(resultList.get(i).getJE())) ? "" : MathUtil.round(resultList.get(i).getJE());
                }catch (Exception e){
                    dataArray[10]="";
                }
                try{
                    dataArray[11] =
                            (StringUtils.isBlank(resultList.get(i).getSE())) ? "" : MathUtil.round(resultList.get(i).getSE());
                }catch (Exception e){
                    dataArray[11]="";
                }
                try{
                    dataArray[12] = (StringUtils.isBlank(resultList.get(i).getJSHJ())) ? ""
                            : MathUtil.round(resultList.get(i).getJSHJ());
                }catch (Exception e){
                    dataArray[12]="";
                }
                
                dataArray[13] = resultList.get(i).getCYJGTIP();
                dataArray[14] = resultList.get(i).getCreateDate();
                dataArray[15] = resultList.get(i).getCheckUser();
                dataArray[16] = resultList.get(i).getBZ();
                dataArray[17] = resultList.get(i).getGFDZDH();
                dataArray[18] = resultList.get(i).getGFYHZH();
                dataArray[19] = resultList.get(i).getXFDZDH();
                dataArray[20] = resultList.get(i).getXFYHZH();
                dataArray[21] = resultList.get(i).getGoodsName();
                dataArray[22] = resultList.get(i).getMODEL();
                dataArray[23] = resultList.get(i).getUNIT();
                dataArray[24] = resultList.get(i).getNUM();
                dataArray[25] = resultList.get(i).getUnitPrice();
                dataArray[26] = resultList.get(i).getDetailAmount();
                dataArray[27] = resultList.get(i).getDetailTaxAmount();

                dataArray[28] = resultList.get(i).getTaxRate();
                dataArray[29] = resultList.get(i).getCph();
                dataArray[30] = resultList.get(i).getLx();
                dataArray[31] = resultList.get(i).getTxrqq();
                dataArray[32] = resultList.get(i).getTxrqz();
                returnList.add(dataArray);
            }
        }
        json.put("datalist", returnList);
        return json;
    }
}
