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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlatformFittingRefund;
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 FacFittingRefundDao extends BaseDao<CrmPlatformFittingRefund> {

	/**
	 * 获取所有服务商备件返厂数据
	 * @param page
	 * @param map
	 * @return
	 */
	public List<Record> getAllFittingData(LayuiPage page, Map<String, String> map) {
		StringBuffer sb = new StringBuffer();
		sb.append("select a.*,c.delivery_name as shipName,c.delivery_mobile as shipMobile,c.delivery_address as shipAddress ");
		sb.append(",DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime ");
		sb.append(" ,d.appliance_brand,d.appliance_category,d.appliance_model");
		sb.append(" from crm_platform_fitting_refund a");
		sb.append(" left join crm_platform_fitting_apply c on a.apply_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.status asc,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(" where a.status in('1','2')  ");
		sb.append(queryCondition(map));
		return Db.queryLong(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("orderNumber"))){
			sb.append(" and a.order_number like '%" + map.get("orderNumber").trim() + "%'");
		}
		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("applianceBrand"))){
			sb.append(" and d.appliance_brand='" + map.get("applianceBrand") + "'");
		}
		if(StringUtils.isNotBlank(map.get("applianceCategory"))){
			sb.append(" and d.appliance_category='" + map.get("applianceCategory") + "'");
		}
		if(StringUtils.isNotBlank(map.get("status"))){
			sb.append(" and a.status='" + map.get("status") + "'");
		}
		if(StringUtils.isNotBlank(map.get("fittingType"))){
			sb.append(" and a.fitting_type='" + map.get("fittingType") + "'");
		}
		if(StringUtils.isNotBlank(map.get("postageType"))){
			sb.append(" and a.postage_type='" + map.get("postageType") + "'");
		}
		if(StringUtils.isNotBlank(map.get("applianceModel"))){
			sb.append(" and d.appliance_model like '%" + map.get("applianceModel").trim() + "%'");
		}
		if(StringUtils.isNotBlank(map.get("logisticsNumber"))){
			sb.append(" and a.logistics_number like '%" + map.get("logisticsNumber").trim() + "%'");
		}
		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'");
		}

		return sb.toString();
	}

}
