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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.core.model.SignQueryVO;
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.enums.FpqsjgEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.TDxRecordInvoiceDao;
import com.dxhy.core.entity.TDxInvoice;
import com.dxhy.core.entity.TDxRecordInvoice;
import com.dxhy.core.service.export.MryxFpqsService;
import com.dxhy.core.service.singletable.CoreInvoiceService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 发票签收业务逻辑处理实现类
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("mryxService")
@Slf4j

public class MryxFpqsServiceImpl extends MpBaseServiceImpl<TDxRecordInvoiceDao, TDxRecordInvoice>
    implements MryxFpqsService {

    @Resource
    private CoreInvoiceService invoiceSmService;
    @Resource
    private DictdetaServcice dictdetaServcice;

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

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject json = new JSONObject();

        List<String> gfshList = (List<String>)pramsMap.get("gfsh");
        if (gfshList.size() == 0) {
            // 未从getUserInfo()获取到购方税号信息
            json.put("total", "0");
            json.put("datalist", new ArrayList<String[]>());
        } else {

            PageHelper.startPage(curr, size);
            List<TDxInvoice> pageList = invoiceSmService.selectQsfpListMryx(pramsMap);
            PageInfo<TDxInvoice> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());

            List<TDxInvoice> resultList = pageInfo.getList();
            List<SignQueryVO> returnList = new ArrayList<>();
            if (resultList != null && resultList.size() > 0) {
                for (int i = 0; i < resultList.size(); i++) {
                    SignQueryVO mxlb = this.convertToQslbView(resultList.get(i), (curr - 1) * size + i + 1);
                    returnList.add(mxlb);
                }
            }
            json.put("datalist", returnList);
        }

        return json;
    }

    /**
     * 转换成扫描查询结果显示
     *
     * @param tDxInvoice
     *            扫描信息
     * @param xh
     *            序号
     *
     * @return 返回
     *
     */
    private SignQueryVO convertToQslbView(TDxInvoice tDxInvoice, int xh) {
        SignQueryVO vo = new SignQueryVO();
        String qsStatus = tDxInvoice.getQsStatus();
        Date invoiceDate = tDxInvoice.getInvoiceDate();
        vo.setXh(String.valueOf(xh));
        vo.setNotes(tDxInvoice.getNotes());
        vo.setInvoiceCode(tDxInvoice.getInvoiceCode());
        vo.setInvoiceNo(tDxInvoice.getInvoiceNo());
        vo.setInvoiceType(new InvoiceTypeUtils().invoiceTypeName(tDxInvoice.getInvoiceType()));
        vo.setInvoiceDate((invoiceDate != null ? DateUtils.dateToStr(tDxInvoice.getInvoiceDate()) : ""));
        vo.setGfTaxNo(tDxInvoice.getGfTaxNo());
        vo.setXfTaxNo(tDxInvoice.getXfTaxNo());
        vo.setInvoiceAmount(((tDxInvoice.getInvoiceAmount() == null) ? ""
            : MathUtil.round(tDxInvoice.getInvoiceAmount().toPlainString())));
        vo.setTaxAmount(
            ((tDxInvoice.getTaxAmount() == null) ? "" : MathUtil.round(tDxInvoice.getTaxAmount().toPlainString())));
        vo.setTotalAmount(
            ((tDxInvoice.getTotalAmount() == null ? "" : MathUtil.round(tDxInvoice.getTotalAmount().toPlainString()))));
        vo.setCreateDate(tDxInvoice.getCreateDate() != null ? DateUtils.dateToStrLong(tDxInvoice.getCreateDate()) : "");
        vo.setQsDate(DateUtils.dateToStr(tDxInvoice.getQsDate()));

//        if (FpqsjgEnum.WQS.getQsjgDm().equals(qsStatus)) {
//            vo.setQsStatus(FpqsjgEnum.WQS.getQsjgMcSmb(FpqsjgEnum.WQS.getIndex()));
//        } else {
//            vo.setQsStatus(FpqsjgEnum.YQS.getQsjgMcSmb(FpqsjgEnum.YQS.getIndex()));
//        }
        vo.setQsStatus(dictdetaServcice.queryDictName(DictConstant.QS_RESULTS,qsStatus));
        vo.setUserName(tDxInvoice.getUserName());
        return vo;
    }
}
