package com.sifang.modules.sunshine.financialManagement.service;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.RandomUtil;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.financialManagement.dao.CrmOrderSiteSettlementDao;
import com.sifang.modules.sunshine.financialManagement.entity.CrmOrderSiteSettlement;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.CrmUtils;

@Service
public class CrmOrderSiteSettlementService extends BaseService {

	@Autowired
	private CrmOrderSiteSettlementDao crmOrderSiteSettlementDao;

	// 加载服务商待结工单列表分页数据
	public Page<Record> loadWaitSettlementList(Page<Record> page, Map<String, String> map) {
		List<Record> list = getWaitSettlementList(page, map);// list
		Long count = getWaitSettlementCount(map);// count
		page.setCount(count);
		page.setList(list);
		return page;
	}

	// 查询列表数据
	public List<Record> getWaitSettlementList(Page<Record> page, Map<String, String> map) {
		StringBuilder sb = new StringBuilder("");
		sb.append(
				" SELECT o.*,a.id as settleId,a.type,a.service_costs,a.reward_costs,a.other_cost,a.mileage_cost,a.remarks AS settleRemarks,a.site_billing_id,a.exigency_costs,a.cost_detail,a.settlement_time, ");
		sb.append(
				" DATE_FORMAT(o.repair_time,'%Y-%m-%d %H:%i:%s') AS repairTime,DATE_FORMAT(o.end_time,'%Y-%m-%d %H:%i:%s') AS endTime,DATE_FORMAT(a.settlement_time,'%Y-%m-%d %H:%i:%s') AS settlementTime ");
		sb.append(" FROM crm_order_site_settlement a INNER JOIN crm_order o ON a.order_id=o.id WHERE a.jz_flag='0' ");// jz_flag：0待结账
		sb.append(sqlConditions(map));
		sb.append(" order by a.settlement_time desc  ");
		if (page != null) {
			sb.append(" limit " + page.getPageSize() + " offset " + (page.getPageNo() - 1) * page.getPageSize());
		}
		List<Record> list = Db.find(sb.toString());
		return list;
	}

	// 获取总数量count
	public Long getWaitSettlementCount(Map<String, String> map) {
		StringBuilder sb = new StringBuilder("");
		sb.append(
				" SELECT count(*) ");
		sb.append(" FROM crm_order_site_settlement a INNER JOIN crm_order o ON a.order_id=o.id WHERE a.jz_flag='0' ");// jz_flag：0待结账
		sb.append(sqlConditions(map));
		return Db.queryLong(sb.toString());
	}

	// 列表查询条件
	public String sqlConditions(Map<String, String> map) {
		StringBuilder sb = new StringBuilder("");
		if (map != null) {
			if (StringUtil.checkParamsValid(map.get("siteName"))) {
				sb.append(" and o.site_id ='" + map.get("siteName") + "'");
			}
			if (StringUtil.checkParamsValid(map.get("orderNumber"))) {
				sb.append(" and o.number like '%" + map.get("orderNumber").trim() + "%'");
			}
			if (StringUtil.checkParamsValid(map.get("startTime"))) {
				sb.append(" and a.settlement_time >= '" + map.get("startTime") + " 00:00:00'");
			}
			if (StringUtil.checkParamsValid(map.get("endTime"))) {
				sb.append(" and a.settlement_time <= '" + map.get("endTime") + " 23:59:59'");
			}
		}
		return sb.toString();
	}

	// 根据条件获取服务商工单待结账数据
	public List<Record> getSettleListByConditions(String siteId, String startTime, String endTime, String orderNumber) {
		StringBuilder sb = new StringBuilder("");
		sb.append(
				" SELECT o.*,a.id as settleId,a.type,ifnull(a.service_costs,0) as service_costs,ifnull(a.reward_costs,0) as reward_costs,ifnull(a.other_cost,0) as other_cost,ifnull(a.mileage_cost,0) as mileage_cost ,a.remarks AS settleRemarks,a.site_billing_id,ifnull(a.exigency_costs,0) as exigency_costs,a.cost_detail,a.settlement_time, ");
		sb.append(
				" DATE_FORMAT(o.repair_time,'%Y-%m-%d %H:%i:%s') AS repairTime,DATE_FORMAT(o.end_time,'%Y-%m-%d %H:%i:%s') AS endTime,DATE_FORMAT(a.settlement_time,'%Y-%m-%d %H:%i:%s') AS settlementTime ");
		sb.append(" FROM crm_order_site_settlement a INNER JOIN crm_order o ON a.order_id=o.id WHERE a.jz_flag='0' ");// jz_flag：0待结账
		sb.append(" and a.site_id=? and a.settlement_time >= '" + startTime + " 00:00:00' and a.settlement_time <= '" + endTime + " 23:59:59' ");
		if (StringUtils.isNotBlank(orderNumber)) {
			sb.append(" and o.number like '%" + orderNumber.trim() + "%' ");
		}
		return Db.find(sb.toString(), siteId);
	}

	// 服务商待结账单合计数据
	public Record countSettlementDatas(Map<String, String> map) {
		StringBuilder sb = new StringBuilder("");
		sb.append(" SELECT SUM(o.appliance_num) AS appliance_num_count,SUM(IFNULL(a.service_costs,0)) AS service_costs_count, ");
		sb.append(" SUM(IFNULL(a.reward_costs,0)) AS reward_costs_count,SUM(IFNULL(a.other_cost,0)) AS other_cost_count, ");
		sb.append(" SUM(IFNULL(a.mileage_cost,0)) AS mileage_cost_count,SUM(IFNULL(a.exigency_costs,0)) AS exigency_costs_count, ");
		sb.append(" SUM(IFNULL(a.mileage_cost,0)+IFNULL(a.exigency_costs,0)+IFNULL(a.other_cost,0)+IFNULL(a.reward_costs,0)+IFNULL(a.service_costs,0)) AS all_cost_count ");
		sb.append(" FROM crm_order_site_settlement a INNER JOIN crm_order o ON a.order_id=o.id WHERE a.jz_flag='0' ");// jz_flag：0待结账
		sb.append(sqlConditions(map));
		return Db.findFirst(sb.toString());
	}

	public Record getOrderSettlement(String orderId) {
		return Db.findFirst("SELECT * FROM crm_order_site_settlement WHERE order_id=?", orderId);
	}

}
