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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxhy.api.model.db.TdxTdysMhxcdEntity;
import com.dxhy.api.model.db.TdxTdysMhxcdMxEntity;
import com.dxhy.api.model.db.TdxTdysTldzkpEntity;
import com.dxhy.api.model.invoice.invoiceCheck.CoreTDxCheckInvoiceDetail;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.CoreTDxCheckHistoryDetailDao;
import com.dxhy.core.service.export.InvoiceCheckDetailService;
import com.dxhy.jxpt.baseService.dao.TdxTdysMhxcdCoreDao;
import com.dxhy.jxpt.baseService.dao.TdxTdysMhxcdMxCoreDao;
import com.dxhy.jxpt.baseService.dao.TdxTdysTldzkpCoreDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 查验业务处理接口实现类
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("invoiceCheckDetailService")
@Slf4j

public class InvoiceCheckDetailServiceImpl extends MpBaseServiceImpl<CoreTDxCheckHistoryDetailDao, CoreTDxCheckInvoiceDetail>
    implements InvoiceCheckDetailService {
    @Resource
    private DictdetaServcice dictdetaServcice;
    @Resource
    private TdxTdysTldzkpCoreDao tdxTdysTldzkpCoreDao;
    @Resource
    private TdxTdysMhxcdCoreDao tdxTdysMhxcdCoreDao;
    @Resource
    private TdxTdysMhxcdMxCoreDao tdxTdysMhxcdMxCoreDao;

    @Override
    public JSONObject getCheckHistoryDetail(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = new JSONObject();

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<CoreTDxCheckInvoiceDetail> pageList = super.baseMapper.selectCheckHistoryDetail(pramsMap);
        PageInfo<CoreTDxCheckInvoiceDetail> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        List<CoreTDxCheckInvoiceDetail> resultList = pageInfo.getList();

        List<String[]> returnList = new ArrayList<>();

        if (resultList != null && resultList.size() > 0) {

            for (int i = 0; i < resultList.size(); i++) {
                String[] dataArray = new String[39];
                dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                dataArray[1] = resultList.get(i).getFPDM();
                dataArray[2] = resultList.get(i).getFPHM();
                dataArray[3] = resultList.get(i).getEleInvoiceNo();
                // 发票类型
                dataArray[4] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, resultList.get(i).getFPZL());
//                dataArray[3] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE,resultList.get(i).getFPZL());
                dataArray[5] =
                    (resultList.get(i).getKPRQ() == null) ? "" : DateUtils.dateToStr(resultList.get(i).getKPRQ());
                dataArray[6] = StringHelper.trim(resultList.get(i).getGFMC(), "");
                dataArray[7] = StringHelper.trim(resultList.get(i).getGFSH(), "");
                dataArray[8] = StringHelper.trim(resultList.get(i).getXFMC(), "");
                dataArray[9] = StringHelper.trim(resultList.get(i).getXFSH(), "");
                if ("N".equals(resultList.get(i).getZFBZ()) || "0".equals(resultList.get(i).getZFBZ())) {
                    dataArray[10] = "正常";
                } else if ("Y".equals(resultList.get(i).getZFBZ()) || "2".equals(resultList.get(i).getZFBZ())) {
                    dataArray[10] = "作废";
                } else if ("3".equals(resultList.get(i).getZFBZ())) {
                    dataArray[10] = "红冲";
                } else {
                    dataArray[10] = "";
                }

                try {
                    dataArray[11] =
                            (StringUtils.isBlank(resultList.get(i).getJE())) ? "" : MathUtil.round(resultList.get(i).getJE());
                }catch (Exception e){
                    dataArray[11]="";
                }
                try{
                    dataArray[12] =
                            (StringUtils.isBlank(resultList.get(i).getSE())) ? "" : MathUtil.round(resultList.get(i).getSE());
                }catch (Exception e){
                    dataArray[12]="";
                }
                try{
                    dataArray[13] = (StringUtils.isBlank(resultList.get(i).getJSHJ())) ? ""
                            : MathUtil.round(resultList.get(i).getJSHJ());
                }catch (Exception e){
                    dataArray[13]="";
                }
                
                dataArray[14] = resultList.get(i).getCYJGTIP();
                dataArray[15] = resultList.get(i).getCreateDate();
                dataArray[16] = resultList.get(i).getCheckUser();
                dataArray[17] = resultList.get(i).getBZ();
                dataArray[18] = resultList.get(i).getGFDZDH();
                dataArray[19] = resultList.get(i).getGFYHZH();
                dataArray[20] = resultList.get(i).getXFDZDH();
                dataArray[21] = resultList.get(i).getXFYHZH();
                dataArray[22] = resultList.get(i).getGoodsName();
                dataArray[23] = resultList.get(i).getMODEL();
                dataArray[24] = resultList.get(i).getUNIT();
                dataArray[25] = resultList.get(i).getNUM();
                dataArray[26] = resultList.get(i).getUnitPrice();
                dataArray[27] = resultList.get(i).getDetailAmount();
                dataArray[28] = resultList.get(i).getDetailTaxAmount();

                dataArray[29] = resultList.get(i).getTaxRate();
                dataArray[30] = resultList.get(i).getCph();
                dataArray[31] = resultList.get(i).getLx();
                dataArray[32] = resultList.get(i).getTxrqq();
                dataArray[33] = resultList.get(i).getTxrqz();

                //"乘坐人","证件号","车次/班次","席别/座位等级","出发/起飞时间"
                if("161".equals(resultList.get(i).getFPZL())){
                    TdxTdysMhxcdEntity tdxTdysMhxcdEntity = tdxTdysMhxcdCoreDao.selectOne(new LambdaQueryWrapper<TdxTdysMhxcdEntity>().eq(TdxTdysMhxcdEntity::getUuid, resultList.get(i).getUuid()));
                    if(ObjectUtils.isNotEmpty(tdxTdysMhxcdEntity)){
                        dataArray[34] = tdxTdysMhxcdEntity.getXm();
                        dataArray[35] = tdxTdysMhxcdEntity.getZjhm();
                        //航班号从明细取
                        List<TdxTdysMhxcdMxEntity> mhxcdMxEntityList = tdxTdysMhxcdMxCoreDao.selectList(new LambdaQueryWrapper<TdxTdysMhxcdMxEntity>().eq(TdxTdysMhxcdMxEntity::getTdysId, resultList.get(i).getUuid()));
                        if(ObjectUtils.isNotEmpty(mhxcdMxEntityList)){
                            TdxTdysMhxcdMxEntity tdxTdysMhxcdMxEntity = mhxcdMxEntityList.get(0);
                            dataArray[36] = tdxTdysMhxcdMxEntity.getHbh();
                            dataArray[37] = tdxTdysMhxcdMxEntity.getZwdj();
                            dataArray[38] = tdxTdysMhxcdMxEntity.getCyrq()+" "+tdxTdysMhxcdMxEntity.getQfsj();
                        }else{
                            dataArray[36] = "";
                            dataArray[37] = "";
                            dataArray[38] = "";
                        }
                    }
                }
                if("283".equals(resultList.get(i).getFPZL())){
                    TdxTdysTldzkpEntity tdxTdysTldzkpEntity = tdxTdysTldzkpCoreDao.selectOne(new LambdaQueryWrapper<TdxTdysTldzkpEntity>().eq(TdxTdysTldzkpEntity::getUuid, resultList.get(i).getUuid()));
                    if(ObjectUtils.isNotEmpty(tdxTdysTldzkpEntity)){
                        dataArray[34] = tdxTdysTldzkpEntity.getXm();
                        dataArray[35] = tdxTdysTldzkpEntity.getZjhm();
                        dataArray[36] = tdxTdysTldzkpEntity.getCccc();
                        dataArray[37] = tdxTdysTldzkpEntity.getXbl();
                        dataArray[38] = (StringUtils.isBlank(tdxTdysTldzkpEntity.getRq())?"":(DateUtils.strToStrDate(tdxTdysTldzkpEntity.getRq())+" "))+tdxTdysTldzkpEntity.getCfsjl();
                    }
                }

                returnList.add(dataArray);
            }
        }
        json.put("datalist", returnList);
        return json;
    }
}
