package com.sifang.modules.order.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.entity.OrderCollections;
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.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 二维码收款DAO接口
 *
 * @author Ivan
 * @version 2019-11-19
 */
@Repository
public class OrderCollectionsDao extends BaseDao<OrderCollections> {

	public List<Record> orderCollectionsList(LayuiPage page, Map<String, String> map) {
		StringBuffer sf = new StringBuffer();
		sf.append(
				"select a.*,a.order_number as orderNumber,o.site_name,o.service_measures from crm_order_collections a left join crm_order o on o.id=a.order_id  where a.status='0'  ");
		sf.append(selectConditions(map));
		sf.append("order by a.create_time desc");
		if (page != null) {
			sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
		}
		return Db.find(sf.toString());
	}

	public Long queryCount( Map<String, String> map) {
		StringBuffer sf = new StringBuffer();
		sf.append("select count(*) from crm_order_collections a left join crm_order o on o.id=a.order_id where a.status='0'   ");
		sf.append(selectConditions(map));
		return Db.queryLong(sf.toString());
	}

	public String selectConditions(Map<String, String> map) {
		StringBuilder stringBuilder = new StringBuilder();
		if (map != null) {
			if (map.get("number") != null ) {
				stringBuilder.append(" and a.order_number like '%" + map.get("number") + "%' ");
			}
			if (map.get("paymentType") != null && StringUtil.isNotEmpty(map.get("paymentType"))) {
				stringBuilder.append(" and a.payment_type = '" + map.get("paymentType") + "' ");
			}
			if (map.get("employeName") != null && StringUtil.isNotEmpty(map.get("employeName"))) {
				stringBuilder.append(" and a.employe_name = '" + map.get("employeName") + "' ");
			}
			if (map.get("siteId") != null && StringUtil.isNotEmpty(map.get("siteId"))) {
				stringBuilder.append(" and a.site_id = '" + map.get("siteId") + "' ");
			}
			if (map.get("description") != null && StringUtil.isNotEmpty(map.get("description"))) {
				stringBuilder.append(" and a.description like '%" + map.get("description") + "%' ");
			}
			if (map.get("source") != null && StringUtil.isNotEmpty(map.get("source"))) {
				stringBuilder.append(" and a.source='" + map.get("source") + "' ");
			}

			if(StringUtil.checkParamsValid(map.get("startTime"))){
				String startTime = map.get("startTime");
				stringBuilder.append(" and a.create_time >= '"+startTime+"'");
			}
			if(StringUtil.checkParamsValid(map.get("endTime"))){
				String endTime = map.get("endTime");
				stringBuilder.append(" and a.create_time <= '"+endTime+"'");
			}

			if (map.get("commissionStatus") != null && StringUtil.isNotEmpty(map.get("commissionStatus"))) {
				if ("0".equals(map.get("commissionStatus"))) {
					stringBuilder.append(" and  (a.commission_status = '0' or a.commission_status is null )");
				} else {
					stringBuilder.append(" and a.commission_status = '1' ");
				}
			}
			if (map.get("confirm") != null && StringUtil.isNotEmpty(map.get("confirm"))) {
				if ("0".equals(map.get("confirm"))) {
					stringBuilder.append(" and  (a.confirm = '0' or a.confirm is null )");
				} else {
					stringBuilder.append(" and (a.confirm = '1' or a.confirm = '2') ");
				}
			}

		}
		return stringBuilder.toString();
	}

	private String getParamValue(Map<String, Object> map, String param) {
		Object value = map.get(param);
		return value == null ? null : ((String[]) map.get(param))[0];
	}

	/**
	 * 包括工单的现金收款记录和工单中的收款记录。
	 */
	public BigDecimal getOrderPayment(String orderId) {
		StringBuilder kit = new StringBuilder().append("select sum(payment_amount) as sk ").append("from crm_order_collections").append(" where order_id=? and status='0'");
		BigDecimal ret = Db.queryBigDecimal(kit.toString(), orderId);
		return ret == null ? new BigDecimal("0") : ret;
	}

	private String getTrimmedParamValue(Map<String, Object> map, String param) {
		return org.apache.commons.lang.StringUtils.trim(getParamValue(map, param));
	}

	public BigDecimal orderCollectionsSumamount( Map<String, String> map) {
		StringBuffer sf = new StringBuffer();
		sf.append("select round(sum(payment_amount),2) from crm_order_collections a where a.status='0'  ");
		sf.append(selectConditions(map));
		sf.append("order by a.create_time desc");
		return Db.queryBigDecimal(sf.toString());

	}

	public BigDecimal commissionSumanount(Map<String, String> map) { // todo 应该包含现金收款和转账吗
		StringBuffer sf = new StringBuffer();
		sf.append("select round(sum(commission_money),2) from crm_order_collections a where a.status='0'  ");
		sf.append(selectConditions(map));
		sf.append("order by a.create_time desc");
		return Db.queryBigDecimal(sf.toString());
	}

	public Record getDetailbyid(String id) {
		String sql = "select * from crm_order_collections where id=? and status='0' ";
		return Db.findFirst(sql, id);
	}

	public int confirmdz(String id) {
		Record rd = Db.findFirst("select a.* from crm_order_collections a where a.id=?", id);
		if ("4".equals(rd.getStr("source"))) {// 优服
			return Db.update("update  crm_order_collections set confirm='2' where id=? and status='0' ", id);
		} else {
			return Db.update("update  crm_order_collections set confirm='1' where id=? and status='0' ", id);
		}
	}

	public int saveCommission(String id, String commissionMoney, String marks) {
		String sql = "update crm_order_collections set commission_money=?,marks=?, commission_status='1' where id=?";
		return Db.update(sql, commissionMoney, marks, id);
	}


	public List<Record> getOrderCollectionList(String orderId){
		return Db.find("select a.*,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') createTime from crm_order_collections a where a.order_id=? and a.status='0' ",orderId);
	}

}
