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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.common.enums.FpztEnum;
import com.dxhy.common.enums.RzztEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.CoreTDxRecordInvoiceDao;
import com.dxhy.api.model.invoice.TDxRecordInvoice;
import com.dxhy.core.model.CautionAbnormalReqVO;
import com.dxhy.core.model.CautionExpirationReqVO;
import com.dxhy.core.model.CautionMaximalReqVO;
import com.dxhy.core.model.RecordInvoiceModel;
import com.dxhy.core.service.export.IExportCautionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author user
 * @date 2019-05-05 发票预警，相关导出实现类
 */

@Service("exportCautionService")
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class ExportCautionServiceImpl extends MpBaseServiceImpl<CoreTDxRecordInvoiceDao, TDxRecordInvoice>
    implements IExportCautionService {

    /**
     * 发票预警 -> 逾期预警导出
     *
     * @return
     */
    @Override
    public PageInfo<RecordInvoiceModel> listExpiration(Page<RecordInvoiceModel> page, CautionExpirationReqVO reqVO,
        List<String> allTaxNo) {
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("gfsh", reqVO.getAllTaxNo());
        pramsMap.put("month", reqVO.getMonth());
        List<RecordInvoiceModel> pageList = super.baseMapper.queryInvoice(pramsMap);
        return new PageInfo<>(pageList);
    }

    /**
     * 异常发票预警
     *
     * @param page
     * @param reqVO
     * @param allTaxNo
     * @return
     */
    @Override
    public PageInfo<TDxRecordInvoice> listAnomaly(Page<TDxRecordInvoice> page, CautionAbnormalReqVO reqVO,
        List<String> allTaxNo) {
        // 查询参数
        QueryWrapper<TDxRecordInvoice> queryWrapper = createAnomalyQueryWrapper(reqVO);
        List<TDxRecordInvoice> list = super.list(queryWrapper);
        return new PageInfo<>(list);

    }

    /**
     * 异常发票预警导出条件
     *
     * @author 陈刚扬
     * @date 2020/10/28
     * @param reqVO
     * @return com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.dxhy.api.model.invoice.TDxRecordInvoice>
     */

    private QueryWrapper<TDxRecordInvoice> createAnomalyQueryWrapper(CautionAbnormalReqVO reqVO) {
        QueryWrapper<TDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            // 认证状态为未认证
            .eq(TDxRecordInvoice::getRzhYesorno, RzztEnum.WRZ.getRzztDm())
            // 发票状态为异常的发票
            .in(TDxRecordInvoice::getInvoiceStatus, FpztEnum.YC.getFpztDm(), FpztEnum.ZF.getFpztDm(),
                FpztEnum.HC.getFpztDm(), FpztEnum.SK.getFpztDm(),FpztEnum.HZDQR.getFpztDm(),FpztEnum.BFNC.getFpztDm(),FpztEnum.QEHC.getFpztDm());
        // 购方税号
        if (reqVO.getAllTaxNo().size() > 0) {
            queryWrapper.lambda().in(TDxRecordInvoice::getGfTaxNo, reqVO.getAllTaxNo());
        }
        // 发票代码
        if (!StringHelper.isBlank(reqVO.getInvoiceCode())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceCode, reqVO.getInvoiceCode());
        }
        // 发票号码
        if (!StringHelper.isBlank(reqVO.getInvoiceNo())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceNo, reqVO.getInvoiceNo());
        }
        // 数电号码
        if (!StringHelper.isBlank(reqVO.getEleInvoiceNo())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getEleInvoiceNo, reqVO.getEleInvoiceNo());
        }
        if (reqVO.getYcStartDate() != null && reqVO.getYcEndDate() != null) {
            queryWrapper.lambda().between(TDxRecordInvoice::getStatusUpdateDate,
                DateUtils.strToDateLong(DateUtils.getMorning(reqVO.getYcStartDate())),
                DateUtils.strToDateLong(DateUtils.getNight(reqVO.getYcEndDate())));
        }

        //发票来源
        if (StringUtils.isNotBlank(reqVO.getInvoiceSource()) && !"99".equals(reqVO.getInvoiceSource())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceSource, reqVO.getInvoiceSource());
        }
        //发票类型
        //如果是14
        if("14".equals(reqVO.getInvoiceType())){
            queryWrapper.lambda().and(
                    txf->txf.eq(TDxRecordInvoice::getInvoiceType,"14").
                            or(lqTxf->lqTxf.eq(TDxRecordInvoice::getInvoiceType,"32").eq(TDxRecordInvoice::getLqTdyslxDm, "08"))
            );
            //and (t.invoice_type = '14' or (t.invoice_type = '32' and t.lq_tdyslxDm='08') )
        }
        if("32".equals(reqVO.getInvoiceType())){
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceType,reqVO.getInvoiceType()).ne(TDxRecordInvoice::getLqTdyslxDm, "08");
            //and t.invoice_type = #{invoiceType} and t.lq_tdyslxDm!='08'
        }
        if(!"14".equals(reqVO.getInvoiceType())&&!"32".equals(reqVO.getInvoiceType())&&!"99".equals(reqVO.getInvoiceType())){
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceType,reqVO.getInvoiceType());
            //and t.invoice_type = #{invoiceType}
        }

        // 发票状态
        if (StringUtils.isNotBlank(reqVO.getInvoiceStatus()) && !"99".equals(reqVO.getInvoiceStatus())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceStatus, reqVO.getInvoiceStatus());
        }
        if (StringUtils.isNoneBlank(reqVO.getCompany()) && !"99".equals(reqVO.getCompany())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getCompany, reqVO.getCompany());
        }
        // 异常日期倒序
        queryWrapper.lambda().orderByDesc(TDxRecordInvoice::getStatusUpdateDate);

        return queryWrapper;
    }

    /**
     * 普票跨年度预警
     *
     * @return
     */
    @Override
    public PageInfo<TDxRecordInvoice> listMaximal(Page<TDxRecordInvoice> page, CautionMaximalReqVO reqVO,
        List<String> allTaxNo) {
        // 查询参数
        QueryWrapper<TDxRecordInvoice> queryWrapper = createMaximalQueryWrapper(reqVO);
        List<TDxRecordInvoice> list = super.list(queryWrapper);
        return new PageInfo<>(list);
    }

    /**
     * 普票跨年度预警导出条件
     *
     * @param reqVO
     * @return
     */
    private QueryWrapper<TDxRecordInvoice> createMaximalQueryWrapper(CautionMaximalReqVO reqVO) {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        // 今年第一天
        Date currYearFirst = DateUtils.getYearFirst(currentYear);
        log.debug("今年第一天：" + DateUtils.dateToStrLong(currYearFirst));

        // 今年最后一天
        Date currYearLast = DateUtils.getYearLast(currentYear);
        log.debug("今年最后一天：" + DateUtils.dateToStrLong(currYearLast));

        int prevYear = currentYear - 1;
        // 去年第一天
        Date prevYearFirst = DateUtils.getYearFirst(prevYear);
        log.debug("去年第一天：" + DateUtils.dateToStrLong(prevYearFirst));
        // 去年最后一天
        Date prevYearLast = DateUtils.getYearLast(prevYear);
        log.debug("去年最后一天：" + DateUtils.dateToStrLong(prevYearLast));

        QueryWrapper<TDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        // 签收日期是今年
        queryWrapper.lambda().ge(TDxRecordInvoice::getQsDate, currYearFirst)
            .le(TDxRecordInvoice::getQsDate, currYearLast)
            // 开票日期为去年
            .ge(TDxRecordInvoice::getInvoiceDate, prevYearFirst).le(TDxRecordInvoice::getInvoiceDate, prevYearLast);

        // 购方税号
        if (reqVO.getAllTaxNo().size() > 0) {
            queryWrapper.lambda().in(TDxRecordInvoice::getGfTaxNo, reqVO.getAllTaxNo());
        }

        // 发票号码
        if (!StringHelper.isBlank(reqVO.getInvoiceNo())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getInvoiceNo, reqVO.getInvoiceNo());
        }
        // 数电号码
        if (!StringHelper.isBlank(reqVO.getEleInvoiceNo())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getEleInvoiceNo, reqVO.getEleInvoiceNo());
        }
        if (StringHelper.isNotBlank(reqVO.getCompany())) {
            queryWrapper.lambda().eq(TDxRecordInvoice::getCompany, reqVO.getCompany());
        }
        // 前端只展示04，10，11的发票
        List<String> invoiceTypeList = new ArrayList<>();
        invoiceTypeList.add("04");
        invoiceTypeList.add("10");
        invoiceTypeList.add("11");
        invoiceTypeList.add("15");
        invoiceTypeList.add("32");
        invoiceTypeList.add("186");
        queryWrapper.lambda().in(TDxRecordInvoice::getInvoiceType, invoiceTypeList);
        // 签收日期倒序
        queryWrapper.lambda().orderByDesc(TDxRecordInvoice::getQsDate);
        return queryWrapper;
    }

}
