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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
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.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.TDxCheckListDao;
import com.dxhy.core.entity.TDxCheckInvoiceDetail;
import com.dxhy.core.model.CheckInvoiceVO;
import com.dxhy.core.service.export.InvoiceCheckListService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

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

public class InvoiceCheckListServiceImpl extends MpBaseServiceImpl<TDxCheckListDao, TDxCheckInvoiceDetail>
    implements InvoiceCheckListService {

    @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<TDxCheckInvoiceDetail> pageList = super.baseMapper.selectCheckList(pramsMap);
        PageInfo<TDxCheckInvoiceDetail> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        List<TDxCheckInvoiceDetail> resultList = pageInfo.getList();

        List<CheckInvoiceVO> returnList = new ArrayList<>();

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

            for (int i = 0; i < resultList.size(); i++) {
                CheckInvoiceVO vo = new CheckInvoiceVO();
                vo.setXh(String.valueOf((curr - 1) * size + i + 1));
                vo.setInvoiceCode(resultList.get(i).getFPDM());
                vo.setInvoiceNo(resultList.get(i).getFPHM());
                vo.setInvoiceDate(
                    (resultList.get(i).getKPRQ() == null) ? "" : DateUtils.dateToStr(resultList.get(i).getKPRQ()));
                vo.setGfmc(StringHelper.trim(resultList.get(i).getGFMC(), ""));
                vo.setXfmc(StringHelper.trim(resultList.get(i).getXFMC(), ""));
                vo.setInvoiceType(new InvoiceTypeUtils().invoiceTypeName(resultList.get(i).getFPZL()));

                if ("N".equals(resultList.get(i).getZFBZ()) || "0".equals(resultList.get(i).getZFBZ())) {
                    vo.setInvoiceStatus("正常");
                } else if ("Y".equals(resultList.get(i).getZFBZ()) || "2".equals(resultList.get(i).getZFBZ())) {
                    vo.setInvoiceStatus("作废");
                } else if ("3".equals(resultList.get(i).getZFBZ())) {
                    vo.setInvoiceStatus("红冲");
                } else {
                    vo.setInvoiceStatus("");
                }

                try{
                    vo.setInvoiceAmount(
                            (StringUtils.isBlank(resultList.get(i).getJE())) ? "" : MathUtil.round(resultList.get(i).getJE()));
                }catch (Exception e){
                    vo.setInvoiceAmount("");
                }
                try{
                    vo.setTaxAmount(
                            (StringUtils.isBlank(resultList.get(i).getSE())) ? "" : MathUtil.round(resultList.get(i).getSE()));
                }catch (Exception e){
                    vo.setTaxAmount("");
                }
                try{
                    vo.setTotalAmount((StringUtils.isBlank(resultList.get(i).getJSHJ())) ? ""
                            : MathUtil.round(resultList.get(i).getJSHJ()));
                }catch (Exception e){
                    vo.setTotalAmount("");
                }

                vo.setCheckMassege(resultList.get(i).getCYJGTIP());
                vo.setCheckUser(resultList.get(i).getCheckUser());
                vo.setHandleDate(resultList.get(i).getCreateDate());

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