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

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.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.core.dao.CoreFprzybDao;
import com.dxhy.api.model.db.TDxTaxCurrentEntity;
import com.dxhy.core.model.FprzybRespVO;
import com.dxhy.core.service.export.IFprzybService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 发票认证月报业务实现类
 *
 * @author zhanghong
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service("corefprzybService")
@Slf4j

public class FprzybServiceImpl extends MpBaseServiceImpl<CoreFprzybDao, FprzybRespVO> implements IFprzybService {

    @Override
    public JSONObject selectByGfshAndRznf(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        String zjsl = "0";
        String zjje = "0.00";
        String zjse = "0.00";
        String dkse = "0.00";
        List<FprzybRespVO> resultList = super.baseMapper.selectByGfshAndRznf(pramsMap);

        // 用于页面展示

        List<String[]> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            String year = (String)pramsMap.get("rznf");
            List<FprzybRespVO> list = toList(resultList, year);
            for (int i = 0; i < list.size(); i++) {
                // 页面展示需要信息
                String[] dataArray = new String[6];
                dataArray[0] = String.valueOf(i + 1);
                dataArray[1] = list.get(i).getSkssq();
                dataArray[2] = list.get(i).getFpsl();
                if (!"".equals(list.get(i).getHjje())) {
                    dataArray[3] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjje());
                } else {
                    dataArray[3] = list.get(i).getHjje();
                }
                if (!"".equals(list.get(i).getHjse())) {
                    dataArray[4] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjse());
                } else {
                    dataArray[4] = list.get(i).getHjse();
                }
                if (StringUtils.isBlank(list.get(i).getDkse())) {
                    dataArray[5] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjse());
                } else {
                    dataArray[5] = AmountFormatUtil.fmtMicrometer(list.get(i).getDkse());
                }
                zjsl = String.valueOf(Integer.parseInt(list.get(i).getFpsl()) + Integer.parseInt(zjsl));
                zjje = MathUtil.add(zjje, list.get(i).getHjje());
                zjse = MathUtil.add(zjse, list.get(i).getHjse());
                if (!StringHelper.isBlank(list.get(i).getDkse())) {
                    dkse = MathUtil.add(dkse, list.get(i).getDkse());
                } else {
                    dkse = MathUtil.add(dkse, list.get(i).getHjse());
                }
                returnList.add(dataArray);
            }
        }

        String[] hjxx = new String[6];
        hjxx[1] = "合计";
        hjxx[2] = AmountFormatUtil.fmtMicrometer(zjsl);
        hjxx[3] = AmountFormatUtil.fmtMicrometer(zjje);
        hjxx[4] = AmountFormatUtil.fmtMicrometer(zjse);
        hjxx[5] = AmountFormatUtil.fmtMicrometer(dkse);

        returnList.add(hjxx);
        json.put("datalist", returnList);

        return json;
    }

    @Override
    public JSONObject selectRzfphzByRznf(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        String zjsl = "0";
        String zjje = "0";
        String zjse = "0";
        String dkse = "0";
        List<String> gfshList = (List<String>)pramsMap.get("gfshArrs");
        List<TDxTaxCurrentEntity> taxList = null;
        if (gfshList != null && gfshList.size() > 0) {
            taxList = super.baseMapper.selectTaxList(gfshList);
        }
        List<FprzybRespVO> resultList = super.baseMapper.selectRzfphzByRznf(pramsMap);

        // 用于页面展示

        List<String[]> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            for (int j = 0; j < resultList.size(); j++) {
                if (j == (resultList.size() - 1)) {
                    boolean flag = true;
                    if (taxList != null && taxList.size() > 0) {
                        for (TDxTaxCurrentEntity tax : taxList) {
                            if (Integer.parseInt(tax.getCurrentTaxPeriod()) <= Integer
                                .parseInt(resultList.get(j).getSkssq())) {
                                flag = false;
                            }
                        }
                    }
                    if (flag) {
                        resultList.get(j).setSfyq("已过税款所属期");
                    } else {
                        resultList.get(j).setSfyq("当前所属期");
                    }
                } else {
                    resultList.get(j).setSfyq("已过税款所属期");
                }
            }
            String year = (String)pramsMap.get("rznf");
            List<FprzybRespVO> list = toList(resultList, year);
            for (int i = 0; i < list.size(); i++) {
                // 页面展示需要信息
                String[] dataArray = new String[7];
                dataArray[0] = String.valueOf(i + 1);
                dataArray[1] = list.get(i).getSkssq();
                dataArray[2] = list.get(i).getFpsl();
                if (!"".equals(list.get(i).getHjje())) {
                    dataArray[3] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjje());
                } else {
                    dataArray[3] = list.get(i).getHjje();
                }
                if (!"".equals(list.get(i).getHjse())) {
                    dataArray[4] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjse());
                } else {
                    dataArray[4] = list.get(i).getHjse();
                }
                if (StringUtils.isBlank(list.get(i).getDkse())) {
                    dataArray[5] = AmountFormatUtil.fmtMicrometer(list.get(i).getHjse());
                } else {
                    dataArray[5] = AmountFormatUtil.fmtMicrometer(list.get(i).getDkse());
                }

                dataArray[6] = list.get(i).getSfyq();
                zjsl = String.valueOf(Integer.parseInt(list.get(i).getFpsl()) + Integer.parseInt(zjsl));
                zjje = MathUtil.add(zjje, list.get(i).getHjje());
                zjse = MathUtil.add(zjse, list.get(i).getHjse());
                if (!StringHelper.isBlank(list.get(i).getDkse())) {
                    dkse = MathUtil.add(dkse, list.get(i).getDkse());
                }
                returnList.add(dataArray);
            }
        }
        String[] hjxx = new String[7];
        hjxx[1] = "合计";
        hjxx[2] = AmountFormatUtil.fmtMicrometer(zjsl);
        hjxx[3] = AmountFormatUtil.fmtMicrometer(zjje);
        hjxx[4] = AmountFormatUtil.fmtMicrometer(zjse);
        hjxx[5] = AmountFormatUtil.fmtMicrometer(dkse);

        returnList.add(hjxx);
        json.put("datalist", returnList);

        return json;
    }

    public List<FprzybRespVO> toList(List<FprzybRespVO> resultList, String year) {
        // 改进版：提高可读性，但是增加内存使用
        int month = 12;
        FprzybRespVO[] voArray = new FprzybRespVO[month];
        for (int i = 0; i < month; i++) {
            FprzybRespVO vo = new FprzybRespVO();
            vo.setSkssq(year + String.format("%02d", i + 1));
            vo.setDkse("0");
            vo.setFpsl("0");
            vo.setHjje("0");
            vo.setHjse("0");
            voArray[i] = vo;
        }
        for (FprzybRespVO currentVo : resultList) {
            if (Integer.parseInt(currentVo.getRzDate()) > month) {
                continue;
            }
            voArray[Integer.parseInt(currentVo.getRzDate()) - 1] = currentVo;
        }
        resultList = Arrays.asList(voArray);
        return resultList;

    }
}
