package com.service.intelligentdevice;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Equipment;
import com.utils.CommonUtils;

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

public class EquipmentService {
    public static final EquipmentService me = new EquipmentService();
    private Equipment dao = new Equipment().dao();

    public Page<Record> findEquipmentList(int pageNumber,int pagesize, Integer orgid) {
        String select = "select * ";
        StringBuilder where = new StringBuilder("from t_equipment where orgid = ? ORDER BY ordernum ");
        return Db.paginate(pageNumber, pagesize,select,where.toString(),orgid);
    }

    public Page<Record> findCirculationRecord(int pageNumber,int pagesize, int libraryid,int orgid,String title,String cardnumber,String starttime,String endtime) {
        String select = "select b.title,b.isbn,r.cardnumber,r.realname,od.deptname,DATE_FORMAT(j.createtime,'%Y-%m-%d') createtime,j.status ";
        StringBuilder where = new StringBuilder("from t_jieyue j left join t_book_detail d on j.bookdetailid = d.id " +
                "left join t_book b on d.bookid = b.id left join t_reader r on j.readerid = r.id " +
                "left join t_org_dept od on r.orgdeptid = od.id " +
                "where jieyuetype = 1 and j.libraryid = ? and j.orgid = ? ");
        List list = new ArrayList();
        list.add(libraryid);
        list.add(orgid);
        if(CommonUtils.isNotNullOrNotEmpty(title)) {
            where.append(" and b.title like ? ");
            list.add("%"+title+"%");
        }
        if(CommonUtils.isNotNullOrNotEmpty(cardnumber)) {
            where.append(" and r.cardnumber =? ");
            list.add(cardnumber);
        }
        if(CommonUtils.isNotNullOrNotEmpty(starttime)) {
            where.append(" and j.createtime >= ?");
            list.add(starttime);
        }
        if(CommonUtils.isNotNullOrNotEmpty(endtime)) {
            where.append(" and j.createtime <=?");
            list.add(endtime+" 23:59:59");
        }
        where.append(" order by j.createtime desc ");
        return Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
    }

}
