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

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.dao.FittingRefundDao;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author lzp
 * @date 2019/10/11 14:28
 * 平台备件返厂service
 */
@Service
public class FittingRefundService extends BaseService {

	@Autowired
	private FittingRefundDao fittingRefundDao;

	public void getAllRefundFitList(LayuiPage page, Map<String, String> map) {
		List<Record> list = fittingRefundDao.getAllFittingData(page, map);
		page.setData(list);
		page.setCount(fittingRefundDao.queryFittingCount(map));
	}
    //平台获取出入库明细数据
	public void getPlatformInAndOutWarehousingData(LayuiPage page, Map<String, String> map){
		List<Record> list=fittingRefundDao.getPlatformInAndOutWarehousingData(page,map);
		page.setData(list);
		page.setCount(fittingRefundDao.getPlatformInAndOutWarehousingDataCount(map));
	}

	public Record getFitRefundDetail(String id){
		StringBuilder sb = new StringBuilder();
		sb.append("select a.*,b.status as orderStatus,b.employe_name ,b.number as order_number,");
		sb.append(" b.power_station_name,b.warranty_type,b.customer_name,b.customer_mobile,");
		sb.append(" CONCAT(b.province,b.city,b.area,b.customer_address)AS Address ");
		sb.append(" from crm_platform_fitting_refund a");
		sb.append(" left join crm_order b on a.order_id=b.id");
		sb.append(" where a.id=? ");
		return Db.findFirst(sb.toString(), id);
	}

	/**
	 * 获取新建、旧件返厂信息
	 * @param orderId
	 * @return
	 */
	public List<Record> getFitRefundInfo(String orderId,String siteId){
		StringBuffer sf = new StringBuffer();
		sf.append("SELECT f.fitting_name,f.logistics_names,f.logistics_number FROM crm_platform_fitting_refund f WHERE f.status IN('1','2') AND f.order_id = ? ");
		if (StringUtil.isNotBlank(siteId)) {
			sf.append(" AND f.site_id= '" + siteId + "' ");
		}
		return Db.find(sf.toString(),orderId);
	}

}
