package com.uas.platform.b2b.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.uas.platform.b2b.manage.dao.CommonDao;
import com.uas.platform.b2b.manage.model.Statistics;
import com.uas.platform.b2b.manage.service.StatisticsService;
import com.uas.platform.b2b.manage.support.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据统计
 *
 * @author hejq
 * @date 2018-10-17 9:29
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private CommonDao commonDao;

    /**
     * 分组统计企业注册信息
     *
     * @param groupType 分组类型
     *                  year: 年
     *                  month: 月
     *                  day: 日
     * @param key       关键字
     *                  具体年份，月份，日期
     * @return Statistics
     */
    @Override
    public List<Statistics> getEnterpriseCount(String groupType, Integer... key) {
        if (Statistics.DateConstant.YEAR.equals(groupType)) {
            if (null == key) {
                key = new Integer[] {Integer.valueOf(DateUtils.getCurrentYear())};
            }
            String sqlKey = JSON.toJSON(key).toString().replace("[", "").replace("]", "");
            StringBuffer sql = new StringBuffer("select count(1) `count`, date_format(en_time, '%Y-%m') `xAxisName` from `sec$enterprises` where en_time is not null");
//            sql.append(" and date_format(en_time, '%Y') in (").append(sqlKey).append(")");
            sql.append(" group by date_format(en_time, '%Y-%m')");
            return commonDao.query(sql.toString(), Statistics.class);
        }
        return null;
    }
}
