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

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

import javax.annotation.Resource;

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.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.extend.dao.ExtDefinitiveStrideYearCopDao;
import com.dxhy.extend.entity.ExtRecordInvoiceEntity;
import com.dxhy.extend.service.bb.ExtDefinitiveStrideYearCopService;
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/16.
 *
 * @author dxhy
 */
@Service
@Slf4j
public class ExtDefinitiveStrideYearCopServiceImpl implements ExtDefinitiveStrideYearCopService {
    @Resource
    private ExtDefinitiveStrideYearCopDao definitiveStrideYearCopDao;

    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 购方税号 signForDate1 签收日期起 signForDate2 签收日期止 billingDate1 开票日期起 billingDate2 开票日期止
     *            invoiceNo 发票号码
     * @return 普票跨年度预警发票的集合 @
     */
    @Override
    public String queryDefinitiveList(Map<String, Object> pramsMap, int curr, int size) {

        log.info("普票跨年度预警发票列表,params {}", pramsMap);
        String[] titlelist = {"序号", "签收日期", "签收状态", "签收人", "发票代码", "发票号码", "开票日期", "购方名称", "销方名称", "金额", "税额"};
        String[] titleprop = {"xh", "qsDate", "qsStatus", "userName", "invoiceCode", "invoiceNo", "invoiceDate",
            "gfName", "xfName", "invoiceAmount", "taxAmount"};
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = this.setTitleList(titlelist, titleprop);

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<ExtRecordInvoiceEntity> listVO = definitiveStrideYearCopDao.queryDefinitiveList(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).getQsDate())) {
                    resultList.get(i).setQsDate((resultList.get(i).getQsDate().substring(0, 10)));
                }
                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())));
                }

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

}
