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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.trade.persistent.domain.BillMaster;
import com.neusoft.smart.pos.trade.persistent.domain.BillMasterExample;
import com.neusoft.smart.pos.trade.persistent.mapper.BillMasterMapper;
import lombok.val;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

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

	@Resource
	private BillMasterMapper billMasterMapper;
	
	/**
	 * 商户支付渠道信息保存方法
	 * @return
	 */
	public Boolean saveBillMaster(BillMaster billMaster){
		val count = billMasterMapper.insertSelective(billMaster);
		return count>0?true:false;
	}
	
	/**
	 * 账单信息查询方法
	 * @return
	 */
	public BillMaster queryBillMasterByBillNumber(Integer orgId,Integer shopId,String billNumber,String acceptStatus){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber)
				.andAcceptStatusEqualTo(acceptStatus);
		val billMasters = billMasterMapper.selectByExample(example);
		return billMasters.size()>0?billMasters.get(0):null;
	}
	
	/**
	 * 账单信息查询方法
	 * @return
	 */
	public BillMaster queryBillMasterByBillNumber(Integer orgId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andBillNumberEqualTo(billNumber);
		val billMasters = billMasterMapper.selectByExample(example);
		return billMasters.size()>0?billMasters.get(0):null;
	}
	
	/**
	 * 根据集团及账单编号查询账单信息是否存在
	 * @return
	 */
	public Integer queryBillMasterCountByBillNumber(Integer orgId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andBillNumberEqualTo(billNumber);
		return billMasterMapper.countByExample(example);
	}
	
	/**
	 * 根据集团及账单编号查询账单信息是否存在
	 * @return
	 */
	public Integer queryBillMasteAcceptrCountByBillNumber(Integer orgId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andBillNumberEqualTo(billNumber)
				.andAcceptStatusEqualTo(DictionaryConstant.BILL_ACCEPT_STATUS_YES.getCode());
		return billMasterMapper.countByExample(example);
	}
	
	/**
	 * 根据集团及账单编号查询账单信息是否存在
	 * @return
	 */
	public Integer queryBillMasterCountByBillNumber(Integer orgId,Integer shopId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber);
		return billMasterMapper.countByExample(example);
	}
	
	/**
	 * 账单归档状态更新方法
	 * @return
	 */
	public Boolean updateOrderStatusByOrderNumber(Integer orgId,String billNumber,String acceptStatus){
		Date updateDate = new Date();
		val billMaster = new BillMaster();
		billMaster.setAcceptStatus(acceptStatus);
		billMaster.setAcceptTime(updateDate);
		billMaster.setUpdatedAt(updateDate);
		val example = new BillMasterExample();
		example.createCriteria().andBillNumberEqualTo(billNumber)
				.andOrgIdEqualTo(orgId);
		val result = billMasterMapper.updateByExampleSelective(billMaster,example);
		return result>0?true:false;
	}
	
	/**
	 * 账单归档状态更新方法
	 * @return
	 */
	public Boolean updateOrderStatusByOrderNumber(Integer orgId,Integer shopId,String billNumber,String acceptStatus){
		Date updateDate = new Date();
		val billMaster = new BillMaster();
		billMaster.setAcceptStatus(acceptStatus);
		billMaster.setAcceptTime(updateDate);
		billMaster.setUpdatedAt(updateDate);
		val example = new BillMasterExample();
		example.createCriteria().andBillNumberEqualTo(billNumber)
				.andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId);
		val result = billMasterMapper.updateByExampleSelective(billMaster,example);
		return result>0?true:false;
	}
	
	/**
	 * 删除账单信息方法
	 * @return
	 */
	public Integer deleteBillMasterByBillNumber(Integer orgId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andBillNumberEqualTo(billNumber);
		return billMasterMapper.deleteByExample(example);
	}
	
	/**
	 * 删除账单信息方法
	 * @return
	 */
	public Integer deleteBillMasterByBillNumber(Integer orgId,Integer shopId,String billNumber){
		val example = new BillMasterExample();
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId)
				.andBillNumberEqualTo(billNumber);
		return billMasterMapper.deleteByExample(example);
	}
}
