package com.dxhy.extend.service.tjbb.impl;

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.enums.FplxEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.extend.dao.ExtRzjgqdDao;
import com.dxhy.extend.model.ExtRzjgqdRespVO;
import com.dxhy.extend.model.ExtTjbbHjxxVO;
import com.dxhy.extend.service.tjbb.IExtRzjgqdService;
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.HashMap;
import java.util.List;
import java.util.Map;

//import com.lowagie.text.DocumentException;

/**
 *
 *
 * 认证结果清单业务实现类
 *
 * @author 张宏 @date 2019年5月13日
 *
 */

@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("extendrzjgqdService")
@Slf4j
public class ExtRzjgqdServiceImpl extends MpBaseServiceImpl<ExtRzjgqdDao, ExtRzjgqdRespVO>
    implements IExtRzjgqdService {
    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;
    /**
     * 发票类型码统一放入map中
     */
    public static final Map<String, String> FPLX_MAP = new HashMap<String, String>(16) {
        private static final long serialVersionUID = 3195016454748903072L;

        {
            for (FplxEnum en : FplxEnum.values()) {
                this.put(en.getFplxDm(), en.getFplxMc(en.getIndex()));
            }
        }
    };

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

        String zjsl = "0";
        String zjje = "0.00";
        String zjse = "0.00";
        String dkse = "0.00";

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<ExtRzjgqdRespVO> pageList = super.baseMapper.rzjgByGfshAndSkssq(pramsMap);
        PageInfo<ExtRzjgqdRespVO> pageInfo = new PageInfo<>(pageList);
        List<ExtRzjgqdRespVO> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        if (resultList != null && resultList.size() > 0) {
            int start = (int)pageInfo.getStartRow();
            for (int i = 0; i < resultList.size(); i++) {
                if (!StringHelper.isBlank(resultList.get(i).getJe())) {
                    resultList.get(i).setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getJe())));
                }
                if (!StringHelper.isBlank(resultList.get(i).getSe())) {
                    resultList.get(i).setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getSe())));
                }
                if (StringUtils.isNotBlank(resultList.get(i).getDkse())) {
                    resultList.get(i)
                        .setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getDkse())));
                } else {
                    if(StringUtils.isNotEmpty(resultList.get(i).getSe())){
                        resultList.get(i).setDkse(resultList.get(i).getSe());
                    }

                }
                //字典值转换
                String fplx = resultList.get(i).getFplx();
                resultList.get(i).setFplx(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,fplx));
                String dklx = resultList.get(i).getDklx();
                resultList.get(i).setDklx(dictdetaServcice.queryDictName(DictConstant.FPDK_TYPE,dklx));

                String glzt = resultList.get(i).getGlzt();
                resultList.get(i).setGlzt(dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));
                //特定要素
                if(StringUtils.isBlank(resultList.get(i).getLqTdyslxDm())){
                    resultList.get(i)
                            .setLqTdyslxDm("非特定业务");
                }else{
                    resultList.get(i)
                            .setLqTdyslxDm(dictdetaServcice.queryDictName(DictConstant.TDYS_LXDM, resultList.get(i).getLqTdyslxDm()));
                }
                String invoiceStatus = resultList.get(i).getInvoiceStatus();
                resultList.get(i).setInvoiceStatus(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, invoiceStatus));
                String lqHzsdbs = resultList.get(i).getLqHzsdbs();
                resultList.get(i).setLqHzsdbs(dictdetaServcice.queryDictName(DictConstant.HZSDZT, lqHzsdbs));
                String lqCpyycbs = resultList.get(i).getLqCpyycbs();
                resultList.get(i).setLqCpyycbs(dictdetaServcice.queryDictName(DictConstant.CPY_YCBS, lqCpyycbs));
                String lqJdcycbs = resultList.get(i).getLqJdcycbs();
                resultList.get(i).setLqJdcycbs(dictdetaServcice.queryDictName(DictConstant.JDC_YCBS, lqJdcycbs));

                resultList.get(i).setXh(String.valueOf(start + i));
            }
        }

        if (1 == curr) {
            List<ExtTjbbHjxxVO> hjxx = super.baseMapper.rzjghjxxByGfshAndSkssq(pramsMap);
            if (!StringHelper.isBlank(hjxx.get(0).getHjje())) {
                zjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjje()));
            }
            if (!StringHelper.isBlank(hjxx.get(0).getHjse())) {
                zjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjse()));
            }
            if(!StringHelper.isBlank(hjxx.get(0).getHjdkse())){
                dkse=AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjdkse()));
            }
            zjsl = hjxx.get(0).getHjsl();
            json.put("zjsl", zjsl);
            json.put("zjje", zjje);
            json.put("zjse", zjse);
            json.put("dkse", dkse);
        }
        String skssq = (String)pramsMap.get("skssq");
        String year = "";
        String month = "";
        if (skssq.length() == 6) {
            year = skssq.substring(0, 4);
            month = skssq.substring(4, 6);
        }
        json.put("gfmc", pramsMap.get("gfmc"));
        json.put("gfsh", pramsMap.get("gfsh"));
        json.put("skssq", year + "年" + month + "月");
        json.put("datalist", resultList);

        return super.responseResult(json);
    }

//    @Override
//    public void exportPdf(Map<String, Object> pramsMap, HttpServletResponse response)
//        throws IOException, TemplateException, DocumentException {
//        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
//        String fileName = "authResultList" + System.currentTimeMillis() + ".pdf";
//
//        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
//        response.setContentType("application/octet-stream;charset=UTF-8");
//        List<ExtRzjgqdRespVO> resultList = super.baseMapper.rzjgByGfshAndSkssq(pramsMap);
//        Map<String, Object> map = new HashMap<>(16);
//
//        String zjsl;
//        String zjje = "0";
//        String zjse = "0";
//        if (resultList != null && resultList.size() > 0) {
//
//            for (int i = 0; i < resultList.size(); i++) {
//                if (resultList.get(i).getDkse() == null) {
//                    resultList.get(i).setDkse("");
//                } else {
//                    resultList.get(i).setDkse(MathUtil.round(resultList.get(i).getDkse()));
//                }
//                if (!StringHelper.isBlank(resultList.get(i).getJe())) {
//                    resultList.get(i).setJe(MathUtil.round(resultList.get(i).getJe()));
//                }
//                if (!StringHelper.isBlank(resultList.get(i).getSe())) {
//                    resultList.get(i).setSe(MathUtil.round(resultList.get(i).getSe()));
//                }
//                resultList.get(i).setInvoiceType(resultList.get(i).getFplx());
//                resultList.get(i).setXh(String.valueOf(i + 1));
//            }
//
//        }
//        List<ExtTjbbHjxxVO> hjxx = super.baseMapper.rzjghjxxByGfshAndSkssq(pramsMap);
//
//        if (!StringHelper.isBlank(hjxx.get(0).getHjje())) {
//            zjje = MathUtil.round(hjxx.get(0).getHjje());
//        }
//        if (!StringHelper.isBlank(hjxx.get(0).getHjse())) {
//            zjse = MathUtil.round(hjxx.get(0).getHjse());
//        }
//        zjsl = hjxx.get(0).getHjsl();
//        map.put("resultList", resultList);
//        map.put("zjje", zjje);
//        map.put("zjse", zjse);
//        map.put("zjsl", zjsl);
//        map.put("currentDate", DateUtils.getStringDateShort());
//
//        String html = ExtPdfUtils.getPdfContent("authResultList.ftl", map);
//        OutputStream out;
//        ITextRenderer render;
//
//        out = response.getOutputStream();
//
//        render = ExtPdfUtils.getRender();
//        render.setDocumentFromString(html);
//        render.layout();
//        render.createPDF(out);
//        render.finishPDF();
//
//    }

}
