package com.dxhy.passengerticket.service.impl;

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.common.utils.DateUtils;
import com.dxhy.passengerticket.dao.PassengerTicketDao;
import com.dxhy.passengerticket.service.PassengerTicketService;
import com.dxhy.passengerticket.vo.CertifiedInvoiceVo;
import com.dxhy.passengerticket.vo.InvoiceMainBody;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author xiexuedi
 * @date 2019/05/07 10:22:45
 */
@Service
@Slf4j
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaCollectionInitShouldAssignCapacity"})

public class PassengerTicketServiceImpl extends MpBaseServiceImpl<PassengerTicketDao, CertifiedInvoiceVo>
    implements PassengerTicketService {
    @Resource
    private PassengerTicketDao passengerTicketDao;

    @Override
    public Map<String, Object> selectcertifiedInvoiceList(Map<String, Object> params, Integer curr, Integer size,
        String dbNmae) {
        DynamicContextHolder.push(dbNmae + DbConstant.BUSINESS_READ);
        Map<String, Object> map = new HashMap<>();
        String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "开票日期", "购方税号", "购方名称", "销方税号", "销方名称", "金额", "税额", "价税合计",
            "发票状态", "签收状态", "认证状态", "税款所属期", "来源系统"};
        String[] titleprop = {"xh", "invoiceString", "invoiceCode", "invoiceNo", "invoiceDate", "gfTaxNo", "gfName",
            "xfTaxNo", "xfName", "invoiceAmount", "taxAmount", "totalAmount", "invoiceStatus", "qsStatus", "rzhYesorno",
            "dqskssq", "sourceSystem", ""};
        map.put("titlelist", titlelist);
        map.put("titleprop", titleprop);
        try {
            if ("99".equals(params.get("gfTaxNo"))) {
                params.put("gfTaxNo", "");
            }
            if (params.get("invoiceMinDate") != null) {
                String invoiceMinDate = (String)params.get("invoiceMinDate");
                params.put("invoiceMinDate", DateUtils.getMorning(DateUtils.strToDate(invoiceMinDate)));
            }
            if (params.get("invoiceMaxDate") != null) {
                String invoiceMaxDate = (String)params.get("invoiceMaxDate");
                params.put("invoiceMaxDate", DateUtils.getNight(DateUtils.strToDate(invoiceMaxDate)));
            }
            log.info("进入查询认证列表查询");
            Page<Object> page = PageHelper.startPage(curr, size);
            List<InvoiceMainBody> list = passengerTicketDao.selectcertifiedInvoiceList(params);
            List<InvoiceMainBody> listXh = new ArrayList<InvoiceMainBody>();
            int index = 1;
            List<String> uuidList = new ArrayList<>();
            for (InvoiceMainBody invoiceMainBody : list) {
                // 序号计算 = (当前页-1)*每页条数+1
                String uuid = invoiceMainBody.getUuid();
                if (!uuidList.contains(uuid)) {
                    invoiceMainBody.setXh((curr - 1) * size + index);
                    index++;
                    listXh.add(invoiceMainBody);
                }
            }
            List<InvoiceMainBody> pageList = returnInvoiceList(listXh);
            List<InvoiceMainBody> retrunList = returnThousandList(pageList);
            int total = (int)page.getTotal();
            map.put("datalist", retrunList);
            map.put("total", total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> selectOtherInvoiceList(Map<String, Object> params, Integer curr, Integer size,
        String dbName) {
        log.info("进入其他发票列表查询");
        Map<String, Object> map = new HashMap<>();
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "开票日期", "销方名称", "购方名称", "金额", "税额", "价税合计", "发票状态",
                "签收状态", "认证状态", "税款所属期", "来源系统"};
            String[] titleprop =
                {"xh", "invoiceString", "invoiceCode", "invoiceNo", "invoiceDate", "xfName", "gfName", "invoiceAmount",
                    "taxAmount", "totalAmount", "invoiceStatus", "qsStatus", "rzhYesorno", "dqskssq", "sourceSystem"};
            map.put("titlelist", titlelist);
            map.put("titleprop", titleprop);
            if ("99".equals(params.get("gfTaxNo"))) {
                params.put("gfTaxNo", "");
            }
            if (params.get("invoiceMinDate") != null) {
                String invoiceMinDate = (String)params.get("invoiceMinDate");
                params.put("invoiceMinDate", DateUtils.getMorning(DateUtils.strToDate(invoiceMinDate)));
            }
            if (params.get("invoiceMaxDate") != null) {
                String invoiceMaxDate = (String)params.get("invoiceMaxDate");
                params.put("invoiceMaxDate", DateUtils.getNight(DateUtils.strToDate(invoiceMaxDate)));
            }
            Page<Object> page = PageHelper.startPage(curr, size);
            List<InvoiceMainBody> list = passengerTicketDao.selectOtherInvoiceList(params);
            List<InvoiceMainBody> listXh = new ArrayList<InvoiceMainBody>();
            int index = 1;
            List<String> uuidList = new ArrayList<>();
            for (InvoiceMainBody invoiceMainBody : list) {
                String uuid = invoiceMainBody.getUuid();
                if (!uuidList.contains(uuid)) {
                    // 序号计算 = (当前页-1)*每页条数+1
                    invoiceMainBody.setXh((curr - 1) * size + index);
                    index++;
                    listXh.add(invoiceMainBody);
                }
            }
            List<InvoiceMainBody> pageList = returnInvoiceList(listXh);
            List<InvoiceMainBody> retrunList = returnThousandList(pageList);
            int total = (int)page.getTotal();
            map.put("datalist", retrunList);
            map.put("total", total);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

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

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

    public static 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("出租车票");
            }
            if ("92".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("火车票");
            }
            if ("93".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("飞机票");
            }
            if ("94".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("汽车票");
            }
            if ("95".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("定额发票");
            }
            if ("96".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("长途汽车票");
            }
            if ("00".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("其他");
            }
            if ("97".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("通用机打发票");
            }
            if ("98".equals(vo.getInvoiceType())) {
                vo.setInvoiceString("政府非税收收入一般缴款书");
            }
            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("其他运输客票");
            }
            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("电票上传签收");
            }
            if ("0".equals(vo.getRzhType())) {
                vo.setRzhType("手工勾选");
            }
            if ("1".equals(vo.getRzhType())) {
                vo.setRzhType("扫码勾选");
            }
            if ("2".equals(vo.getRzhType())) {
                vo.setRzhType("导入勾选");
            }
            if ("3".equals(vo.getRzhType())) {
                vo.setRzhType("智能勾选");
            }
            if ("4".equals(vo.getRzhType())) {
                vo.setRzhType("手工认证");
            }
            if ("5".equals(vo.getRzhType())) {
                vo.setRzhType("扫码认证");
            }
            if ("6".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);
            }
            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;
    }
}
