package com.sifang.modules.platform.fittingManager.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.entity.PlatformFitting;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

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

/**
 * @author lzp
 * @date 2019/10/29 18:37
 */
@Repository
public class PlatformFittingDao extends BaseDao<PlatformFitting> {

	private Logger logger = Logger.getLogger(PlatformFittingDao.class);


	public List<Record> getFittingList(LayuiPage page, Map<String,String> map){
	StringBuilder sf = new StringBuilder();
		sf.append(" SELECT * FROM crm_platform_fitting a WHERE a.status='1' ");
		sf.append(queryCondition(map));
		sf.append(" order by a.create_time desc ");
		sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());

		return Db.find(sf.toString());
	}

	public long getFittingCount(Map<String,String> map){
		StringBuilder sf = new StringBuilder();
		sf.append(" SELECT count(id) FROM crm_platform_fitting a WHERE a.status='1' ");
		sf.append(queryCondition(map));
		return Db.queryLong(sf.toString());
	}

	public boolean getcheckFittingCode(String code){
		String sql = "select count(id) from crm_platform_fitting where code =? and status ='1' ";
		return Db.queryLong(sql,code)>0 ;
	}

	public String queryCondition(Map<String, String> map) {
		if (map == null || map.size() == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isNotBlank((CharSequence) map.get("fittingCode"))){
			sb.append(" and a.code like '%" + map.get("fittingCode").trim() + "%' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("fittingName"))){
			sb.append(" and a.name like '%" + map.get("fittingName").trim() + "%' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("fittingVersion"))){
			sb.append(" and a.version like '%" + map.get("fittingVersion").trim() + "%' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("suitPrinter"))){
			sb.append(" and a.suit_printer like '%" + map.get("suitPrinter") + "%' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("remarks"))){
			sb.append(" and a.remarks like '%" + map.get("remarks") + "%' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("warningMin"))){
			sb.append(" and a.warning >= '" + map.get("warningMin") + "' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("warningMax"))){
			sb.append(" and a.warning <= '" + map.get("warningMax") + "' ");
		}

		return sb.toString();
	}


	public Record getFittingByCode(String code){
		String sql = "select * from crm_platform_fitting where code =? and status ='1' ";
		return Db.findFirst(sql, code);
	}

}
