package com.neusoft.smart.pos.trade.repository;


import com.neusoft.smart.pos.trade.persistent.domain.BillDetail;
import com.neusoft.smart.pos.trade.persistent.domain.BillDetailExample;
import com.neusoft.smart.pos.trade.persistent.domain.BillDetailProfitExample;
import com.neusoft.smart.pos.trade.persistent.ext.mapper.BillDetailExtMapper;
import com.neusoft.smart.pos.trade.persistent.mapper.BillDetailMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zcf
 * @date 2018-02-23 10：43
 * @description 商户支结算详情信息管理使用Repository
 */
@Component
public class BillDetailRepository {

	@Resource
	private BillDetailMapper billDetailMapper;

	@Resource
	private BillDetailExtMapper billDetailExtMapper;
	
	/**
	 * 商户支付渠道信息保存方法
	 * @return
	 */
	public Boolean saveBillDetail(BillDetail billDetail){
		val count = billDetailMapper.insertSelective(billDetail);
		return count>0?true:false;
	}
	
	/**
	 * 结算单明细数据条数方法
	 * @return
	 */
	public Integer queryBillDetailCount(Integer shopId,String billNumber){
		BillDetailExample example = new BillDetailExample();
		example.createCriteria().andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber);
		return billDetailMapper.countByExample(example);
	}
	
	/**
	 * 结算单明细数据条数方法
	 * @return
	 */
	public Integer queryBillDetailCount(BillDetailExample example){
		return billDetailMapper.countByExample(example);
	}
	
	/**
	 * 结算单明细数据查询方法
	 * @return
	 */
	public List<BillDetail> queryBillDetails(BillDetailExample example){
		return billDetailMapper.selectByExample(example);
	}

	/**
	 * 查询门店支付方式分润汇总信息
	 * @param shopId
	 * @param billNumber
	 * @return
	 */
	public Map queryShopPayTypeProfit(Integer shopId,String billNumber,String payType,String subjectName){
		BillDetailProfitExample example = new BillDetailProfitExample();
		example.createCriteria().andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber)
				.andPayTypeEqualTo(payType)
				.andSubjectNameEqualTo(subjectName);
		return billDetailExtMapper.queryShopPayTypeProfit(example);
	}

	/**
	 * 查询门店支付方式分润汇总信息
	 * @param shopId
	 * @param billNumber
	 * @return
	 */
	public List<Map> queryBillDetailProfit(Integer shopId,String billNumber,String payType){
		BillDetailProfitExample example = new BillDetailProfitExample();
		example.createCriteria().andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber)
				.andPayTypeEqualTo(payType);
		return billDetailExtMapper.queryBillDetailProfit(example);
	}

	/**
	 * 根据月份查询结算编号列表信息
	 * @return
	 */
	public List<Map> queryBillNumberList(Integer orgId, Integer shopId,String month){
		Map example = new HashMap();
		example.put("orgId",orgId);
		example.put("shopId",shopId);
		example.put("month",month);
//		BillMasterExample example = new BillMasterExample();
//		example.createCriteria().andOrgIdEqualTo(orgId)
//				.andBillNumberEqualTo(month);
		return billDetailExtMapper.queryBillNumberList(example);
	}
	
	/**
	 * 删除账单信息方法
	 * @return
	 */
	public Integer deleteBillDetailByBillNumber(Integer orgId,String billNumber){
		val example = new BillDetailExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andBillNumberEqualTo(billNumber);
		return billDetailMapper.deleteByExample(example);
	}
	
	/**
	 * 删除账单信息方法
	 * @return
	 */
	public Integer deleteBillDetailByBillNumber(Integer orgId,Integer shopId,String billNumber){
		val example = new BillDetailExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber);
		return billDetailMapper.deleteByExample(example);
	}

	/**
	 * 根据集团编号查询分润主体列表信息
	 * @return
	 */
	public List<Map<String,String>> querySubjectNameListByOrgId(Integer orgId){
		Map example = new HashMap();
		example.put("orgId",orgId);
		return billDetailExtMapper.querySubjectNameListByOrgId(example);
	}
}
