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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
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.ExtFprzybDao;
import com.dxhy.extend.entity.ExtTdxTaxCurrent;
import com.dxhy.extend.model.ExtFprzybRespVO;
import com.dxhy.extend.service.tjbb.IExtFprzybService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 认证月报查询业务处理类
 *
 * @author jiaohongyang
 */

@Service("extendfprzybService")
@Slf4j
public class FbrzybServiceImpl extends MpBaseServiceImpl<ExtFprzybDao, ExtFprzybRespVO> implements IExtFprzybService {
    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String selectByGfshAndRznf(Map<String, Object> pramsMap) {
        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";
        String zjse = "0";
        String zdkse = "0";
        List<ExtFprzybRespVO> resultList = super.baseMapper.selectByGfshAndRznf(pramsMap);
        String hjje = "0.00";
        String hjse = "0.00";
        String dkse;
        if (resultList != null && resultList.size() > 0) {
            for (ExtFprzybRespVO extFprzybRespVO : resultList) {
                if (StringUtils.isBlank(extFprzybRespVO.getDkse())) {
                    dkse = MathUtil.round(extFprzybRespVO.getHjse());
                    extFprzybRespVO.setDkse(AmountFormatUtil.fmtMicrometer(dkse));
                } else {
                    dkse = MathUtil.round(extFprzybRespVO.getDkse());
                    extFprzybRespVO.setDkse(AmountFormatUtil.fmtMicrometer(dkse));
                }
                if (!StringHelper.isBlank(extFprzybRespVO.getHjje())) {
                    hjje = MathUtil.round(extFprzybRespVO.getHjje());
                    extFprzybRespVO.setHjje(AmountFormatUtil.fmtMicrometer(hjje));
                }
                if (!StringHelper.isBlank(extFprzybRespVO.getHjse())) {
                    hjse = MathUtil.round(extFprzybRespVO.getHjse());
                    extFprzybRespVO.setHjse(AmountFormatUtil.fmtMicrometer(hjse));
                }
                zjsl = String.valueOf(Integer.parseInt(extFprzybRespVO.getFpsl()) + Integer.parseInt(zjsl));
                zjje = MathUtil.add(zjje, hjje);
                zjse = MathUtil.add(zjse, hjse);
                zdkse = MathUtil.add(zdkse, dkse);
            }

        }
        // 设置记录总数
        json.put("gfsh", pramsMap.get("gfsh"));
        json.put("gfmc", pramsMap.get("gfmc"));
        json.put("zjsl", AmountFormatUtil.fmtMicrometer(zjsl));
        json.put("zjje", AmountFormatUtil.fmtMicrometer(zjje));
        json.put("zjse", AmountFormatUtil.fmtMicrometer(zjse));
        json.put("zdkse", AmountFormatUtil.fmtMicrometer(zdkse));

        String year = (String)pramsMap.get("rznf");
        List<ExtFprzybRespVO> list = toList(resultList, year);
        json.put("datalist", list);
        json = dataybChart(json, list, year);
        return super.responseResult(json);
    }

    @Override
    public String selectRzfphzByRznf(Map<String, Object> pramsMap) {

        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);

        List<String> gfshList = (List<String>)pramsMap.get("gfsh");
        List<ExtTdxTaxCurrent> taxList = null;
        if (gfshList != null && gfshList.size() > 0) {
            taxList = super.baseMapper.selectTaxList(gfshList);
        }
        String zjsl = "0";
        String zjje = "0";
        String zjse = "0";
        String zjhjse = "0";
        List<ExtFprzybRespVO> resultList = super.baseMapper.selectRzfphzByRznf(pramsMap);
        String hjje = "0.00";
        String hjse = "0.00";
        String dkse = "0";
        String djse;
        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i).getDkse() == null) {
                    djse = MathUtil.round(resultList.get(i).getHjse());
                    resultList.get(i).setDkse(AmountFormatUtil.fmtMicrometer(djse));
                } else {
                    djse = MathUtil.round(resultList.get(i).getDkse());
                    dkse=MathUtil.round(resultList.get(i).getDkse());
                    resultList.get(i).setDkse(AmountFormatUtil.fmtMicrometer(djse));
                }
                if (!StringHelper.isBlank(resultList.get(i).getHjje())) {
                    hjje = MathUtil.round(resultList.get(i).getHjje());
                    resultList.get(i).setHjje(AmountFormatUtil.fmtMicrometer(hjje));
                }
                if (!StringHelper.isBlank(resultList.get(i).getHjse())) {
                    hjse = MathUtil.round(resultList.get(i).getHjse());
                    resultList.get(i).setHjse(AmountFormatUtil.fmtMicrometer(hjse));
                }
                if(!StringHelper.isBlank(resultList.get(i).getFpsl())){
                    zjsl = String.valueOf(Integer.parseInt(resultList.get(i).getFpsl()) + Integer.parseInt(zjsl));
                }
                zjje = MathUtil.add(zjje, hjje);
                zjse = MathUtil.add(zjse, hjse);
                zjhjse = MathUtil.add(zjhjse, dkse);
                boolean flag = true;
                if (i == (resultList.size() - 1)) {
                    for (ExtTdxTaxCurrent tax : taxList) {
                        if (Integer.parseInt(tax.getCurrentTaxPeriod()) <= Integer
                            .parseInt(resultList.get(i).getSkssq())) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        resultList.get(i).setSfyq("已过税款所属期");
                    } else {
                        resultList.get(i).setSfyq("当前所属期");
                    }
                } else {
                    resultList.get(i).setSfyq("已过税款所属期");
                }
            }

        }
        json.put("total", zjsl);
        json.put("hjje", AmountFormatUtil.fmtMicrometer(zjje));
        json.put("hjse", AmountFormatUtil.fmtMicrometer(zjse));
        json.put("dkse", AmountFormatUtil.fmtMicrometer(zjhjse));
        String year = (String)pramsMap.get("rznf");
        List<ExtFprzybRespVO> list = toList(resultList, year);
        json.put("datalist", list);
        json = dataTjChart(json, list, year);

        return super.responseResult(json);
    }

    public List<ExtFprzybRespVO> toList(List<ExtFprzybRespVO> resultList, String year) {
        // 初始化完成

        // 改进版：提高可读性，但是增加内存使用
        int month = 12;
        ExtFprzybRespVO[] voArray = new ExtFprzybRespVO[month];
        for (int i = 0; i < month; i++) {
            ExtFprzybRespVO vo = new ExtFprzybRespVO();
            vo.setSkssq(year + String.format("%02d", i + 1));
            vo.setDkse("0.00");
            vo.setFpsl("0");
            vo.setHjje("0.00");
            vo.setHjse("0.00");

            voArray[i] = vo;
        }
        for (ExtFprzybRespVO currentVo : resultList) {
            if (Integer.parseInt(currentVo.getRzDate()) > month) {
                continue;
            }
            voArray[Integer.parseInt(currentVo.getRzDate()) - 1] = currentVo;
        }
        resultList = Arrays.asList(voArray);
        return resultList;

    }

    public JSONObject dataybChart(JSONObject json, List<ExtFprzybRespVO> resultList, String year) {
        try {

            JSONObject object = new JSONObject();
            JSONArray array = new JSONArray();
            String[] sumArray = {"合计金额", "合计税额", "发票数量", "合计抵扣税额"};
            String[] mouthArray = {year + "01", year + "02", year + "03", year + "04", year + "05", year + "06",
                year + "07", year + "08", year + "09", year + "10", year + "11", year + "12"};
            for (int i = 0; i < sumArray.length; i++) {
                JSONObject obj = new JSONObject();
                obj.put("name", sumArray[i]);
                obj.put("type", "bar");
                obj.put("areaStyle", "");
                JSONArray skssqarray = new JSONArray();
                for (ExtFprzybRespVO vo : resultList) {
                    if (i == 0) {
                        if(StringUtils.isNotEmpty(vo.getHjje())){
                            skssqarray.add(new DecimalFormat().parse(vo.getHjje()).doubleValue());
                        }else{
                            skssqarray.add("");
                        }
                    } else if (i == 1) {
                        if(StringUtils.isNotEmpty(vo.getHjse())){
                            skssqarray.add(new DecimalFormat().parse(vo.getHjse()).doubleValue());
                        }else{
                            skssqarray.add("");
                        }
                    } else if (i == 2) {
                        if(StringUtils.isNotEmpty(vo.getFpsl())){
                            skssqarray.add(new DecimalFormat().parse(vo.getFpsl()).doubleValue());
                        }else{
                            skssqarray.add("");
                        }
                    } else {
                        if(StringUtils.isNotEmpty(vo.getDkse())){
                            skssqarray.add(new DecimalFormat().parse(vo.getDkse()).doubleValue());
                        }else{
                            skssqarray.add("");
                        }
                    }
                }
                obj.put("data", skssqarray);
                array.add(obj);
            }
            object.put("series", array);
            JSONArray arrayAxis = new JSONArray();
            JSONObject objxAxis = new JSONObject();
            objxAxis.put("type", "category");
            objxAxis.put("boundaryGap", false);

            objxAxis.put("data", mouthArray);
            arrayAxis.add(objxAxis);
            object.put("xAxis", arrayAxis);
            JSONObject objlegend = new JSONObject();
            String[] legendArray = {"合计金额", "合计税额", "发票数量", "合计抵扣税额"};
            objlegend.put("legend", legendArray);
            object.put("legend", objlegend);
            json.put("dataChart", object);
            return json;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
        }
        return json;

    }

    public JSONObject dataTjChart(JSONObject json, List<ExtFprzybRespVO> resultList, String year) {
        try {

            JSONObject object = new JSONObject();
            JSONArray array = new JSONArray();
            String[] sumArray = {"合计金额", "合计税额", "发票数量", "合计抵扣税额"};
            String[] mouthArray = {year + "01", year + "02", year + "03", year + "04", year + "05", year + "06",
                year + "07", year + "08", year + "09", year + "10", year + "11", year + "12"};
            for (int i = 0; i < sumArray.length; i++) {
                JSONObject obj = new JSONObject();
                obj.put("name", sumArray[i]);
                obj.put("type", "line");
                obj.put("stack", "总量");
                obj.put("areaStyle", "");
                JSONArray skssqarray = new JSONArray();
                for (ExtFprzybRespVO vo : resultList) {
                    if (i == 0) {
                        skssqarray.add(new DecimalFormat().parse(vo.getHjje()).doubleValue());
                    } else if (i == 1) {
                        skssqarray.add(new DecimalFormat().parse(vo.getHjse()).doubleValue());
                    } else if (i == 2) {
                        skssqarray.add(new DecimalFormat().parse(vo.getFpsl()).doubleValue());
                    } else {
                        skssqarray.add(new DecimalFormat().parse(vo.getDkse()).doubleValue());
                    }
                }
                obj.put("data", skssqarray);
                array.add(obj);
            }
            object.put("series", array);
            JSONArray arrayAxis = new JSONArray();
            JSONObject objxAxis = new JSONObject();
            objxAxis.put("type", "category");
            objxAxis.put("boundaryGap", false);

            objxAxis.put("data", mouthArray);
            arrayAxis.add(objxAxis);
            object.put("xAxis", arrayAxis);
            JSONObject objlegend = new JSONObject();
            String[] legendArray = {"合计金额", "合计税额", "发票数量", "合计抵扣税额"};
            objlegend.put("legend", legendArray);
            object.put("legend", objlegend);
            json.put("dataChart", object);
            return json;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
        }
        return json;

    }

}
