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 CancellationStatisticsService {
    public static final CancellationStatisticsService me = new CancellationStatisticsService();

    /**
     * 图书注销统计
     * @param orgid
     * @return
     */
    public List<Record> bookCancellationStatistics(int orgid,String kssj,String jssj,String reason){
        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 c.bookid = b.id " +
                "LEFT JOIN t_book_zhuxiao d ON c.id = d.bookdetailid " +
                "WHERE a.parentid IN(1,2,3,4,5) AND b.orgid = ? AND d.`status` = 0 and d.flag=0 ");
        List values = new ArrayList();
        values.add(orgid);
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            where.append(" AND d.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND d.createtime >= ?");
            values.add(kssj);
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND d.createtime <= ?");
            values.add(jssj+" 23:59:59");
        }
        if(StringUtils.isNotBlank(reason)){
            if(!reason.equals("其它")){
                where.append(" AND d.reason = ?");
                values.add(reason);
            }else {
                where.append(" AND d.reason != '丢失' AND d.reason != '剔旧' AND d.reason != '损坏'");
            }
        }
        where.append(" GROUP BY a.id");
        List<Record> list = Db.find(select+where, CommonUtils.listToArray(values));
//        String select2 = "SELECT a.id,a.`name`,COUNT(DISTINCT b.zhongcihao,b.typecode) as zs,COUNT(c.id) as cs,SUM(c.price) as price ";
//        StringBuilder where2 = new StringBuilder("FROM t_booktype a " +
//                "LEFT JOIN t_periodical_zhengding b ON a.id = b.typeid LEFT JOIN t_periodical_detail c ON c.periodicalid = b.id " +
//                "LEFT JOIN t_book_zhuxiao d ON c.id = d.bookdetailid " +
//                "WHERE a.parentid IN(1,2,3,4,5) AND b.orgid = ?  ");
//        List values2 = new ArrayList();
//        values2.add(orgid);
//        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
//            where2.append(" AND d.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
//        }
//        if(StringUtils.isNotBlank(kssj)){
//            where2.append(" AND d.createtime > '"+kssj+"'");
//        }
//        if(StringUtils.isNotBlank(jssj)){
//            where2.append(" AND d.createtime < '"+jssj+" 23:59:59"+"'");
//        }
//        if(StringUtils.isNotBlank(reason)){
//            where2.append(" AND d.reason = ?");
//            values2.add(reason);
//        }
//        where2.append(" GROUP BY a.id");
//        List<Record> list2 = Db.find(select2+where2, CommonUtils.listToArray(values2));
//        list.addAll(list2);
        return list;
    }

    /**
     * 注销统计图表，select+where复用时报错，所以加了个select1+where1
     * @param orgid
     * @param kssj
     * @param jssj
     * @param reason
     * @return
     */
    public Map bookCancellationStatistics_z(int orgid,String kssj,String jssj,String reason){
        String select = "SELECT a.id,a.`name`,COUNT(c.id) as value ";
        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 c.bookid = b.id " +
                "LEFT JOIN t_book_zhuxiao d ON c.id = d.bookdetailid " +
                "WHERE a.parentid IN(1,2,3,4,5) AND b.orgid = ? AND d.`status` = 0 and d.flag=0 ");
        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 c.bookid = b.id " +
                "LEFT JOIN t_book_zhuxiao d ON c.id = d.bookdetailid " +
                "WHERE a.parentid IN(1,2,3,4,5) AND b.orgid = ? AND d.`status` = 0 and d.flag=0 ");
        List values = new ArrayList();
        values.add(orgid);
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            where.append(" AND d.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
            where1.append(" AND d.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND d.createtime >= ?");
            where1.append(" AND d.createtime >= ?");
            values.add(kssj);
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND d.createtime <= ?");
            where1.append(" AND d.createtime <= ?");
            values.add(jssj+" 23:59:59");
        }
        if(StringUtils.isNotBlank(reason)){
            if(!reason.equals("其它")){
                where.append(" AND d.reason = ?");
                where1.append(" AND d.reason = ?");
                values.add(reason);
            }else {
                where.append(" AND d.reason != '丢失' AND d.reason != '剔旧' AND d.reason != '损坏'");
                where1.append(" AND d.reason != '丢失' AND d.reason != '剔旧' AND d.reason != '损坏'");
            }
        }
        //where.append(" GROUP BY b.typeid) test on a.id = test.typeid WHERE a.parentid IN(1,2,3,4,5) ");
        List<Record> list = Db.find(select+where.append(" GROUP BY a.id ORDER BY value DESC LIMIT 10"), CommonUtils.listToArray(values));
        Map map = new HashMap();
        map.put("list",list);
        List<Record> list1 = Db.find(select1+where1.append(" GROUP BY a.parentid "), CommonUtils.listToArray(values));

        map.put("list1",list1);

        return map;
    }
}
