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

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.core.dao.SgqsDao;
import com.dxhy.core.entity.TDxInvoice;
import com.dxhy.core.model.BbrzrbcxRespVO;
import com.dxhy.core.model.SignQueryVO;
import com.dxhy.core.service.export.SgqsService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("sgqsService")
@Slf4j

public class SgqsServiceImpl extends MpBaseServiceImpl<SgqsDao, TDxInvoice> implements SgqsService {

    @Override
    public JSONObject selectSgqs(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        JSONObject json = new JSONObject();
        try {
            List<String> gfshList = (List<String>)pramsMap.get("gfsh");

            if (gfshList.size() == 0) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("hjsl", "0");
                json.put("hjje", "0");
                json.put("hjse", "0");
                json.put("total", "0");
                json.put("datalist", new ArrayList<BbrzrbcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<TDxInvoice> pageList = super.baseMapper.selectSgqs(pramsMap);
                PageInfo<TDxInvoice> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                List<SignQueryVO> returnList = new ArrayList<>();
                // 用于页面返回list对象
                if (pageList.size() > 0) {
                    for (int i = 0; i < pageList.size(); i++) {
                        SignQueryVO vo = new SignQueryVO();
                        vo.setXh(String.valueOf((curr - 1) * size + i + 1));
                        vo.setInvoiceCode(pageList.get(i).getInvoiceCode());
                        vo.setInvoiceNo(pageList.get(i).getInvoiceNo());
                        vo.setEleInvoiceNo(pageList.get(i).getEleInvoiceNo());
                        vo.setInvoiceType(new InvoiceTypeUtils().invoiceTypeName(pageList.get(i).getInvoiceType()));
                        vo.setInvoiceDate(new SimpleDateFormat("yyyy-MM-dd").format(pageList.get(i).getInvoiceDate()));
                        vo.setGfName(pageList.get(i).getGfName());
                        vo.setGfTaxNo(pageList.get(i).getGfTaxNo());
                        vo.setXfName(pageList.get(i).getXfName());
                        vo.setXfTaxNo(pageList.get(i).getXfTaxNo());
                        vo.setInvoiceAmount(MathUtil.round(pageList.get(i).getInvoiceAmount().toPlainString()));
                        vo.setTaxAmount(MathUtil.round(pageList.get(i).getTaxAmount().toPlainString()));
                        returnList.add(vo);
                    }
                }

                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

}
