package com.dxhy.manage.service.impl;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

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.manage.dao.OperationsToolsDao;
import com.dxhy.manage.dao.SellersStatDao;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.service.OperationsToolsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author futianhao
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service
@Slf4j
public class OperationsToolsServiceImpl implements OperationsToolsService {

    @Resource
    private OperationsToolsDao operationsToolsDao;

    @Resource
    private SellersStatDao sellersStatDao;

    @Override
    public String updateCreateDateByInvoices(JSONArray detailArray) {
        try {
            String taxno = "";
            String majorIntoWhereTable = "";
            for (int i = 0; i < detailArray.size(); i++) {
                JSONObject object = (JSONObject)detailArray.get(i);
                String newGfTaxno = object.getString("taxno");
                String invoiceCode = object.getString("invoiceCode");
                String invoiceNo = object.getString("invoiceNo");
                String uuid = invoiceCode + invoiceNo;
                if (!taxno.equals(newGfTaxno)) {
                    // 获取税号所属主表
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    SysDeptEntity sysDeptEntity = operationsToolsDao.getSysDeptInfo(newGfTaxno);
                    if (sysDeptEntity != null) {
                        majorIntoWhereTable = sysDeptEntity.getMajorIntoWhereTable();
                        taxno = newGfTaxno;
                        DynamicContextHolder.push(sysDeptEntity.getMajorIntoWhereDb() + DbConstant.BUSINESS_WRITE);
                        operationsToolsDao.updateCreateDateByUuid(majorIntoWhereTable, uuid);
                    } else {
                        return "该税号未配置";
                    }
                } else {
                    operationsToolsDao.updateCreateDateByUuid(majorIntoWhereTable, uuid);
                }
            }
            return "处理成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "处理失败";
        }

    }

    @Override
    public List<SysDeptEntity> getBlocList() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return operationsToolsDao.getBlocList();
    }

    @Override
    public List<SysDeptEntity> getTaxNoList(String blocId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return operationsToolsDao.getTaxNoList(blocId);
    }

    @Override
    public String updateCreateDateByBlocId(Map<String, Object> params) {
        try {

            String company = params.get("company") + "";
            String taxno = params.get("taxno") + "";
            // 需要查询获取一下
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            String majorIntoWhereDb = operationsToolsDao.getMajorIntoWhereDb(company);
            // 集团，分表，税号更新
            if ("99".equals(taxno)) {
                DynamicContextHolder.push(DbConstant.BASICS_READ);
                List<String> tables = operationsToolsDao.getAllTables(company);
                for (String table : tables) {
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    // 对于税号需要拆分
                    List<String> taxnos = operationsToolsDao.getTaxnoes(company, table);
                    // 根据税号拆分更新当前时间
                    // 设置每次多少条数据
                    int perCount = 100;
                    // 默认0
                    int index = 0;
                    // 遍历次数
                    int times = taxnos.size() / perCount;
                    DynamicContextHolder.push(majorIntoWhereDb + DbConstant.BUSINESS_WRITE);
                    for (index = 0; index <= times; index++) {
                        List<String> sbList = null;
                        if (taxnos.size() >= perCount) {
                            sbList = taxnos.subList(0, perCount);
                            updateCreateDateByTaxNo(table, sbList, params);
                            taxnos.removeAll(sbList);
                        } else {
                            if (taxnos.size() != 0) {
                                sbList = taxnos.subList(0, taxnos.size());
                                updateCreateDateByTaxNo(table, sbList, params);
                                taxnos.removeAll(sbList);
                            } else {
                                continue;
                            }
                        }
                    }
                }
            } else {
                List<String> list = new ArrayList();
                list.add(taxno);
                // // 单独查询一个税号的
                DynamicContextHolder.push(DbConstant.BASICS_READ);
                SysDeptEntity entity = sellersStatDao.getTaxNameByTaxno(taxno);
                DynamicContextHolder.push(majorIntoWhereDb + DbConstant.BUSINESS_WRITE);
                updateCreateDateByTaxNo(entity.getMajorIntoWhereTable(), list, params);
            }
            return "处理成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "服务异常";
        }
    }

    /**
     * 根据税号更新入库时间
     * 
     * @param table
     * @param sbList
     */
    private void updateCreateDateByTaxNo(String table, List<String> sbList, Map<String, Object> params) {
        operationsToolsDao.updateCreateDateByTaxNo(table, sbList, params);

    }
}
