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.sunshine.fittingManager.entity.CrmPlantformFittingApply;
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
 */
@Repository
public class FittingApplyDao extends BaseDao<CrmPlantformFittingApply> {

	/**
	 * 备件申请数据
	 * @param status 申请状态：1.待发货 2.已发货
	 * @return
	 */
	public List<Record> getAllApplyList(LayuiPage<Record> page, Map<String, String> map, String status) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT a.id,a.site_id,a.site_name,a.`fitting_name`,a.`fitting_code`,a.`apply_num`,a.status,a.applicant_name,a.fitting_version,a.apply_marks,a.site_price ");
		sb.append(" ,a.`delivery_name`,a.`delivery_mobile`,a.`delivery_address`,a.refuse_reason,a.confirm_time,a.out_fitting_time,a.out_fitting_marks,a.fitting_audit_num,a.factory_marks ");
		sb.append(" ,a.`create_time`,a.logistics_number,a.logistics_names,a.old_fitting_flag ");
		sb.append(" ,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime");
		sb.append(" ,DATE_FORMAT(a.send_time,'%Y-%m-%d %H:%i:%s') as sendTime");
		sb.append(" FROM `crm_platform_fitting_apply` a where 1=1 ");
		if(StringUtil.isNotBlank(status)){
			sb.append(" and a.status='" + status + "' ");
		}else{
			sb.append(" and  a.status in ('1','2','3','4','7') ");
		}
		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 queryApplyDataCount(Map<String, String> map, String status) {
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from crm_platform_fitting_apply a where 1=1 ");
		if(StringUtil.isNotBlank(status)){
			sb.append(" and a.status='" + status + "' ");
		}else{
			sb.append(" and a.status in ('1','2','3','4','7') ");
		}
		sb.append(queryCondition(map));
		return Db.queryLong(sb.toString());
	}

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

		if(StringUtils.isNotBlank((CharSequence) map.get("applyTimeSta"))){
			sb.append(" and a.create_time >= '" + map.get("applyTimeSta") + " 00:00:00' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("applyTimeEnd"))){
			sb.append(" and a.create_time <= '" + map.get("applyTimeEnd") + " 23:59:59' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("startShipTime"))){
			sb.append(" and a.confirm_time >= '" + map.get("startShipTime") + " 00:00:00' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("endShipTime"))){
			sb.append(" and a.confirm_time <= '" + map.get("endShipTime") + " 23:59:59' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("outTimeStart"))){
			sb.append(" and a.out_fitting_time >= '" + map.get("outTimeStart") + " 00:00:00' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("outTimeEnd"))){
			sb.append(" and a.out_fitting_time <= '" + map.get("outTimeEnd") + " 23:59:59' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("oldFittingFlag"))){
			sb.append(" and a.old_fitting_flag = '" + map.get("oldFittingFlag") + " ' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("applyMarks"))){
			sb.append(" and a.apply_marks = '" + map.get("applyMarks") + " ' ");
		}
		if(StringUtils.isNotBlank((CharSequence) map.get("factoryMarks"))){
			sb.append(" and a.factory_marks = '" + map.get("factoryMarks") + " ' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("applyStatus"))){
			sb.append(" and a.status = '" + map.get("applyStatus") + " ' ");
		}

		if(StringUtils.isNotBlank((CharSequence) map.get("hadReturnOldFit"))){
			if("0".equals((CharSequence) map.get("hadReturnOldFit"))){
				sb.append(" and d.fitting_type = '"+map.get("hadReturnOldFit")+"' ");
			}else if("1".equals((CharSequence) map.get("hadReturnOldFit"))){
				sb.append(" and d.fitting_type = '"+map.get("hadReturnOldFit")+"' ");
			}else{
				sb.append(" and d.id null ");
			}
		}


		return sb.toString();
	}

	/**
	 * 服务商首页统计
	 */
	public Record getSiteFittingApplyCount(String siteId){
		StringBuilder sf = new StringBuilder();
		sf.append("SELECT ");
		sf.append("COUNT(CASE WHEN a.status = '0' THEN 1 END) AS dckCount, ");
		sf.append("COUNT(CASE WHEN a.status = '1' THEN 1 END) AS ywcCount ");
		sf.append("FROM crm_site_fitting_apply a WHERE a.site_id=?  AND a.site_id IS NOT NULL  AND  DATE_FORMAT(a.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ");
		Record rd = Db.findFirst(sf.toString(),siteId);
		return rd;
	}

	/**
	 * 服务商平台首页统计
	 */
	public Record getpaltformFittingApplyCount(){
		StringBuilder sf = new StringBuilder();
		sf.append("SELECT ");
		sf.append("COUNT(CASE WHEN a.status = '1' THEN 1 END) AS dshCount, ");
		sf.append("COUNT(CASE WHEN a.status = '2' THEN 1 END) AS dckCount,");
		sf.append("COUNT(CASE WHEN a.status = '4' THEN 1 END) AS ywcCount ");
		sf.append("FROM crm_platform_fitting_apply a WHERE  DATE_FORMAT(a.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ");
		Record rd = Db.findFirst(sf.toString());
		return rd;
	}

}
