package com.dxhy.passengerticket.service.impl;

import cn.hutool.core.date.DateUtil;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.passengerticket.dao.PassengerInvoiceDetailDao;
import com.dxhy.passengerticket.dao.PassengerReimbursementSystemDao;
import com.dxhy.passengerticket.dao.PassengerVehicleDetailDao;
import com.dxhy.passengerticket.entity.TdxInvoiceReimburse;
import com.dxhy.passengerticket.entity.TdxRecordInvoiceDetail;
import com.dxhy.passengerticket.entity.TdxVehicleSaleInvoice;
import com.dxhy.passengerticket.service.PassengerReimbursementSystemService;
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 org.springframework.transaction.annotation.Transactional;

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/10 09:45:44
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaCollectionInitShouldAssignCapacity"})
@Service
@Slf4j

public class PassengerReimbursementSystemServiceImpl
    extends MpBaseServiceImpl<PassengerReimbursementSystemDao, TdxInvoiceReimburse>
    implements PassengerReimbursementSystemService {
    @Resource
    private PassengerReimbursementSystemDao passengerReimbursementSystemDao;
    @Resource
    private PassengerInvoiceDetailDao invoivieDetailDao;
    @Resource
    private PassengerVehicleDetailDao passengerVehicleDetailDao;

    @Override
    public Integer insertReimbursementSystem(TdxInvoiceReimburse vo, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        String time = DateUtil.now();
        vo.setCreateDate(time);
        vo.setQsDate(time);
        vo.setQsStatus("1");
        vo.setQsName(vo.getBusinessDocumentMaker());

        return reimbursementSystem(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer reimbursementSystem(TdxInvoiceReimburse vo) {
        return passengerReimbursementSystemDao.insert(vo);
    }

    @Override
    public Map<String, Object> selectpassengerTicketList(Map<String, Object> params, Integer curr, Integer size,
        String dbName) {
        Map<String, Object> map = new HashMap<>();
        try {

            Page<Object> page = PageHelper.startPage(curr, size);
            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)));
            }
            if (params.get("qsDateMin") != null) {
                String qsDateMin = (String)params.get("qsDateMin");
                params.put("qsDateMin", DateUtils.getMorning(DateUtils.strToDate(qsDateMin)));
            }
            if (params.get("qsDateMax") != null) {
                String qsDateMax = (String)params.get("qsDateMax");
                params.put("qsDateMax", DateUtils.getNight(DateUtils.strToDate(qsDateMax)));
            }

            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
            List<InvoiceMainBody> list = passengerReimbursementSystemDao.selectpassengerTicketList(params);
            List<InvoiceMainBody> listXh = new ArrayList<>();
            int index = 1;
            for (InvoiceMainBody invoiceMainBody : list) {
                // 序号计算 = (当前页-1)*每页条数+1
                invoiceMainBody.setXh((curr - 1) * size + index);
                index++;
                listXh.add(invoiceMainBody);
            }
            List<InvoiceMainBody> pageList = PassengerTicketServiceImpl.returnInvoiceList(listXh);
            List<InvoiceMainBody> returnList = PassengerTicketServiceImpl.returnThousandList(pageList);
            int total = (int)page.getTotal();
            map.put("datalist", returnList);
            map.put("total", total);
            String[] titlelist = {"序号", "发票类型", "开票时间", "客票号码", "乘坐人", "金额", "税额", "价税合计", "发票状态", "税款所属期", "来源系统"};
            String[] titleprop = {"xh", "invoiceString", "invoiceDate", "ticketNumber", "passenger", "invoiceAmount",
                "taxAmount", "totalAmount", "invoiceStatus", "dqskssq", "sourceSystem"};
            map.put("titlelist", titlelist);
            map.put("titleprop", titleprop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public TdxInvoiceReimburse selectPassengerTicketDetails(String selectId, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        return passengerReimbursementSystemDao.selectPassengerTicketDetails(selectId);
    }

    @Override
    public boolean selectUuid(String uuid, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        String flag = passengerReimbursementSystemDao.selectUuid(uuid);
        return "".equals(flag) || flag == null;
    }

    @Override
    public void addInvoiceDetails(List<TdxRecordInvoiceDetail> tdxRecordInvoiceDetailList, String uuid, String dbName,
                                  String invoiceCode, String incoiceNo) {
        DynamicContextHolder.push(dbName + DbConstant.DETAIL_WRITE);
        for (TdxRecordInvoiceDetail invoiceDetail : tdxRecordInvoiceDetailList) {
            invoiceDetail.setUuid(uuid);
            invoiceDetail.setInvoiceCode(invoiceCode);
            invoiceDetail.setInvoiceNo(incoiceNo);
            Aops.getSelf(this).invoiceDetails(invoiceDetail);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void invoiceDetails(TdxRecordInvoiceDetail invoiceDetail) {
        invoivieDetailDao.insert(invoiceDetail);
    }

    @Override
    public void updateiInsertReimbursement(String selectId, String reimbursementStatus, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        passengerReimbursementSystemDao.updateiInsertReimbursement(selectId, reimbursementStatus);
    }

    @Override
    public String selectDbName(String gfTaxNo) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return passengerReimbursementSystemDao.selectOrgType(gfTaxNo);
    }

    @Override
    public void insertVehicleDetail(List<TdxVehicleSaleInvoice> invoiceDetailsList, String uuid, String dbNmae) {
        DynamicContextHolder.push(dbNmae + DbConstant.BUSINESS_WRITE);
        for (TdxVehicleSaleInvoice invoiceDetail : invoiceDetailsList) {
            String time = DateUtil.now();
            invoiceDetail.setCreateDate(time);
            invoiceDetail.setUuid(uuid);
            Aops.getSelf(this).vehicleDetail(invoiceDetail);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void vehicleDetail(TdxVehicleSaleInvoice invoiceDetail) {
        passengerVehicleDetailDao.insert(invoiceDetail);
    }

    @Override
    public TdxInvoiceReimburse deleteInsertReimbursement(String selectId, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        return passengerReimbursementSystemDao.selectPassengerTicketDetails(selectId);
    }

    @Override
    public void deleteVehicleDetail(String uuid, String dbNmae) {
        DynamicContextHolder.push(dbNmae + DbConstant.BUSINESS_WRITE);
        passengerReimbursementSystemDao.deleteVehicleDetail(uuid);
    }

    @Override
    public void deleteInvoiceDetails(String uuid, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.DETAIL_WRITE);
        passengerReimbursementSystemDao.deleteInvoiceDetails(uuid);
    }

    @Override
    public void delectReimbursementSystem(String selectId, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        passengerReimbursementSystemDao.delectReimbursementSystem(selectId);
    }

}
