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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.trade.persistent.domain.*;
import com.neusoft.smart.pos.trade.persistent.mapper.BillSettlementRuleMapper;
import lombok.val;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-10-31 10：43
 * @description 账单结算规则信息管理使用Repository
 */
@Component
public class BillSettlementRuleRepository {

	@Resource
	private BillSettlementRuleMapper billSettlementRuleMapper;
	
	/**
	 * 结算规则列表查询方法
	 * @return
	 */
	public List<BillSettlementRule> queryBillSettlementRules(Integer orgId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		example.createCriteria().andOrgIdEqualTo(orgId);
		example.setOrderByClause("sort_no desc,created_at desc");
		return billSettlementRuleMapper.selectByExample(example);
	}
	
	/**
	 * 结算规则列表查询方法
	 * @return
	 */
	public List<BillSettlementRule> queryBillSettlementRulesByShopId(Integer orgId,Integer shopId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		BillSettlementRuleExample.Criteria criteria1 = example.createCriteria();
		criteria1.andOrgIdEqualTo(orgId)
				 .andShopIdEqualTo(shopId);
		
		BillSettlementRuleExample.Criteria criteria2 = example.createCriteria();
		criteria2.andOrgIdEqualTo(orgId)
		         .andShopIdEqualTo(0);
		example.or(criteria2);
		example.setOrderByClause("sort_no desc,created_at desc");
		return billSettlementRuleMapper.selectByExample(example);
	}
	
	/**
	 * 结算规则查询方法
	 * @return
	 */
	public BillSettlementRule queryBillSettlementRuleById(Integer ruleId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		//如果shopId为0则查询集团默认规则列表
		example.createCriteria().andIdEqualTo(ruleId);
		example.setOrderByClause("sort_no desc,created_at desc");
		List<BillSettlementRule> billSettlementRuleList = billSettlementRuleMapper.selectByExample(example);
		return billSettlementRuleList.size()>0?billSettlementRuleList.get(0):null;
	}
	
	/**
	 * 集团默认结算规则列表查询方法
	 * @return
	 */
	public BillSettlementRule queryOrgDefaultBillRule(Integer orgId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		//如果shopId为0则查询集团默认规则列表
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(0)
				.andRateStartTimeEqualTo("")
				.andRateEndTimeEqualTo("")
				.andRuleStatusEqualTo(DictionaryConstant.BILL_RULE_STATUS_YES.getCode());
		example.setOrderByClause("sort_no desc,created_at desc");
		List<BillSettlementRule> billSettlementRuleList = billSettlementRuleMapper.selectByExample(example);
		return billSettlementRuleList.size()>0?billSettlementRuleList.get(0):null;
	}
	
	/**
	 * 结算规则列表查询方法
	 * @return
	 */
	public List<BillSettlementRule> queryShopBillSettlementRuleList(Integer orgId,Integer shopId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		//如果shopId为0则查询集团默认规则列表
		example.createCriteria().andOrgIdEqualTo(orgId)
				.andShopIdEqualTo(shopId);
		example.setOrderByClause("sort_no desc,created_at desc");
		return billSettlementRuleMapper.selectByExample(example);
	}
	
	/**
	 * 商户结算规则信息删除方法
	 * @return
	 */
	public Boolean deleteBillSettlementRule(Integer orgId,Integer ruleId){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		example.createCriteria()
				.andOrgIdEqualTo(orgId)
				.andIdEqualTo(ruleId);
		int count = billSettlementRuleMapper.deleteByExample(example);
		return count>0?true:false;
	}
	
	/**
	 * 商户结算规则信息保存方法
	 * @return
	 */
	public Integer saveBillSettlementRule(BillSettlementRule billSettlementRule){
		return billSettlementRuleMapper.insertSelective(billSettlementRule);
	}
	
	/**
	 * 结算规则信息更新方法
	 * @return
	 */
	public Boolean updateBillSettlemenDetailRule(Integer ruleId,BillSettlementRule billSettlementRule){
		billSettlementRule.setRuleId(null);
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		example.createCriteria().andIdEqualTo(ruleId);
		int result = billSettlementRuleMapper.updateByExampleSelective(billSettlementRule,example);
		return result>0?true:false;
	}
	
	/**
	 * 结算规则信息更新方法
	 * @return
	 */
	public Boolean updateBillSettlemenDetailRule(Integer orgId,Integer ruleId,Integer sortNo){
		BillSettlementRuleExample example = new BillSettlementRuleExample();
		example.createCriteria().andIdEqualTo(ruleId)
				.andOrgIdEqualTo(orgId);
		BillSettlementRule billSettlementRule = new BillSettlementRule();
		billSettlementRule.setSortNo(sortNo);
		int result = billSettlementRuleMapper.updateByExampleSelective(billSettlementRule,example);
		return result>0?true:false;
	}
}
