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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author lzp
 * @date 2019/10/11 14:28
 * 平台备件返厂dao
 */
@Repository
public class FittingRefundDao extends BaseDao {

	/**
	 * 平台获取所有服务商备件返厂数据
	 * @param page
	 * @param map
	 * @return
	 */
	public List<Record> getAllFittingData(LayuiPage page, Map<String, String> map) {
		StringBuffer sb = new StringBuffer();
		sb.append("select a.* ");
		sb.append(",DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime ");
		sb.append(" ,d.number as order_number ,d.power_station_name,d.customer_name,d.province,d.city,d.area,d.customer_address,d.customer_mobile,c.is_crm_customer ");
		sb.append(" from  crm_platform_fitting_refund a");
		sb.append(" left join crm_site c on a.site_id=c.id ");
		sb.append(" left join crm_order d on a.order_id=d.id");
		sb.append(" where a.status in ('1','2') ");
		sb.append(queryCondition(map));
		sb.append(" order by a.create_time desc ");
		sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
		return Db.find(sb.toString());
	}

	public long queryFittingCount(Map<String, String> map) {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from  crm_platform_fitting_refund a");
		sb.append(" left join crm_order d on a.order_id=d.id");
		sb.append(" where a.status in('1','2')");
		sb.append(queryCondition(map));
		return Db.queryLong(sb.toString());
	}

	/***
	 * 平台获取出入库明细数据
	 * @param map
	 * @return
	 */
	public List<Record> getPlatformInAndOutWarehousingData(LayuiPage page, Map<String, String> map){
		StringBuffer sb = new StringBuffer();
	    sb.append(" SELECT a.*,f.suit_printer,f.`version` ");
	    sb.append(" FROM `crm_platform_fitting_keep` a");
	    sb.append(" LEFT JOIN `crm_platform_fitting` f ON f.id=a.`fitting_id`");
	    sb.append(" WHERE 1=1");
		sb.append(queryCondition2(map));
	    sb.append(" ORDER BY a.create_time DESC");
		sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
		return Db.find(sb.toString());
	}
	public long getPlatformInAndOutWarehousingDataCount( Map<String, String> map){
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT COUNT(a.id) FROM `crm_platform_fitting_keep` a ");
		sb.append(" LEFT JOIN `crm_platform_fitting` f ON f.id=a.`fitting_id`");
		sb.append(" WHERE 1=1");
		sb.append(queryCondition2(map));
		return Db.queryLong(sb.toString());
	}
	public String queryCondition2(Map<String, String> map) {

		if (map == null || map.size() == 0) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		if(StringUtil.isNotBlank(map.get("type"))){  //明细类型
			sb.append(" and a.type='" + map.get("type") + "'");
		}
		if(StringUtils.isNotBlank(map.get("fittingCode"))){   //物料号
			sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("fittingName"))){ //备件名称
			sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("fittingVersion"))){  // 备件型号
			sb.append(" and f.version like '%" + map.get("fittingVersion").trim() + "%'");
		}

		if(StringUtils.isNotBlank(map.get("suitPrinter"))){  //适用机型
			sb.append(" and f.suit_printer like '%" + map.get("suitPrinter").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("applicant"))){  //申请人
			sb.append(" and a.applicant='" + map.get("applicant") + "'");
		}
		if(StringUtils.isNotBlank(map.get("remarks"))){  // 备注
			sb.append(" and a.remarks like '%" + map.get("remarks").trim() + "%'");
		}

		if(StringUtils.isNotBlank(map.get("operateTimeStart"))){  //出入库时间
			sb.append(" and a.operate_time >= '" + map.get("operateTimeStart") + " 00:00:00'");
		}
		if(StringUtils.isNotBlank(map.get("operateTimeEnd"))){
			sb.append(" and a.operate_time <= '" + map.get("operateTimeEnd") + " 23:59:59'");
		}

		return sb.toString();
	}
	public String queryCondition(Map<String, String> map) {

		if (map == null || map.size() == 0) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		if(StringUtils.isNotBlank(map.get("siteId"))){  // 服务商名称
			sb.append(" and a.site_id='" + map.get("siteId") + "'");
		}
		if(StringUtils.isNotBlank(map.get("fittingCode"))){   //物料号
			sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("fittingName"))){
			sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("fittingVersion"))){
			sb.append(" and a.fitting_version like '%" + map.get("fittingVersion").trim() + "%'");
		}

		if(StringUtils.isNotBlank(map.get("powerStationName"))){
			sb.append(" and d.power_station_name like '%" + map.get("powerStationName").trim() + "%'");
		}
		/*if(StringUtils.isNotBlank(map.get("fittingType"))){
			sb.append(" and a.fitting_type='" + map.get("fittingType") + "'");
		}*/
		if(StringUtils.isNotBlank(map.get("warrantyType"))){
			sb.append(" and d.warranty_type='" + map.get("warrantyType") + "'");
		}
		if(StringUtils.isNotBlank(map.get("status"))){
			sb.append(" and a.status='" + map.get("status") + "'");
		}

		if(StringUtils.isNotBlank(map.get("refundTimeStart"))){
			sb.append(" and a.create_time >= '" + map.get("refundTimeStart") + " 00:00:00'");
		}
		if(StringUtils.isNotBlank(map.get("refundTimeEnd"))){
			sb.append(" and a.create_time <= '" + map.get("refundTimeEnd") + " 23:59:59'");
		}
		if(StringUtils.isNotBlank(map.get("endTimeSta"))){
			sb.append(" and a.end_time >= '" + map.get("endTimeSta") + " 00:00:00'");
		}
		if(StringUtils.isNotBlank(map.get("endTimeEnd"))){
			sb.append(" and a.end_time <= '" + map.get("endTimeEnd") + " 23:59:59'");
		}
		//登记时间
		if(StringUtils.isNotBlank(map.get("createTimeSta"))){
			sb.append(" and a.create_time >= '" + map.get("createTimeSta") + " 00:00:00'");
		}
		if(StringUtils.isNotBlank(map.get("createTimeEnd"))){
			sb.append(" and a.create_time <= '" + map.get("createTimeEnd") + " 23:59:59'");
		}
		return sb.toString();
	}

}
