package com.service;

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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    public Page<Record> shubenshimuludayin_page(int orgid,Map param,int pageNumber, int pageSize){
        String select = "select * ";
        StringBuilder where = new StringBuilder(" from (select title,b.author,b.shucihao,b.publisher,b.bookno,b.price,b.edition,b.isbn,\n" +
                "\t\t\tb.typecode,\n" +
                "\t\t\tb.typeid,\n" +
                "\t\t\tp.juanci,p.totalpages,l.library,count(d.id) as booknum from t_book_detail d left join t_book b on b.id=d.bookid left join t_library l on l.id=d.libraryid " +
                "left join t_book_plus p on p.bookid=b.id where d.status != 3 AND b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and d.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and d.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        if(param.get("libraryid") !=null){
            where.append(" and d.libraryid=?");
            values.add(param.get("libraryid"));
        }
        if(param.get("bookno") != null && !"".equals(param.get("bookno"))){
            where.append(" AND b.bookno LIKE ? ");
            values.add("%"+param.get("bookno")+"%");
        }
        where.append(" group by title,b.author,b.publisher,b.bookno,b.price,b.edition,b.isbn,p.totalpages,l.library ) t");
        if(param.get("field") != null && !"".equals(param.get("field"))){
            where.append(" "+param.get("field"));
        }
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(values));
        return page;
    }

    public List<Record> shubenshimuludayin_list_print(int orgid,Map param){
        String select = "select title,b.author,b.publisher,b.bookno,b.price,b.edition,b.isbn,p.totalpages,l.library,count(d.id) as booknum ";
        StringBuilder where = new StringBuilder("from t_book_detail d left join t_book b on b.id=d.bookid left join t_library l on l.id=d.libraryid " +
                "left join t_book_plus p on p.bookid=b.id where d.status != 3 AND b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and d.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and d.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        if(param.get("libraryid") !=null){
            where.append(" and d.libraryid=?");
            values.add(param.get("libraryid"));
        }
        if(param.get("bookno") != null && !"".equals(param.get("bookno"))){
            where.append(" AND b.bookno LIKE ? ");
            values.add("%"+param.get("bookno")+"%");
        }
        where.append(" group by title,b.author,b.publisher,b.bookno,b.price,b.edition,b.isbn,p.totalpages,l.library");
        if(param.get("field") != null && !"".equals(param.get("field"))){
            where.append(" "+param.get("field"));
        }
        List<Record> page = Db.find(select+where.toString(),CommonUtils.listToArray(values));
        return page;
    }
    public List<Record> shubenshimuludayin_export(int orgid,Map param){
        String select = "select title,b.author,b.publisher,b.bookno,b.price,b.edition,b.isbn,p.totalpages,l.library,count(d.id) as booknum ";
        StringBuilder where = new StringBuilder("from t_book_detail d left join t_book b on b.id=d.bookid left join t_library l on l.id=d.libraryid " +
                "left join t_book_plus p on p.bookid=b.id where d.status != 3 AND b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and d.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and d.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        if(param.get("libraryid") !=null){
            where.append(" and d.libraryid=?");
            values.add(param.get("libraryid"));
        }
        if(param.get("bookno") != null && !"".equals(param.get("bookno"))){
            where.append(" AND b.bookno LIKE ? ");
            values.add("%"+param.get("bookno")+"%");
        }
        where.append(" group by title,b.author,b.publisher,b.bookno,b.price,b.edition,b.isbn,p.totalpages,l.library");
        if(param.get("field") != null && !"".equals(param.get("field"))){
            where.append(" "+param.get("field"));
        }
        String sql = select+where.toString();
        return Db.find(sql,CommonUtils.listToArray(values));
    }
    /**
     * 查询思路： 一个sql把所有数据查询出来，然后在内存中处理。
     * @param orgid
     * @param param
     * @return
     */
    public List tushuzongkuodengjibu_list(int orgid,Map param){
        StringBuilder sql = new StringBuilder("select bp.id,DATE_FORMAT(bp.createtime,'%Y-%m-%d') as createtime,bp.batchname,b.typecode,count(distinct b.typecode,b.shucihao) zhongshu, " +
                "count(d.id) ceshu,sum(b.price) as price from t_book_detail d inner " +
                "join t_bookpici bp on d.piciid=bp.id and bp.orgid=? " +
                "left join t_book b on d.bookid=b.id where d.bookstatus=1 and d.status!=3");
        List values= new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            sql.append(" and bp.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            sql.append(" and bp.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        sql.append(" group by bp.id,bp.batchname,bp.createtime,b.typecode ");
        List<Record> piciList = Db.find(sql.toString(), CommonUtils.listToArray(values));
        Map<Integer,Map> map = new HashMap();
        if(piciList != null && piciList.size()>0){
            for(Record record : piciList){
                int id = record.getInt("id");
                if(map.get(id) == null){
                    Map t = new HashMap();
                    t.put("id",id);
                    t.put("createtime",record.get("createtime"));
                    t.put("batchname",record.get("batchname"));
                    t.put("totalprice",0d);
                    t.put("type1_zs",0);
                    t.put("type1_cs",0);
                    t.put("type2_zs",0);
                    t.put("type2_cs",0);
                    t.put("type3_zs",0);
                    t.put("type3_cs",0);
                    t.put("type4_zs",0);
                    t.put("type4_cs",0);
                    t.put("type5_zs",0);
                    t.put("type5_cs",0);
                    map.put(id, t);
                }
                Map temp = map.get(id);
                String typecode = record.get("typecode");
                if(typecode == null ){
                    continue;
                }
                Double totalPrice = (Double)temp.get("totalprice")+(record.get("price")==null?0:record.getDouble("price"));
                temp.put("totalprice",new BigDecimal(totalPrice).setScale(2, RoundingMode.UP).doubleValue());
                if(typecode.startsWith("A")){
                    temp.put("type1_zs",(Integer)temp.get("type1_zs")+(record.get("price")==null?0:record.getInt("zhongshu")));
                    temp.put("type1_cs",(Integer)temp.get("type1_cs")+(record.get("price")==null?0:record.getInt("ceshu")));
                }else if(typecode.startsWith("B")){
                    temp.put("type2_zs",(Integer)temp.get("type2_zs")+(record.get("price")==null?0:record.getInt("zhongshu")));
                    temp.put("type2_cs",(Integer)temp.get("type2_cs")+(record.get("price")==null?0:record.getInt("ceshu")));
                }else if(typecode.startsWith("C") || typecode.startsWith("D") || typecode.startsWith("E")
                        || typecode.startsWith("F") || typecode.startsWith("G") || typecode.startsWith("H")
                        || typecode.startsWith("I") || typecode.startsWith("J") || typecode.startsWith("K")
                         ){
                    temp.put("type3_zs",(Integer)temp.get("type3_zs")+(record.get("price")==null?0:record.getInt("zhongshu")));
                    temp.put("type3_cs",(Integer)temp.get("type3_cs")+(record.get("price")==null?0:record.getInt("ceshu")));
                }else if(typecode.startsWith("N") || typecode.startsWith("O") || typecode.startsWith("P")
                        || typecode.startsWith("Q") || typecode.startsWith("R") || typecode.startsWith("S")
                        || typecode.startsWith("T") || typecode.startsWith("U") || typecode.startsWith("V")
                        || typecode.startsWith("X")){
                    temp.put("type4_zs",(Integer)temp.get("type4_zs")+(record.get("price")==null?0:record.getInt("zhongshu")));
                    temp.put("type4_cs",(Integer)temp.get("type4_cs")+(record.get("price")==null?0:record.getInt("ceshu")));
                }else if(typecode.startsWith("Z")){
                    temp.put("type5_zs",(Integer)temp.get("type5_zs")+(record.get("price")==null?0:record.getInt("zhongshu")));
                    temp.put("type5_cs",(Integer)temp.get("type5_cs")+(record.get("price")==null?0:record.getInt("ceshu")));
                }
            }
        }
        List result = new ArrayList();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        for(Record r : piciList){
            Map t = map.get(r.getInt("id"));
            if(t == null ){
                continue;
            }
            t.put("zongceshu", (Integer)t.get("type1_cs")+(Integer)t.get("type2_cs")+(Integer)t.get("type3_cs")
                    +(Integer)t.get("type4_cs")+(Integer)t.get("type5_cs"));
            t.put("zongzhongshu", (Integer)t.get("type1_zs")+(Integer)t.get("type2_zs")+(Integer)t.get("type3_zs")
                    +(Integer)t.get("type4_zs")+(Integer)t.get("type5_zs"));
            t.put("createtime",t.get("createtime"));
            result.add(t);
            map.remove(r.getInt("id"));
        }
        return result;
    }

    public Page<Record> tushugebiedengjibu_page(int pageNumber,int pageSize, int orgid,Map param){
        String select = "select d.createtime,b.title,b.author,b.publisher,b.publishdate,b.edition,d.barcode," +
                "c.name as newsource,p.totalpages,b.bookno,b.price,p.fuzhu ";
        StringBuilder where = new StringBuilder("from t_book_detail d inner join t_book b on b.id=d.bookid " +
                "left join t_book_plus p on p.bookid=d.bookid LEFT JOIN t_bookpici a ON d.piciid = a.id " +
                "LEFT JOIN t_dict c ON a.dictid = c.id where d.bookstatus = 1 and d.status != 3 and b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and d.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and d.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        if(param.get("guancangId") != null && !"".equals(param.get("guancangId"))){
            where.append(" and d.libraryid=?");
            values.add(param.get("guancangId"));
        }
        where.append(" ORDER BY d.barcode");
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(values));

        return page;
    }

    public List<Record> tushugebiedengjibu_list_print(int orgid,Map param){
        String select = "select d.createtime,b.title,d.barcode,b.author,b.publisher,b.publishdate,b.edition," +
                "c.name as newsource,p.totalpages,b.bookno,b.price,p.fuzhu ";
        StringBuilder where = new StringBuilder("from t_book_detail d inner join t_book b on b.id=d.bookid " +
                "left join t_book_plus p on p.bookid=d.bookid LEFT JOIN t_bookpici a ON d.piciid = a.id " +
                "LEFT JOIN t_dict c ON a.dictid = c.id where d.bookstatus = 1 and d.status != 3 and b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
//        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
//            where.append(" and d.createtime>=str_to_date('"+param.get("starttime")+" 00:00:00','%Y-%m-%d %H:%i:%S')");
//        }
//        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
//            where.append(" and d.createtime <= str_to_date('"+param.get("endtime")+" 23:59:59','%Y-%m-%d %H:%i:%S')");
//        }
        if(param.get("guancangId") != null && !"".equals(param.get("guancangId"))){
            where.append(" and d.libraryid=?");
            values.add(param.get("guancangId"));
        }
        where.append(" ORDER BY d.barcode");
        List<Record> list = Db.find(select+where.toString(),CommonUtils.listToArray(values));

        return list;
    }

    public List<Record> tushugebiedengjibu_export(int orgid,Map param){
        String select = "select DATE_FORMAT(d.createtime,'%Y-%m-%d') as createtime,b.title,b.author,b.publisher,b.publishdate,b.edition,d.barcode," +
                "c.name as newsource,p.totalpages,b.bookno,b.price,p.fuzhu ";
        StringBuilder where = new StringBuilder("from t_book_detail d inner join t_book b on b.id=d.bookid " +
                "left join t_book_plus p on p.bookid=d.bookid LEFT JOIN t_bookpici a ON d.piciid = a.id " +
                "LEFT JOIN t_dict c ON a.dictid = c.id where d.bookstatus = 1 and d.status<3 and b.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and d.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and d.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        if(param.get("guancangId") != null && !"".equals(param.get("guancangId"))){
            where.append(" and d.libraryid=?");
            values.add(param.get("guancangId"));
        }
        String sql = select+where.toString();
        System.out.println(sql);
        return Db.find(sql,CommonUtils.listToArray(values));
    }

    public Page<Record> tushuzhuxiaobaobiao_page(int orgid,Map param,int pageNumber, int pageSize){
        String select = "select d.barcode,b.title,b.isbn,b.author,b.publisher,b.publishdate,b.bookno,b.price,l.library,z.reason," +
                "DATE_FORMAT(z.createtime,'%Y-%m-%d') as createtime,u.realname ";
        StringBuilder where = new StringBuilder("from t_book_zhuxiao z inner join t_book_detail d on z.bookdetailid=d.id " +
                "inner join t_book b on b.id=z.bookid left join t_user u on u.id=z.operatorid " +
                "inner join t_library l on l.id=d.libraryid where z.status = 0 AND z.flag = 0 AND z.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and z.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and z.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(values));
        return page;
    }

    public List<Record> tushuzhuxiaobaobiao_list_print(int orgid,Map param){
        String select = "select d.barcode,b.title,b.author,b.publisher,b.bookno,b.price,l.library,z.reason," +
                "DATE_FORMAT(z.createtime,'%Y-%m-%d') as createtime,u.realname ";
        StringBuilder where = new StringBuilder("from t_book_zhuxiao z inner join t_book_detail d on z.bookdetailid=d.id " +
                "inner join t_book b on b.id=z.bookid left join t_user u on u.id=z.operatorid " +
                "inner join t_library l on l.id=d.libraryid where z.status = 0 AND z.flag = 0 AND z.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and z.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and z.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        List<Record> page = Db.find(select+where.toString(),CommonUtils.listToArray(values));
        return page;
    }
    public String tushuzhuxiaobaobiao_totalprice(int orgid,Map param){
        String select = "select SUM(b.price) as totalprice ";
        StringBuilder where = new StringBuilder("from t_book_zhuxiao z inner join t_book_detail d on z.bookdetailid=d.id " +
                "inner join t_book b on b.id=z.bookid left join t_user u on u.id=z.operatorid " +
                "inner join t_library l on l.id=d.libraryid where z.status = 0 AND z.flag = 0 AND z.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and z.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and z.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        Record r = Db.findFirst(select+where.toString(),CommonUtils.listToArray(values));
        String totalprice = r.getStr("totalprice");
        if(StringUtils.isBlank(totalprice)){
            totalprice = "0";
        }
        return  totalprice;
    }

    public List<Record> tushuzhuxiaobaobiao_export(int orgid,Map param){
        String select = "select d.barcode,b.isbn,b.title,b.author,b.publisher,b.publishdate,b.bookno,b.price,l.library,z.reason," +
                "DATE_FORMAT(z.createtime,'%Y-%m-%d') as createtime,u.realname ";
        StringBuilder where = new StringBuilder("from t_book_zhuxiao z inner join t_book_detail d on z.bookdetailid=d.id " +
                "inner join t_book b on b.id=z.bookid left join t_user u on u.id=z.operatorid " +
                "inner join t_library l on l.id=d.libraryid where z.status = 0 AND z.flag = 0 AND z.orgid=? ");
        List values = new ArrayList();
        values.add(orgid);
        if(param.get("starttime") != null && !"".equals(param.get("starttime"))){
            where.append(" and z.createtime>=str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("starttime")+" 00:00:00");
        }
        if(param.get("endtime") != null && !"".equals(param.get("endtime"))){
            where.append(" and z.createtime <= str_to_date(?,'%Y-%m-%d %H:%i:%S')");
            values.add(param.get("endtime")+" 23:59:59");
        }
        String sql = select+where.toString();
        return Db.find(sql,CommonUtils.listToArray(values));
    }

    public List<Record> book_export(Map<String,String> param){
        String select = "SELECT   a.id as bookid, a.bookno, a.orgid, typeid, typecode, typecommonid, shucihao, booknum, isbn, title, publisher, keyword, edition, author, authorworkmodeid," +
                " authorguobieid, publishdate,  publishaddress, price,   newsource,   c.id as bookdetailid,  libraryid, piciid, barcode, status, bookstatus, jieyuenums, yuyuenums, yujienums," +
                "   destroytime, destroyreason, putindate  ,   b.id as bookplusid,   b.bltitle, subtitle, otherauthor, totalpages, workmodeid, guobieid, sizeid, filesurl, languageid, bindingid," +
                " congshuming, congshuauthor, juanci," +
                "    ceci, b.imgurl, b.resourceurl, b.resourceopentimes, moneysource, arrivedate, fuzhu ,d.library ";
        StringBuilder where = new StringBuilder(" from t_book a inner JOIN t_book_plus b ON a.id = b.bookid " +
                " inner JOIN t_book_detail c on a.id=c.bookid" +
                " inner JOIN t_library d  ON c.libraryid = d.id " +
                " where a.booknum>0 and a.orgid =? ");
        List values = new ArrayList();
        values.add(param.get("orgid"));
        if (StringUtils.isNotBlank(param.get("isbn"))){
            where.append(" and isbn like ? ");
            values.add(param.get("isbn")+"%");
        }
        if (StringUtils.isNotBlank(param.get("typeid"))){
            where.append(" and typeid =? ");
            values.add(param.get("typeid"));
        }
        if (StringUtils.isNotBlank(param.get("title"))){
            where.append(" and title like ? ");
            values.add(param.get("title")+"%");
        }
        if (StringUtils.isNotBlank(param.get("author"))){
            where.append(" and author like ? ");
            values.add(param.get("author")+"%");
        }
        if (StringUtils.isNotBlank(param.get("publisher"))){
            where.append(" and publisher like ? ");
            values.add(param.get("publisher")+"%");
        }
        if (StringUtils.isNotBlank(param.get("bookno"))){
            where.append(" and a.bookno like ? ");
            values.add(param.get("bookno")+"%");
        }
        if (StringUtils.isNotBlank(param.get("bookstatus"))){
            where.append(" and bookstatus =? ");
            values.add(param.get("bookstatus"));
        }

        where.append(" group by a.id order by a.createtime DESC");
        String sql = select+where.toString();
        System.out.println("到处副本："+sql);
        return Db.find(sql,CommonUtils.listToArray(values));
    }

    public List<Record> periodical_export(int orgid,Map<String,String> param){
        String select = "select p.id,p.kanming,p.futiming,p.bianjibu,p.issn,p.tongyikanhao,p.youfadaihao,case when p.chubanpinlv =1 then '季刊'  when p.chubanpinlv =2 then '月刊'  when p.chubanpinlv =3 then '半月刊'  when p.chubanpinlv =4 then '旬刊'   else '周刊' end as chubanpinlv,p.bookno,d.hedingtime,d.niandu,d.qizhiqi,d.price,count(*) as num ";
        StringBuilder where = new StringBuilder(" from t_periodical_zhengding p left join t_periodical_detail d on p.id=d.periodicalid where p.orgid= ?  and status != 3  ");
        List values = new ArrayList();
        values.add(orgid);
        if(StringUtils.isNotBlank(param.get("issnorkanming"))){
            where.append(" and (p.issn=? or p.kanming like ?)");
            values.add(param.get("issnorkanming"));
            values.add("%"+param.get("issnorkanming")+"%");
        }
        if(StringUtils.isNotBlank(param.get("chubanpinlv"))){
            where.append(" and p.chubanpinlv =?");
            values.add(param.get("chubanpinlv"));
        }
        if(StringUtils.isNotBlank(param.get("issn"))){
            where.append(" and p.issn =?");
            values.add(param.get("issn"));
        }

        where.append(" group by p.id ");
        return Db.find(select+where.toString(),CommonUtils.listToArray(values));
    }

    public Page<Record> mulukadayin(int orgid,String title,String isbn,String bookno,String publisher,String typecode,int pageNumber, int pageSize){
        List paramList = new ArrayList();
        paramList.add(orgid);
        String select = "select b.shucihao,p.juanci,p.ceci,b.id,b.bookno,b.title,p.bltitle,p.subtitle,b.author,CONCAT(b.author,(select name from t_dict where id = b.authorworkmodeid)) authorworkmodeid,CONCAT(p.otherauthor,(select name from t_dict where id = p.workmodeid)) otherauthor,b.publishaddress,b.publisher,b.publishdate,p.totalpages,(select name from t_dict where id = p.sizeid) sizeid,p.congshuming,p.congshuauthor,p.fuzhu,b.isbn,b.price,b.keyword,b.typecode";
        StringBuffer sql = new StringBuffer(" from t_book b inner join t_book_plus p on b.id=p.bookid INNER JOIN t_book_detail d ON b.id = d.bookid where b.orgid=? ");
        if(null != title && !"".equals(title)) {
            sql.append(" and b.title like ?");
            paramList.add("%"+title+"%");
        }
        if(null != isbn && !"".equals(isbn)) {
            sql.append(" and b.isbn =?");
            paramList.add(isbn);
        }
        if(null != bookno && !"".equals(bookno)) {
            sql.append(" and b.bookno =?");
            paramList.add(bookno);
        }
        if(null != publisher && !"".equals(publisher)) {
            sql.append(" and b.publisher like ?");
            paramList.add("%"+publisher+"%");
        }
        if(null != typecode && !"".equals(typecode)) {
            sql.append(" and b.typecode =?");
            paramList.add(typecode);
        }
        sql.append(" group by b.id");
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,sql.toString(),CommonUtils.listToArray(paramList));
        return page;
    }

    public List<Record> mulukadayin(int orgid,String title,String isbn,String bookno,String publisher,String typecode){
        List paramList = new ArrayList();
        paramList.add(orgid);
        String select = "select b.shucihao,p.juanci,p.ceci,b.id,b.bookno,b.title,p.bltitle,p.subtitle,b.author,CONCAT(b.author,(select name from t_dict where id = b.authorworkmodeid)) authorworkmodeid,CONCAT(p.otherauthor,(select name from t_dict where id = p.workmodeid)) otherauthor,b.publishaddress,b.publisher,b.publishdate,p.totalpages,(select name from t_dict where id = p.sizeid) sizeid,p.congshuming,p.congshuauthor,p.fuzhu,b.isbn,b.price,b.keyword,b.typecode";
        StringBuffer sql = new StringBuffer(" from t_book b inner join t_book_plus p on b.id=p.bookid INNER JOIN t_book_detail d ON b.id = d.bookid where b.orgid=? ");
        if(null != title && !"".equals(title)) {
            sql.append(" and b.title like ?");
            paramList.add("%"+title+"%");
        }
        if(null != isbn && !"".equals(isbn)) {
            sql.append(" and b.isbn =?");
            paramList.add(isbn);
        }
        if(null != bookno && !"".equals(bookno)) {
            sql.append(" and b.bookno =?");
            paramList.add(bookno);
        }
        if(null != publisher && !"".equals(publisher)) {
            sql.append(" and b.publisher like ?");
            paramList.add("%"+publisher+"%");
        }
        if(null != typecode && !"".equals(typecode)) {
            sql.append(" and b.typecode =?");
            paramList.add(typecode);
        }
        sql.append(" GROUP BY b.id");
        return Db.find(select + sql.toString(),CommonUtils.listToArray(paramList));
    }

    public Page<Record> periodicalBindingList(int pageNumber,int pagesize,int orgid,String isbn,String chubanpinlv,String year,Integer libraryid){

        String select = "select p.id,d.barcode,p.kanming,p.issn,p.chubanpinlv,p.bookno,DATE_FORMAT(d.hedingtime,'%Y-%m-%d') as hedingtime,d.niandu,d.qizhiqi,d.price,d.hedingpiciid,c.library ";
        StringBuilder where = new StringBuilder(" from t_periodical_zhengding p inner join t_periodical_detail d on p.id=d.periodicalid " +
                "LEFT JOIN t_library c ON d.libraryid = c.id where p.orgid= ?  and status not in (3) ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(isbn)){
            where.append(" and (p.issn=? or p.kanming like ?)");
            list.add(isbn);
            list.add("%"+isbn+"%");
        }
        if(StringUtils.isNotBlank(chubanpinlv)){
            where.append(" and p.chubanpinlv =?");
            list.add(chubanpinlv);
        }
        if(StringUtils.isNotBlank(year)){
            where.append(" AND d.niandu = ?");
            list.add(year);
        }
        if(libraryid != null){
            where.append(" AND d.libraryid = ?");
            list.add(libraryid);
        }
        return  Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public List<Record> periodicalBindingList_print(int orgid,String isbn,String chubanpinlv,String year,Integer libraryid){

        String select = "select p.id,d.barcode,p.kanming,p.issn,p.chubanpinlv,p.bookno,DATE_FORMAT(d.hedingtime,'%Y-%m-%d') as hedingtime,d.niandu,d.qizhiqi,d.price,d.hedingpiciid,c.library ";
        StringBuilder where = new StringBuilder(" from t_periodical_zhengding p inner join t_periodical_detail d on p.id=d.periodicalid " +
                "LEFT JOIN t_library c ON d.libraryid = c.id where p.orgid= ?  and status not in (3) ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(isbn)){
            where.append(" and (p.issn=? or p.kanming like ?)");
            list.add(isbn);
            list.add("%"+isbn+"%");
        }
        if(StringUtils.isNotBlank(chubanpinlv)){
            where.append(" and p.chubanpinlv =?");
            list.add(chubanpinlv);
        }
        if(StringUtils.isNotBlank(year)){
            where.append(" AND d.niandu = ?");
            list.add(year);
        }
        if(libraryid != null){
            where.append(" AND d.libraryid = ?");
            list.add(libraryid);
        }
        return  Db.find(select+where.toString(),CommonUtils.listToArray(list));
    }
    public List<Record> periodicalBindingList_export(int orgid,String isbn,String chubanpinlv,String year,Integer libraryid){

        String select = "select p.id,d.barcode,p.kanming,p.issn," +
                "case p.chubanpinlv when 1 then '季刊' when 2 then '月刊' when 3 then '半月刊' when 4 then '旬刊' when 2 then '周刊' end as chubanpinlv," +
                "p.bookno,DATE_FORMAT(d.hedingtime,'%Y-%m-%d') as hedingtime,d.niandu,d.qizhiqi,d.price,d.hedingpiciid,c.library ";
        StringBuilder where = new StringBuilder(" from t_periodical_zhengding p inner join t_periodical_detail d on p.id=d.periodicalid " +
                "LEFT JOIN t_library c ON d.libraryid = c.id where p.orgid= ?  and status not in (3) ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(isbn)){
            where.append(" and (p.issn=? or p.kanming like ?)");
            list.add(isbn);
            list.add("%"+isbn+"%");
        }
        if(StringUtils.isNotBlank(chubanpinlv)){
            where.append(" and p.chubanpinlv =?");
            list.add(chubanpinlv);
        }
        if(StringUtils.isNotBlank(year)){
            where.append(" AND d.niandu = ?");
            list.add(year);
        }
        if(libraryid != null){
            where.append(" AND d.libraryid = ?");
            list.add(libraryid);
        }
        List<Record> result = Db.find(select+where.toString(),CommonUtils.listToArray(list));
        return  result;
    }

}
