package com.palmnest.dao.notice.impl;

import cn.jiguang.common.utils.StringUtils;
import cn.superdesk.libs.common.json.JsonUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.airconditioner.impl.ConditionerAddTimeDaoImpl;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.AppNoticeDao;
import com.palmnest.dao.notice.AppNoticeRecordDao;
import com.palmnest.model.notice.AppNoticeModel;
import com.palmnest.model.notice.AppNoticeRecordModel;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository("appNoticeRecordDao")
public class AppNoticeRecordDaoImpl extends GenericDaoHibernate<AppNoticeRecordModel, Integer> implements AppNoticeRecordDao {

    public AppNoticeRecordDaoImpl() {
        super(AppNoticeRecordModel.class);

    }
    private Logger LOG = Logger.getLogger(AppNoticeRecordDaoImpl.class);
    @Override
    public Page<AppNoticeRecordModel,String> getRecordList(Map<String, Object> map, int pageNo, int pageSize) throws Exception {
        LOG.info("AppNoticeRecordDaoImpl getRecordList======");
        StringBuffer sbf = new StringBuffer();
        sbf.append("select  id  , n_id  nId, department,   userId ,  name , telephone , type,orgId ,publicObj from tb_app_notice_record   where n_id=");
        sbf.append(map.get("id").toString());
        if(map.containsKey("orgId") && null != map.get("orgId") && StringUtils.isNotEmpty(map.get("orgId").toString())){
        	sbf.append(" and orgId=");
            sbf.append(map.get("orgId").toString());
        }
        LOG.info("AppNoticeRecordDaoImpl getRecordList sql" + sbf.toString());
        return pagedQueryByJdbcTemplate(sbf.toString(), pageNo, pageSize);
    }

    @Override
    public List<Map<String, Object>> recordFind(Map<String, Object> params,Integer type) {
        LOG.info("AppNoticeRecordDaoImpl recordFind======");
        StringBuilder sql = new StringBuilder("select").append("\n");
        sql.append("  publicObj,id  , n_id  nId, department,   userId ,  name , telephone , type ,(case when type = 1 then '已读'  else '未读' end) typeName,orgId,openId from tb_app_notice_record   where n_id=");
        sql.append(params.get("id").toString());
        sql.append(" and orgId=");
        sql.append(params.get("orgId").toString());
        if (params.containsKey("userId") && StringUtils.isNotEmpty(params.get("userId") == null ? "" : params.get("userId").toString())) {
            sql.append("  and userId=");
            sql.append(params.get("userId").toString());
        }
        if(type==0){
            sql.append(" and type=0");
        }
        LOG.info("AppNoticeRecordDaoImpl recordFind sql" + sql.toString());
        return getJdbcTemplate().queryForList(sql.toString());
    }

    @Override
    public void updateRecord(Map<String, Object> params) {
        LOG.info("AppNoticeRecordDaoImpl updateRecord======");

        StringBuilder sql = new StringBuilder();
        sql.append("update tb_app_notice_record set type=1").append("\n");
        sql.append(" where ").append("\n");
        sql.append(" n_id= "+params.get("id").toString()).append("\n");
        sql.append(" and userId= "+params.get("userId").toString()).append("\n");
        sql.append(" and orgId= "+params.get("orgId").toString()).append("\n");
        LOG.info("AppNoticeRecordDaoImpl updateRecord sql" + sql.toString());
        int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
    }

    @Override
    public void deleteRecord(Integer id) {
        LOG.info("AppNoticeRecordDaoImpl deleteRecord======");

        StringBuilder sql = new StringBuilder();
        sql.append("delete from tb_app_notice_record where n_id= "+id);

        LOG.info("AppNoticeRecordDaoImpl deleteRecord sql" + sql.toString());
        int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
    }

    @Override
    public void insertRecord( List<Map<String, Object>> maps,Integer nId,Integer publicObj){
//    	LOG.info("AppNoticeRecordDaoImpl  insertRecord maps:"+JsonUtils.toJson(maps));
    	if(CollectionUtils.isEmpty(maps)){
    		return;
    	}
        
        StringBuilder sql = new StringBuilder("insert into tb_app_notice_record(n_id,department,userId,name,telephone,orgId,publicObj) values");
        for (Map<String, Object> map : maps) {
            sql.append("(");
            sql.append(nId);
            if (map.containsKey("deptName") && StringUtils.isNotEmpty(map.get("deptName") == null ? "" : map.get("deptName").toString())) {
                sql.append(",'"+map.get("deptName").toString()+"'");
            }else {
                sql.append(",''");
            }
            sql.append(","+map.get("userId").toString());
            sql.append(",'"+map.get("userName").toString()+"'");
            sql.append(",'"+map.get("userMobile").toString()+"'");
            sql.append(","+map.get("organizationId").toString());

            sql.append(","+publicObj);
            sql.append("),");
        }
        sql.deleteCharAt(sql.length() - 1);
        LOG.info("AppNoticeRecordDaoImpl insertRecord sql" + sql.toString());
        int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
        LOG.info("AppNoticeRecordDaoImpl insertRecord count" + count);
    }

	@Override
	public Integer getRecordCount(Integer id) {
		String sql="select count(id) count from tb_app_notice_record where n_id="+id+" and type=1";
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("count") == null ? "0" : list.get(0).get("count").toString());
		}
		return count;
	}

	@Override
	public List<AppNoticeRecordModel> findByPublicObj(Integer id, Integer publicObj, Integer userId) {
		StringBuilder sql=new StringBuilder();
		sql.append(" select * from tb_app_notice_record where n_id=? and publicObj=? ");
		if(null != userId){
			sql.append("  and userId="+userId);
		}
		LOG.info("findByType-sql:"+sql.toString());
		return getSession().createSQLQuery(sql.toString()).addEntity(AppNoticeRecordModel.class).setInteger(0, id).setInteger(1, publicObj).list();
	}
}
