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

import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.sign.TDxInvoice;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.CoreTDxInvoiceDao;
import com.dxhy.core.model.ElecQueryReqVO;
import com.dxhy.core.model.ElecQueryRespVO;
import com.dxhy.core.service.export.IExportElecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 user
 * @date 2019-05-13 电票导出相关
 */

@Service("exportElecService")
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class ExportElecServiceImpl extends MpBaseServiceImpl<CoreTDxInvoiceDao, TDxInvoice> implements IExportElecService {

    @Resource
    private DictdetaServcice dictdetaServcice;
    /**
     * 电票查询列表导出
     *
     * @param reqVO
     * @param userAccount
     * @return
     */
    @Override
    public JSONObject listElecInvoice(ElecQueryReqVO reqVO, List<String> allTaxNo, String userAccount, int curr,
        int size) {
        // 查询参数
        JSONObject json = new JSONObject();
        Map<String, Object> pramsMap = createElecQueryWrapper(reqVO, allTaxNo, userAccount);
        PageHelper.startPage(curr, size);
        List<TDxInvoice> invoiceList = baseMapper.selectElecInvoice(pramsMap);

        PageInfo<TDxInvoice> pageInfo = new PageInfo<>(invoiceList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        List<ElecQueryRespVO> returnList = convertToData(invoiceList, curr, size);
        json.put("datalist", returnList);
        return json;
    }

    /**
     * @param reqVO
     * @param allTaxNo
     * @param userName
     * @return
     */
    private Map<String, Object> createElecQueryWrapper(ElecQueryReqVO reqVO, List<String> allTaxNo, String userName) {
        Map<String, Object> pramsMap = new HashMap<>(6);
        pramsMap.put("userName", userName);
        // 购方税号
        if (allTaxNo != null && allTaxNo.size() > 0 && !"99".equals(reqVO.getGfshFlag())) {
            pramsMap.put("gfsh", allTaxNo);
        }
        // 发票号码
        if (StringUtils.isNotBlank(reqVO.getInvoiceNo())) {
            pramsMap.put("invoiceNo", reqVO.getInvoiceNo());
        }
        // 签收时间
        if (reqVO.getQsStartDate() != null && reqVO.getQsEndDate() != null) {
            pramsMap.put("qsrqq", DateUtils.getMorning(reqVO.getQsStartDate()));
            pramsMap.put("qsrqz", DateUtils.getNight(reqVO.getQsEndDate()));
        }
        // 业务单元
        if (!"99".equals(reqVO.getBusinessCode())) {
            pramsMap.put("businessCode", reqVO.getBusinessCode());
        }
        if (StringUtils.isNotBlank(reqVO.getCompany())) {
            pramsMap.put("company", reqVO.getCompany());
        }
        return pramsMap;
    }

    /**
     * 查询出的数据转为导出data
     *
     * @param list
     * @return
     */
    private List<ElecQueryRespVO> convertToData(List<TDxInvoice> list, int curr, int size) {
        List<ElecQueryRespVO> dataList = new ArrayList<>();
        int xh = 1;
        for (TDxInvoice record : list) {
            ElecQueryRespVO vo = new ElecQueryRespVO();
            vo.setXh((curr - 1) * size + xh + "");
            xh++;
            // 签收日期
            vo.setQsDate(DateUtils.dateToStr(record.getQsDate()));
            // 签收描述
            vo.setNotes(record.getNotes());
            // 签收结果
            vo.setQsStatusName(dictdetaServcice.queryDictName(DictConstant.QS_RESULTS,record.getQsStatus()));
            // 签收人
            vo.setQsUserName(record.getUserName());
            // 发票代码
            vo.setInvoiceCode(record.getInvoiceCode());
            // 发票号码
            vo.setInvoiceNo(record.getInvoiceNo());
            // 发票类型
            vo.setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,record.getInvoiceType()));
//            if ("10".equals(record.getInvoiceType())) {
//                vo.setInvoiceType("增值税电子普通发票");
//            } else if ("14".equals(record.getInvoiceType())) {
//                vo.setInvoiceType("增值税电子普通发票（通行费）");
//            } else if ("08".equals(record.getInvoiceType())) {
//                vo.setInvoiceType("增值税电子专用发票");
//            }

            // 开票日期
            vo.setInvoiceDate(DateUtils.dateToStr(record.getInvoiceDate()));
            // 购方名称
            vo.setGfName(record.getGfName());
            vo.setXfName(record.getXfName());
            // 金额
            if (record.getInvoiceAmount() != null) {
                vo.setAmount(MathUtil.round(record.getInvoiceAmount().toPlainString()));
            }
            // 税额
            if (record.getTaxAmount() != null) {
                vo.setTaxAmount(MathUtil.round(record.getTaxAmount().toPlainString()));
            }
            vo.setBusinessName(record.getDeptName());
            dataList.add(vo);

        }
        return dataList;
    }

}
