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

import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.invoice.invoiceCheck.TDxCheckLog;
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.common.util.StringHelper;
import com.dxhy.core.dao.TDxCheckLogDao;
import com.dxhy.core.model.CheckInvoiceVO;
import com.dxhy.core.service.export.IInvoiceQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 查验业务处理接口实现类
 *
 * @author
 */

@Service("invoiceQueryService")
@Slf4j

public class InvoiceQueryServiceImpl extends MpBaseServiceImpl<TDxCheckLogDao, TDxCheckLog>
    implements IInvoiceQueryService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject getCheckHistory(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<CheckInvoiceVO> pageList = super.baseMapper.selectCheckHistory(pramsMap);
        PageInfo<CheckInvoiceVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        List<CheckInvoiceVO> resultList = pageInfo.getList();
        if (resultList != null && resultList.size() > 0) {

            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh(String.valueOf((curr - 1) * size + i + 1));
                resultList.get(i)
                    .setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, resultList.get(i).getInvoiceType()));
                resultList.get(i).setGfmc(StringHelper.trim(resultList.get(i).getGfmc(), ""));
                resultList.get(i).setInvoiceAmount((StringUtils.isBlank(resultList.get(i).getInvoiceAmount())) ? ""
                    : MathUtil.round(resultList.get(i).getInvoiceAmount()));
                resultList.get(i).setTaxAmount((StringUtils.isBlank(resultList.get(i).getTaxAmount())) ? ""
                    : MathUtil.round(resultList.get(i).getTaxAmount()));
                resultList.get(i).setTotalAmount((StringUtils.isBlank(resultList.get(i).getTotalAmount())) ? ""
                    : MathUtil.round(resultList.get(i).getTotalAmount()));
                resultList.get(i).setCheckMassege(StringHelper.trim(resultList.get(i).getCheckMassege(), ""));
            }
        }
        json.put("datalist", resultList);
        return json;
    }

}
