package com.dxhy.manage.service.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.utils.PageUtils;
import com.dxhy.manage.dao.CheckStatDao;
import com.dxhy.manage.dao.SellersStatDao;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.model.BlocNumModel;
import com.dxhy.manage.model.CheckNumModel;
import com.dxhy.manage.model.CollectNumModel;
import com.dxhy.manage.service.CheckStatService;
import com.dxhy.manage.service.SellersStatService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("checkStatService")
public class CheckStatServiceImpl extends MpBaseServiceImpl<CheckStatDao, SysDeptEntity> implements CheckStatService {

    @Resource
    private EasyExcelUtil excelUtil;

    @Override
    public List<SysDeptEntity> queryTaxnoList() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.queryCheckTaxno();

    }

    @Override
    public JSONObject getYearTitle() {
        JSONObject json = new JSONObject();
        String[] titlelist = {"查验年份", "查验次数", "成功次数", "失败次数", "结算次数", "异常失败次数"};
        String[] titleprop = {"year", "checkCount", "successCount", "failCount", "account", "exceptionCount"};
        // 需要设置返回列表标题时调用
        json = super.setTitleList(titlelist, titleprop);
        return json;
    }

    @Override
    public PageUtils queryYearPage(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();
        int count = 0;
        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);
            count = baseMapper.getCheckCountYearCount(taxno, beginDate, endDate);
            totalList = baseMapper.getCheckCount(taxno, beginDate, endDate,
                (Integer.parseInt(params.get("curr")) - 1) * Integer.parseInt(params.get("size")),
                Integer.parseInt(params.get("size")));

        }

        return new PageUtils(totalList, count, Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));

    }

    @Override
    public JSONObject getMonthTitle() {
        JSONObject json = new JSONObject();
        String[] titlelist = {"查验月份", "查验次数", "成功次数", "失败次数", "结算次数", "异常失败次数"};
        String[] titleprop = {"year", "checkCount", "successCount", "failCount", "account", "exceptionCount"};
        // 需要设置返回列表标题时调用
        json = super.setTitleList(titlelist, titleprop);
        return json;
    }

    @Override
    public PageUtils queryMonthPage(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();
        int count = 0;
        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);
            count = baseMapper.getCheckCountMonthCount(taxno, beginDate, endDate);

            totalList = baseMapper.getCheckCountMonth(taxno, beginDate, endDate,
                (Integer.parseInt(params.get("curr")) - 1) * Integer.parseInt(params.get("size")),
                Integer.parseInt(params.get("size")));

        }

        return new PageUtils(totalList, count, Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));
    }

    @Override
    public void exportExcel(Map<String, String> params, HttpServletResponse response) {
        String timeType = params.get("timeType");
        PageUtils page = null;
        if ("0".equals(timeType)) {
            page = queryDayPageExport(params);
        } else if ("1".equals(timeType)) {
            page = queryYearPageExport(params);
        } else if ("2".equals(timeType)) {
            page = queryMonthPageExport(params);
        }
        try {
            // 添加序号
            List<CheckNumModel> list = new ArrayList<>();
            int i = 1;
            for (Object obj : page.getList()) {
                CheckNumModel model = (CheckNumModel)obj;
                model.setOrder(i++ + "");
                list.add(model);
            }
            excelUtil.writeExcel(response, list, params.get("taxno") + "CheckStat" + System.currentTimeMillis(),
                "sheet1", CheckNumModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PageUtils queryYearPageExport(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();

        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);

            totalList = baseMapper.getCheckCountByExport(taxno, beginDate, endDate);

        }

        return new PageUtils(totalList, totalList.size(), Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));

    }

    public PageUtils queryMonthPageExport(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();
        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);
            totalList = baseMapper.getCheckCountMonthByExport(taxno, beginDate, endDate);

        }

        return new PageUtils(totalList, totalList.size(), Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));
    }

    @Override
    public PageUtils queryDayPage(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();
        int count = 0;
        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);
            count = baseMapper.getDayCheckCountCount(taxno, beginDate, endDate);
            totalList = baseMapper.getDayCheckCount(taxno, beginDate, endDate,
                (Integer.parseInt(params.get("curr")) - 1) * Integer.parseInt(params.get("size")),
                Integer.parseInt(params.get("size")));

        }

        return new PageUtils(totalList, count, Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));

    }

    public PageUtils queryDayPageExport(Map<String, String> params) {
        String taxno = params.get("taxno");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate") + " 23:59:59";
        List<CheckNumModel> totalList = new ArrayList<>();

        // 通过deptid获取分库信息
        if (taxno.length() > 0) {
            // 单独查询一个税号的
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity entity = baseMapper.getTaxNameByTaxno(taxno);
            DynamicContextHolder.push(entity.getMajorIntoWhereDb() + DbConstant.BUSINESS_READ);

            totalList = baseMapper.getDayCheckCountByExport(taxno, beginDate, endDate);

        }

        return new PageUtils(totalList, totalList.size(), Integer.parseInt(params.get("size")),
            Integer.parseInt(params.get("curr")));

    }

    @Override
    public JSONObject getDayTitle() {
        JSONObject json = new JSONObject();
        String[] titlelist = {"查验日期", "查验次数", "成功次数", "失败次数", "结算次数", "异常失败次数"};
        String[] titleprop = {"year", "checkCount", "successCount", "failCount", "account", "exceptionCount"};
        // 需要设置返回列表标题时调用
        json = super.setTitleList(titlelist, titleprop);
        return json;
    }
}
