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.model.Org;
import com.model.OrgDept;
import com.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 部门
 * 2018-10-17
 * zj
 */
public class OrgDeptService {

    public static final OrgDeptService me = new OrgDeptService();
    private OrgDept dao = new OrgDept().dao();

    public List<OrgDept> list(OrgDept bean) {


        StringBuilder sql = new StringBuilder(" SELECT t_org_dept.*,t_reader_auth.name as authname FROM t_org_dept left join t_reader_auth on  t_org_dept.readerauthid=t_reader_auth.id   where 1=1 " );
        List list = new ArrayList();
        String deptname = bean.getDeptname();
        if(StringUtils.isNotBlank(deptname)){
            sql.append("and deptname like ?");
            list.add("%"+deptname+"%");
        }
        Integer orgid=bean.getOrgid();
        if(orgid !=null){
            sql.append("and t_org_dept.orgid = ?");
            list.add(orgid);
        }
        sql.append(" order by deptname");
        return dao.find(sql.toString(),list.toArray(new Object[0]));

    }

    public void deleteById(int id) {
        dao.deleteById(id);
    }
    public OrgDept findById(int id){
        return dao.findById(id);
    }
    public void addOrUpdate(OrgDept bean, int operatorid){

        if(bean.getId()== null){
            if(bean.getParentid() == null){
                bean.setParentid(0);
            }
            bean.setCreatetime(new Date()).setOperatorid(operatorid).save();
        }else{
            OrgDept old = dao.findById(bean.getId());
            if(bean.getParentid() == null){
                bean.setParentid(0);
            }
            bean.setCreatetime(old.getCreatetime()).setOperatorid(old.getOperatorid()).setUpdatetime(new Date()).setUpdateuserid(operatorid).update();
            Db.update("update t_reader set readerauthid = ? where orgdeptid = ? and orgid = ?",bean.getReaderauthid(),bean.getId(),bean.getOrgid());
        }
    }
    //查询是否存在下级
    public boolean isJunior(int id){
        OrgDept i = dao.findFirst(" select count(id)as ac from t_org_dept WHERE parentid =? ",id);
        int ac = i.getInt("ac");
        Record record = Db.findFirst("select count(1) count from t_reader where orgdeptid = ?",id);
        int ct = record.getInt("count");

        if(ac > 0 || ct > 0) {
            return true;
        } else {
            return false;
        }

    }
    public List<OrgDept> findOrgDept(int orgid) {
        String sql ="SELECT id,deptname,readerauthid FROM t_org_dept where orgid=?";
        return dao.find(sql,orgid);
    }

    public OrgDept findOrgDeptByName(String name,int orgid) {
        String sql ="SELECT id,deptname,readerauthid FROM t_org_dept where deptname =? and orgid=?";
        return dao.findFirst(sql,name,orgid);
    }
    public List<OrgDept> deptList(int orgid) {
        List<OrgDept> orgDepts = dao.find("SELECT id,ifnull(deptname,'') deptname FROM t_org_dept where orgid=? order by deptname ",orgid);
        return orgDepts;
    }

}
