package com.palmnest.dao.release.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.release.RenovationManageDao;
import com.palmnest.model.release.RenovationManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Slf4j
@Repository("renovationManageDao")
public class RenovationManageDaoImpl extends GenericDaoHibernate<RenovationManage, Long> implements RenovationManageDao {


	public RenovationManageDaoImpl() {
		super(RenovationManage.class);
	}

	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		log.info("RenovationManageDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select t.id,	org.id orgId,	vir.id virtId,	t.apply_user applyUser,	t.apply_tel applyTel,	t.renovation_company renovationCompany,").append("\n");
		sql.append("t.renovation_user renovationUser,	t.renovation_tel renovationTel,	t.address address,	t.status status,	t.audit_result auditResult,").append("\n");
		sql.append("t.audit_opinion auditOpinion,	t.code code,").append("\n");
		sql.append("org.name orgName,vir.`name` virtName,DATE_FORMAT(t.apply_time,'%Y-%m-%d %H:%i') applyTime,appuser.FULL_NAME auditUser,DATE_FORMAT(t.audit_time,'%Y-%m-%d %H:%i') auditTime,").append("\n");
		sql.append("DATE_FORMAT(t.start_time,'%Y-%m-%d %H:%i') startTime,DATE_FORMAT(t.end_time,'%Y-%m-%d %H:%i') endTime").append("\n");
		sql.append("from tb_renovation_manage t").append("\n");
		sql.append("left join tb_organization org on t.org_id=org.id").append("\n");
		sql.append("left join tb_virtualarchitecture vir on t.virt_id=vir.id").append("\n");
		sql.append("left join tb_app_user appuser on t.audit_user_id=appuser.id").append("\n");

		sql.append(" where 1=1 ");

		// 所属项目
		if (params.get("orgId") != null && StringUtils.isNotEmpty(params.get("orgId").toString())) {
			sql.append(" and t.org_id in (" + params.get("orgId") + ") ");
		}

		if (params.get("status") != null && !params.get("status").equals("")) {
			sql.append("  and t.status in (" + params.get("status") + ")").append("\n");
		}

		if (params.get("companyName") != null && !params.get("companyName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("companyName") + "%'");
		}

		// 企业名称
		if (params.get("virName") != null && !params.get("virName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("virName") + "%' ");
		}

		// 申请时间
		if (params.get("applyTime") != null && !params.get("applyTime").equals("")) {
			sql.append(" and date_format(t.apply_time,'%Y-%m-%d')='" + params.get("applyTime") + "'");
		}

		// 申请人名称
		if (params.get("applyName") != null && !params.get("applyName").equals("")) {
			sql.append(" and t.apply_user like '%" + params.get("applyName") + "%' ");
		}

		if (params.get("creatorBillId") != null && !params.get("creatorBillId").equals("")) {
			sql.append(" and t.creator_bill_id = '" + params.get("creatorBillId") + "'").append("\n");
		}

		if (params.get("startTime") != null && !params.get("startTime").equals("")) {
			sql.append(" and t.start_time >= str_to_date('" + params.get("startTime") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("endTime") != null && !params.get("endTime").equals("")) {
			sql.append(" and t.end_time <= str_to_date('" + params.get("endTime") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}


		sql.append(" order by t.status,t.apply_time desc  ").append("\n");

		log.info("RenovationManageDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}
}
