package com.dxhy.extend.service.companypool;

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 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.enums.FpqsjgEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.extend.dao.ExtTicketDao;
import com.dxhy.extend.entity.ExtTicketInvoiceModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author futianhao
 * @date 2020-08-28 企业票池首页
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
@Slf4j
public class TicketServiceImpl extends MpBaseServiceImpl<ExtTicketDao, ExtTicketInvoiceModel> implements TicketService {
    @Resource
    private ExtTicketDao extTicketDao;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String queryList(Map<String, Object> paramsMap, int curr, int size) {
        long enter = System.currentTimeMillis();
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "开票日期", "销方名称", "乘坐人", "证件号", "班次/车次", "金额",
                "签收状态", "签收日期", "签收方式", "签收人", "抵扣状态", "业务单号", "入池时间","凭证号", "过账时间", "影像号",
                    "账期","报账单号","报账人","入账状态","所属组织"};

            String[] titleprop = {"xh", "invoiceTypeString", "invoiceCode", "invoiceNo", "invoiceDate", "xfName",
                "passenger", "passengerSfzh", "trainNumber", "invoiceAmount", "qsStatus",
                "qsDate", "qsType", "qsName", "isDeduction", "businessDocumentNumber", "createDate","snVoucherNumber",
                    "postingTime","imageId","accountPeriod","bzdh","bzr","inAccountStatus","compCode"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                log.info("查询实名客票总数参数 {}",paramsMap);
                List<ExtTicketInvoiceModel> hjList = super.baseMapper.queryTotal(paramsMap);
                ExtTicketInvoiceModel hjxx = hjList.get(0);
                hjsl = hjxx.getId();
                if (!"0".equals(hjsl)) {
                    hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                    hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
                log.info("实名客票-查询首页合计信息耗时:{}",System.currentTimeMillis()-enter);
            }
            PageHelper.startPage(curr, size);
            log.info("查询实名客票参数 {}",paramsMap);
            List<ExtTicketInvoiceModel> pageList = super.baseMapper.queryTickets(paramsMap);
            PageInfo<ExtTicketInvoiceModel> pageInfo = new PageInfo<>(pageList);
            log.info("实名客票-查询列表信息耗时:{}",System.currentTimeMillis()-enter);
            // 用于页面返回list对象
            if (pageList.size() > 0) {

                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));

                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQsStatus())) {
                            pageList.get(i).setQsStatus(e.getQsjgMc(e.getIndex()));
                        }
                    }

                    for (QsfsEnum e : QsfsEnum.values()) {
                        if (e.getQsfsDm().equals(pageList.get(i).getQsType())) {
                            pageList.get(i).setQsType(e.getQsfsMc(e.getIndex()));
                        }
                    }
                    if ("接口推送".equals(pageList.get(i).getQsType())) {
                        pageList.get(i).setQsType("接口签收");
                    }
                    if (pageList.get(i).getInvoiceDate() != null) {
                        if (pageList.get(i).getInvoiceDate().length() > 0) {
                            pageList.get(i).setInvoiceDate(pageList.get(i).getInvoiceDate().substring(0, 10));
                        }
                    }
                    if (pageList.get(i).getQsDate() != null) {
                        if (pageList.get(i).getQsDate().length() > 0) {
                            pageList.get(i).setQsDate(pageList.get(i).getQsDate().substring(0, 10));
                        }
                    }

                    if ("1".equals(pageList.get(i).getIsDeduction())) {
                        pageList.get(i).setIsDeduction("不可抵扣");
                    } else if ("0".equals(pageList.get(i).getIsDeduction())) {
                        pageList.get(i).setIsDeduction("可抵扣");
                    }
                    if ("93".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("飞机票");
                    }
                    if ("10".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("增值税电子普通发票");
                    }
                    if ("92".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("火车票");
                    }
                    if ("90".equals(pageList.get(i).getInvoiceType())) {
//                        pageList.get(i).setInvoiceType("93");
                        pageList.get(i).setInvoiceTypeString("航空客运电子客票行程单");
                    }
                    if ("89".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("公路运输客票(实名)");
                    }
                    if ("88".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("水路运输客票(实名)");
                    }
                    if ("87".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setInvoiceTypeString("其他运输客票(实名)");
                    }
                    if (StringUtils.isNotBlank(pageList.get(i).getCreateDate())) {
                        pageList.get(i).setCreateDate(pageList.get(i).getCreateDate().substring(0, 10));
                    }
                    //报账单号字典值转换
                    if ("1".equals(pageList.get(i).getInAccountStatus())){
                        pageList.get(i).setInAccountStatus("已入账");
                    }else {
                        pageList.get(i).setInAccountStatus("未入账");
                    }
                    log.info("入账状态====>{}",pageList.get(i).getInAccountStatus());
//                    pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS,pageList.get(i).getInAccountStatus()));
//                    log.info("入账状态====>{}",pageList.get(i).getInAccountStatus());

                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtTicketInvoiceModel> resultList = pageInfo.getList();
            json.put("datalist", resultList);
            log.info("实名客票-合计耗时:{}",System.currentTimeMillis()-enter);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

}
