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.CoreTDxRecordInvoiceDao;
import com.dxhy.api.model.invoice.TDxRecordInvoice;
import com.dxhy.core.model.FpcjWbfpmxRespVO;
import com.dxhy.core.service.export.IFpcjmxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 发票采集明细查询实现
 *
 * @author user
 */
@Service
public class FpcjmxServiceImpl extends MpBaseServiceImpl<CoreTDxRecordInvoiceDao, TDxRecordInvoice>
    implements IFpcjmxService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectWbfpmx(Map<String, Object> pramsMap, int curr, int size) {

        JSONObject json = new JSONObject();

        List<String> gfshList = (List<String>)pramsMap.get("gfsh");
        if (gfshList.size() == 0) {
            // 未从getUserInfo()获取到购方税号信息
            json.put("datalist", new ArrayList<FpcjWbfpmxRespVO>());
        } else {
            DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                long hjsl = 0;
                String hjje = "0.00";
                String hjse = "0.00";
                List<TDxRecordInvoice> hjList = super.baseMapper.selectWbfpmxHj(pramsMap);
                TDxRecordInvoice hjxx = hjList.get(0);
                hjsl = hjxx.getId();
                if (hjsl > 0) {
                    hjje = MathUtil.round(hjxx.getInvoiceAmount().toPlainString());
                    hjse = MathUtil.round(hjxx.getTaxAmount().toPlainString());
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }

            PageHelper.startPage(curr, size);
            List<FpcjWbfpmxRespVO> pageList = super.baseMapper.selectWbfpmx(pramsMap);
            PageInfo<FpcjWbfpmxRespVO> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());

            List<FpcjWbfpmxRespVO> resultList = pageInfo.getList();

            if (resultList != null && resultList.size() > 0) {
                int i = 1;
                for (FpcjWbfpmxRespVO tDxRecordInvoice : resultList) {
                    tDxRecordInvoice.setXh(String.valueOf((curr - 1) * size + i));
                    tDxRecordInvoice.setFplx(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,tDxRecordInvoice.getFplx()));
                    if(StringUtils.isNotEmpty(tDxRecordInvoice.getJe())){
                        tDxRecordInvoice.setJe(MathUtil.round(tDxRecordInvoice.getJe()));
                    }
                    if(StringUtils.isNotEmpty(tDxRecordInvoice.getSe())){
                        tDxRecordInvoice.setSe(MathUtil.round(tDxRecordInvoice.getSe()));
                    }
                    tDxRecordInvoice.setInvoiceSourcs(dictdetaServcice.queryDictName(DictConstant.INVOICE_SOURCE,tDxRecordInvoice.getInvoiceSourcs()));
                    i++;
                }
            }
            json.put("datalist", resultList);
        }

        return json;
    }

}
