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

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

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
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.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.vo.DictdetaModel;
import com.dxhy.core.dao.QyswxxDao;
import com.dxhy.core.model.QyswxxRespVO;
import com.dxhy.core.service.export.IQyswxxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 税务信息业务实现类
 *
 * @author user
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("qyswxxService")
@Slf4j

public class QyswxxServiceImpl extends MpBaseServiceImpl<QyswxxDao, QyswxxRespVO> implements IQyswxxService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectByGfshAndXydj(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        PageHelper.startPage(curr, size);
        List<QyswxxRespVO> pageList = super.baseMapper.selectByGfshAndXydj(pramsMap);
        PageInfo<QyswxxRespVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        // 用于页面展示
        List<QyswxxRespVO> resultList = pageInfo.getList();
        List<String[]> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            int i = 0;
            for (QyswxxRespVO hzxx : resultList) {
                // 页面展示需要信息
                i++;
                hzxx.setXh(String.valueOf((curr - 1) * size + i));
                //字典转换
                hzxx.setCompanyType(dictdetaServcice.queryDictName(DictConstant.COMPANY_TYPE,hzxx.getCompanyType()));
                hzxx.setOilType(dictdetaServcice.queryDictName(DictConstant.OIL_TYPE,hzxx.getOilType()));
                hzxx.setTaxpayerType(dictdetaServcice.queryDictName(DictConstant.TAXPAYER_TYPE,hzxx.getTaxpayerType()));

                List<DictdetaModel> sbzts = dictdetaServcice.selectDictdeta("sbzt");
                String periodIndexInfo = hzxx.getPeriodIndexInfo();
                String monthIndexInfo = hzxx.getMonthIndexInfo();
                if (StringUtils.isNotBlank(periodIndexInfo)) {
                    String period = periodIndexInfo.substring(periodIndexInfo.lastIndexOf("_") + 1);
                    for (DictdetaModel model : sbzts) {
                        if (model.getDictcode().equals(period)) {
                            hzxx.setPeriodIndexInfo(periodIndexInfo.substring(0, periodIndexInfo.lastIndexOf("_") + 1)
                                + model.getDictname());
                        }
                    }
                }
                if (StringUtils.isNotBlank(monthIndexInfo)) {
                    String month = monthIndexInfo.substring(monthIndexInfo.lastIndexOf("_") + 1);
                    for (DictdetaModel model : sbzts) {
                        if (model.getDictcode().equals(month)) {
                            hzxx.setMonthIndexInfo(
                                monthIndexInfo.substring(0, monthIndexInfo.lastIndexOf("_") + 1) + model.getDictname());
                        }
                    }
                }
                hzxx.setSbzq(getSbzq(hzxx.getSbzq()));
                if ("0".equals(hzxx.getSdzt())) {
                    hzxx.setSdzt("否");
                }
                if ("1".equals(hzxx.getSdzt())) {
                    hzxx.setSdzt("是");
                }
            }
        }
        json.put("datalist", resultList);

        return json;
    }

    public String getSbzq(String sbzq) {
        String sbztmsc = "";
        switch (sbzq) {
            case "1":
                sbztmsc = "月报";
                break;
            case "3":
                sbztmsc = "季报";
                break;
            default:
                break;
        }
        return sbztmsc;
    }
}
