package com.service.report;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

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

public class ClassificationStatisticsService {
    public static final ClassificationStatisticsService me = new ClassificationStatisticsService();

    /**
     * 图书分类统计 按22大类
     * @param orgid
     * @return
     */
    public List<Record> bookClassificationStatistics(int orgid){
        String select = "SELECT a.id,a.`name`,COUNT(DISTINCT b.shucihao,b.typecode) as zs,COUNT(c.id) as cs,SUM(b.price) as price ";
        StringBuilder where = new StringBuilder(" FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid " +
                "WHERE c.status !=3 AND c.bookstatus = 1 AND b.orgid = ?");
        List values = new ArrayList();
        values.add(orgid);
        where.append(" GROUP BY a.id");
        List<Record> list = Db.find(select+where, CommonUtils.listToArray(values));
        return list;
    }

    /**
     * 环形图
     * @param orgid
     * @return
     */
    public Map bookClassificationStatistics_z(int orgid){
        String select = "SELECT a.id,a.`name`,COUNT(c.id) as value  \n" +
                "FROM t_booktype a LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid WHERE c.status !=3 AND c.bookstatus = 1 AND b.orgid = "+orgid+"\n" +
                "GROUP BY a.id ORDER BY a.parentid ";
        String select1 = "SELECT a.id,a.`name`,COUNT(c.id) as value ";
        StringBuilder where1 = new StringBuilder(" FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid " +
                "WHERE c.status !=3 AND c.bookstatus = 1 AND b.orgid = ?");
        List values = new ArrayList();
        values.add(orgid);

        List<Record> list = Db.find(select);
        List<Record> list1 = Db.find(select1+where1.append(" GROUP BY a.parentid"), CommonUtils.listToArray(values));
        Map map = new HashMap();
        map.put("list",list);
        map.put("list1",list1);
        return map;
    }
    /**
     * 图书分类统计 总计
     * @param orgid
     * @return
     */
    public Record bookTotalClassificationStatistics(int orgid){
        String select = "SELECT a.id,COUNT(DISTINCT b.shucihao,b.typeid) as zs,COUNT(c.id) as cs ";
        StringBuilder where = new StringBuilder(" FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid " +
                "WHERE a.parentid IN (1,2,3,4,5) AND b.orgid = ?");
        List values = new ArrayList();
        values.add(orgid);
        Record list = Db.findFirst(select+where, CommonUtils.listToArray(values));
        return list;
    }
    /**
     * 图书分类统计 按五大类
     * 总册数前端保存，五大类比例 = 册数 / 总册数
     * @param orgid
     * @return
     */
    public List<Record> bookFiveClassificationStatistics(int orgid){
        String select = "SELECT a.id,COUNT(c.id) as cs ";
        StringBuilder where = new StringBuilder(" FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid " +
                "WHERE a.parentid IN (1,2,3,4,5) AND b.orgid = ?");
        List values = new ArrayList();
        values.add(orgid);
        where.append(" GROUP BY a.parentid");
        List<Record> list = Db.find(select+where, CommonUtils.listToArray(values));
        return list;
    }
}
