package com.dxhy.core.service.invoicedetail;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.core.util.InvoiceTypeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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.AmountFormatUtil;
import com.dxhy.core.dao.OtherInvoiceDetailDao;
import com.dxhy.core.dao.TDxRecordInvoiceDao;
import com.dxhy.core.entity.InvoiceMainBody;
import com.dxhy.core.entity.TDxRecordInvoice;

/**
 * 功能描述
 *
 * @author 陈刚扬
 * @date 2020/10/30
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service
public class OtherInvoiceDetailServiceImpl extends MpBaseServiceImpl<TDxRecordInvoiceDao, TDxRecordInvoice>
    implements OtherInvoiceDetailService {

    @Resource
    private OtherInvoiceDetailDao otherInvoiceDetailDao;

    @Resource
    private  DictdetaServcice dictdetaServcice;

    @Override
    public List<InvoiceMainBody> selectPassengerTheme(String selectId, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        List<InvoiceMainBody> list = otherInvoiceDetailDao.selectPassengerTheme(selectId);
        List<InvoiceMainBody> returnList = returnInvoiceList(list);
        return returnThousandList(returnList);
    }

    @Override
    public List<InvoiceMainBody> selectInvoiceMainBody(String invoiceNo, String invoiceCode, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        List<InvoiceMainBody> list = otherInvoiceDetailDao.selectInvoiceMainBody(invoiceNo, invoiceCode);
        return returnInvoiceList(list);
    }

    public  List<InvoiceMainBody> returnInvoiceList(List<InvoiceMainBody> list) {
        List<InvoiceMainBody> pageList = new ArrayList<>();
        for (InvoiceMainBody vo : list) {
//            if ("01".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("增值税专用发票");
//            }
//            if ("04".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("增值税普通发票");
//            }
//            if ("10".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("增值税普通发票（电子）");
//            }
//            if ("11".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("增值税普通发票（卷式）");
//            }
//            if ("03".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("机动车销售统一发票");
//            }
//            if ("02".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("货运运输业增值税专用发票");
//            }
//            if ("91".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("出租车票");
//                vo.setInvoiceTypeCode("出租车票");
//            }
//            if ("92".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("火车票");
//            }
//            if ("93".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("飞机票");
//            }
//            if ("94".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("汽车票");
//                vo.setInvoiceTypeCode("汽车票");
//            }
//            if ("95".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("定额发票");
//                vo.setInvoiceTypeCode("定额发票");
//            }
//            if ("96".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("长途汽车票");
//            }
//            if ("00".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("其他");
//                vo.setInvoiceTypeCode("其他");
//            }
//            if ("97".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("通用机打发票");
//                vo.setInvoiceTypeCode("通用机打发票");
//            }
//            if ("98".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("政府非税收收入一般缴款书");
//                vo.setInvoiceTypeCode("政府非税收收入一般缴款书");
//            }
//            if ("14".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("增值税电子普通发票（通行费）");
//            }
//            if ("90".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("航空运输客票");
//            }
//            if ("89".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("公路运输客票");
//            }
//            if ("88".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("水路运输客票");
//            }
//            if ("87".equals(vo.getInvoiceType())) {
//                vo.setInvoiceString("其他运输客票");
//            }
            String invoiceType = vo.getInvoiceType();
            vo.setInvoiceString(new InvoiceTypeUtils().invoiceTypeName(invoiceType));
            vo.setInvoiceTypeCode(new InvoiceTypeUtils().invoiceTypeName(invoiceType));
            if ("0".equals(vo.getQsStatus())) {
                vo.setQsStatus("未签收");
            }
            if ("1".equals(vo.getQsStatus())) {
                vo.setQsStatus("已签收");
            }
            if ("0".equals(vo.getInvoiceStatus())) {
                vo.setInvoiceStatus("正常");
            }
            if ("1".equals(vo.getInvoiceStatus())) {
                vo.setInvoiceStatus("失控");
            }
            if ("2".equals(vo.getInvoiceStatus())) {
                vo.setInvoiceStatus("作废");
            }
            if ("3".equals(vo.getInvoiceStatus())) {
                vo.setInvoiceStatus("红冲");
            }
            if ("4".equals(vo.getInvoiceStatus())) {
                vo.setInvoiceStatus("异常");
            }
            if ("0".equals(vo.getRzhYesorno())) {
                vo.setRzhYesorno("未认证");
            }
            if ("1".equals(vo.getRzhYesorno())) {
                vo.setRzhYesorno("已认证");
            }
            if ("0".equals(vo.getSourceSystem())) {
                vo.setSourceSystem("采集");
            }
            if ("1".equals(vo.getSourceSystem())) {
                vo.setSourceSystem("查验");
            }
            if ("2".equals(vo.getSourceSystem())) {
                vo.setSourceSystem("录入");
            }
            if ("2".equals(vo.getReimbursementStatus())) {
                vo.setReimbursementStatus("报销中");
            }
            if ("3".equals(vo.getReimbursementStatus())) {
                vo.setReimbursementStatus("已报销");
            }
            if ("0".equals(vo.getQsType())) {
                vo.setQsType("扫码签收");
            }
            if ("1".equals(vo.getQsType())) {
                vo.setQsType("扫描仪签收");
            }
            if ("2".equals(vo.getQsType())) {
                vo.setQsType("app签收");
            }
            if ("3".equals(vo.getQsType())) {
                vo.setQsType("导入签收");
            }
            if ("4".equals(vo.getQsType())) {
                vo.setQsType("手工签收");
            }
            if ("5".equals(vo.getQsType())) {
                vo.setQsType("pdf上传签收");
            }
            if ("1".equals(vo.getRzhType())) {
                vo.setRzhType("勾选认证");
            }
            if ("2".equals(vo.getRzhType())) {
                vo.setRzhType("扫描认证");
            }
            if ("0".equals(vo.getIsDeduction())) {
                vo.setIsDeduction("可抵扣");
            }
            if ("1".equals(vo.getIsDeduction())) {
                vo.setIsDeduction("不可抵扣");
            }
            if (!"".equals(vo.getInvoiceDate()) && vo.getInvoiceDate() != null) {
                String s = vo.getInvoiceDate().substring(0, 11);
                vo.setInvoiceDate(s);
            }
            if (!"".equals(vo.getCreateDate()) && vo.getCreateDate() != null) {
                String s = vo.getCreateDate().substring(0, 11);
                vo.setCreateDate(s);
            }
            if (!"".equals(vo.getQsDate()) && vo.getQsDate() != null) {
                String s = vo.getQsDate().substring(0, 11);
                vo.setQsDate(s);
            }
            if (!"".equals(vo.getReimbursementTime()) && vo.getReimbursementTime() != null) {
                String s = vo.getReimbursementTime().substring(0, 11);
                vo.setReimbursementTime(s);
            }
            if (!"".equals(vo.getTicketDepartureTime()) && vo.getTicketDepartureTime() != null) {
                String s = vo.getTicketDepartureTime().substring(0, 11);
                vo.setTicketDepartureTime(s);
            }
            if (!"".equals(vo.getTicketArrivalTime()) && vo.getTicketArrivalTime() != null) {
                String s = vo.getTicketArrivalTime().substring(0, 11);
                vo.setTicketArrivalTime(s);
            }
            if(StringUtils.isNotBlank(vo.getCurrency())){
                vo.setCurrency(dictdetaServcice.queryDictName(DictConstant.Currency, vo.getCurrency()));
            }
            pageList.add(vo);
        }
        return pageList;
    }

    public static List<InvoiceMainBody> returnThousandList(List<InvoiceMainBody> list) {
        List<InvoiceMainBody> pageList = new ArrayList<>();
        for (InvoiceMainBody vo : list) {
            if (vo.getInvoiceAmount() != null) {
                vo.setInvoiceAmount(AmountFormatUtil.fmtMicrometer(vo.getInvoiceAmount()));
            }
            if (vo.getTaxAmount() != null) {
                vo.setTaxAmount(AmountFormatUtil.fmtMicrometer(vo.getTaxAmount()));
            }
            if (vo.getTotalAmount() != null) {
                vo.setTotalAmount(AmountFormatUtil.fmtMicrometer(vo.getTotalAmount()));
            }
            pageList.add(vo);
        }
        return pageList;
    }
}
