package com.dxhy.extend.service.bb.impl;

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

import javax.annotation.Resource;

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.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.extend.dao.ExtRecordInvoiceDao;
import com.dxhy.extend.entity.ExtRecordInvoiceEntity;
import com.dxhy.extend.service.bb.ExtRecordInvoiceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

/**
 * 逾期预警业务层 Created by alfred.zong on 2018/04/12.
 *
 * @author dxhy
 */
@Service("extendrecordInvoiceservice")
@Slf4j
public class ExtRecordInvoiceServiceImpl implements ExtRecordInvoiceService {
    @Resource
    private ExtRecordInvoiceDao recordInvoicedao;

    public String responseResult(JSONObject json) {
        String jsonString = null;
        if (json != null) {
            jsonString = Base64.encode(json.toJSONString());
        }
        return jsonString;
    }

    public JSONObject setTitleList(String[] titlelist, String[] titleprop) {
        JSONObject titleList = new JSONObject();
        if (titlelist != null) {
            titleList.put("titlelist", titlelist);
        }
        if (titleprop != null) {
            titleList.put("titleprop", titleprop);
        }

        JSONObject returnJson = new JSONObject();
        returnJson.put("titlelist", titleList);
        return returnJson;
    }

    /**
     * 根据多条件查询异常发票进行预警
     *
     * @param pramsMap
     *            查询条件 gfTaxNo 购方税号 firstDate 异常日期起 lastDate 异常日期止 invoiceNo 发票号码 invoiceStatus 异常状态码
     * @return 异常发票集合信息 @
     */
    @Override
    public String queryInvoice(Map<String, Object> pramsMap, int curr, int size) {
        log.info("今日发票认证列表,params {}", pramsMap);
        String[] titlelist = {"序号", "认证截止日", "发票代码", "发票号码", "开票日期", "购方名称", "销方名称", "金额", "税额", "抵扣税额"};
        String[] titleprop = {"xh", "cutApproveDate", "invoiceCode", "invoiceNo", "invoiceDate", "gfName", "xfName",
            "invoiceAmount", "taxAmount", "dkse"};
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = this.setTitleList(titlelist, titleprop);

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<ExtRecordInvoiceEntity> listVO = recordInvoicedao.queryInvoice(pramsMap);
        PageInfo<ExtRecordInvoiceEntity> pageInfo = new PageInfo<>(listVO);
        List<ExtRecordInvoiceEntity> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh((curr - 1) * size + i + 1);
                if (!StringHelper.isBlank(resultList.get(i).getCutApproveDate())) {
                    resultList.get(i).setCutApproveDate((resultList.get(i).getCutApproveDate().substring(0, 6)));
                }
                if (!StringHelper.isBlank(resultList.get(i).getInvoiceCode())) {
                    resultList.get(i).setInvoiceCode(resultList.get(i).getInvoiceCode());
                }
                if (StringUtils.isNotBlank(resultList.get(i).getInvoiceNo())) {
                    resultList.get(i).setInvoiceNo(resultList.get(i).getInvoiceNo());
                }
                if (StringUtils.isNotBlank(resultList.get(i).getInvoiceDate())) {
                    resultList.get(i).setInvoiceDate(resultList.get(i).getInvoiceDate().substring(0, 10));
                }
                if (StringUtils.isNotBlank(resultList.get(i).getGfName())) {
                    resultList.get(i).setGfName(resultList.get(i).getGfName());
                }
                if (StringUtils.isNotBlank(resultList.get(i).getXfName())) {
                    resultList.get(i).setXfName(resultList.get(i).getXfName());
                }

                if (!StringHelper.isBlank(resultList.get(i).getInvoiceAmount())) {
                    resultList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getInvoiceAmount())));
                }
                if (!StringHelper.isBlank(resultList.get(i).getTaxAmount())) {
                    resultList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getTaxAmount())));
                }

                if (!StringHelper.isBlank(resultList.get(i).getDkse())) {
                    resultList.get(i)
                        .setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getDkse())));
                }

            }
        }
        json.put("datalist", resultList);
        return this.responseResult(json);
    }

}
