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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
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.AmountFormatUtil;
import com.dxhy.core.dao.CustomsDao;
import com.dxhy.core.model.JkszhcxRespVO;
import com.dxhy.core.service.export.CustomsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 海关缴款书综合查询
 *
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("customsService")
@Slf4j

public class CustomsServiceImpl extends MpBaseServiceImpl<CustomsDao, JkszhcxRespVO> implements CustomsService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            PageHelper.startPage(curr, size);
            List<JkszhcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);

            PageInfo<JkszhcxRespVO> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            List<String[]> returnList = new ArrayList<>();
            String[] titleColumns = {"序号", "缴款书号码", "管理状态", "填发日期", "缴款单位名称", "缴款单位税号", "税款金额", "抵扣税款金额", "稽核结果",
                "是否申请核对", "核对结果", "认证状态", "认证类型", "认证日期", "认证方式", "认证人", "采集状态", "底账库采集时间", "采集来源", "签收方式", "签收状态",
                "签收日期", "签收人", "入账月份", "入账状态", "凭证号", "业务单元", "入池时间","凭证号","入账时间","账期","影像id"};
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                for (int i = 0; i < pageList.size(); i++) {
                    JkszhcxRespVO vo = pageList.get(i);
                    String[] dataArray = new String[37];
                    // xh
                    dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                    // 缴款书号码
                    dataArray[1] = vo.getCustomsCode();
                    // 管理状态
                    if (vo.getGlzt() != null && "0".equals(vo.getGlzt())){
                        dataArray[2] = "正常";
                    }else if (vo.getGlzt() != null && "1".equals(vo.getGlzt())){
                        dataArray[2] = "异常";
                    }else if (vo.getGlzt() != null && "2".equals(vo.getGlzt())){
                        dataArray[2] = "疑似非正常";
                    }
//                    dataArray[2] = dictdetaServcice.queryDictName(DictConstant.GLZT, vo.getGlzt());
                    // 填发日期
                    dataArray[3] = vo.getFillInDate();
                    // 缴款单位名称
                    dataArray[4] = vo.getGfName();
                    // 缴款单位税号
                    dataArray[5] = vo.getGfTaxNo();
                    // 税款金额
                    dataArray[6] = AmountFormatUtil.fmtMicrometer(vo.getTaxAmount());
                    // 抵扣税款金额
                    dataArray[7] = AmountFormatUtil.fmtMicrometer(vo.getYxTaxAmount());
                    // 稽核结果
                    dataArray[8] = dictdetaServcice.queryDictName(DictConstant.AUDIT_STATUS, vo.getAuditStatus());
                    // 是否申请核对
                    dataArray[9] = dictdetaServcice.queryDictName(DictConstant.APPLY_CHECK, vo.getApplyCheck());
                    // 核对结果
                    dataArray[10] = dictdetaServcice.queryDictName(DictConstant.CHECK_STATUS, vo.getCheckStatus());
                    // 认证状态
                    dataArray[11] = dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, vo.getRzhYesorno());
                    // 认证类型
//                    dataArray[12] = dictdetaServcice.queryDictName(DictConstant.INVOICE_RZLX, vo.getRzlx());
                    if("0".equals(vo.getBdkStatus())){
                        dataArray[12] ="";
                    }else {
                        dataArray[12] = dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, vo.getBdkStatus());
                    }
                    // 认证日期
                    dataArray[13] = vo.getRzhDate();
                    // 认证方式
                    dataArray[14] = dictdetaServcice.queryDictName(DictConstant.DEDU_TYPE, vo.getRzhType());
                    // 认证人
                    dataArray[15] = vo.getConfirmUser();
                    // 采集状态
                    dataArray[16] = vo.getCreateDate() == null ? "未采集" : "已采集";
                    // 底账库采集时间
                    dataArray[17] = vo.getCreateDate() == null ? "" : vo.getCreateDate().trim().substring(0, 11);
                    // 采集来源
                    dataArray[18] = dictdetaServcice.queryDictName(DictConstant.JKSCJFS, vo.getDataSources());
                    // 签收方式
                    dataArray[19] = dictdetaServcice.queryDictName(DictConstant.QS_TYPE, vo.getQsType());
                    // 签收状态
                    dataArray[20] = dictdetaServcice.queryDictName(DictConstant.QS_STATUS, vo.getQsStatus());

                    // 签收日期
                    dataArray[21] = vo.getQsDate();
                    // 签收人
                    dataArray[22] = vo.getQsUser();
                    // 入账月份
                    dataArray[23] =
                        vo.getInAccountDate() == null ? "" : vo.getInAccountDate().replaceAll("-", "").substring(0, 6);
                    // 入账状态
//                    dataArray[24] =
//                        dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS, vo.getInAccountStatus());

                    // 凭证号
//                    dataArray[24] = vo.getVoucherNumber();
                    // 业务单元
                    dataArray[24] = vo.getBusinessName();
                    // 入池时间
                    dataArray[25] = vo.getCreateDate() == null ? "" : vo.getCreateDate().trim().substring(0, 11);
                    dataArray[26] = vo.getSnVoucherNumber();
                    dataArray[27] = vo.getPostingTime();
                    dataArray[29] = vo.getAccountPeriod();
                    dataArray[28] = vo.getImageId();
                    dataArray[30] = vo.getBzdh();
                    dataArray[31] = vo.getBzr();
                    if (vo.getInAccountStatus() != null && "1".equals(vo.getInAccountStatus())){
                        dataArray[32] = "已入账";
                    }else {
                        dataArray[32] = "未入账";
                    }
                    dataArray[33] = vo.getCompCode();
                    returnList.add(dataArray);
                }
            }
            json.put("datalist", returnList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return json;
    }

}
