package com.neusoft.smart.pos.promotion.persistent.repository;

import com.neusoft.smart.pos.promotion.persistent.domain.PromotionPayChannel;
import com.neusoft.smart.pos.promotion.persistent.domain.PromotionPayChannelExample;
import com.neusoft.smart.pos.promotion.persistent.ext.domain.PromotionPayChannelQuery;
import com.neusoft.smart.pos.promotion.persistent.mapper.PromotionPayChannelMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Component
public class PromotionPayChannelRepository {
    
    @Autowired
    private PromotionPayChannelMapper promotionPayChannelMapper;
    
    /**
     * @param promotionPayChannel
     */
    public void create(PromotionPayChannel promotionPayChannel) {
        promotionPayChannel.setCreateBy(RequestHeaderUtil.getUserId());
        promotionPayChannel.setCreateTime(new Date());
        promotionPayChannelMapper.insertSelective(promotionPayChannel);
    }
    
    
    /**
     *
     * @param promotionPayChannel
     */
    public void update(PromotionPayChannel promotionPayChannel) {
        promotionPayChannelMapper.updateByPrimaryKey(promotionPayChannel);
    }
    
    /**
     *
     * @param id
     */
    public void delete(Integer id) {
        promotionPayChannelMapper.deleteByPrimaryKey(id);
    }
    
    /**
     *
     * @param id
     * @return
     */
    public PromotionPayChannel get(Integer id) {
        return promotionPayChannelMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 查询列表
     *
     * @param promotionPayChannelQuery
     * @return
     */
    public List<PromotionPayChannel> getList(PromotionPayChannelQuery promotionPayChannelQuery) {
        PromotionPayChannelExample promotionPayChannelExample = this.promotionPayChannelExampleProvider(promotionPayChannelQuery);
        if (promotionPayChannelQuery.getIsPage()) {
            promotionPayChannelExample.setLimit(promotionPayChannelQuery.getLimit());
            promotionPayChannelExample.setOffset(promotionPayChannelQuery.getOffset());
        }
        promotionPayChannelExample.setOrderByClause("create_time desc");
        return promotionPayChannelMapper.selectByExample(promotionPayChannelExample);
    }
    
    /**
     * @param promotionPayChannelQuery
     * @return
     */
    public Integer getCount(PromotionPayChannelQuery promotionPayChannelQuery) {
        PromotionPayChannelExample promotionPayChannelExample = this.promotionPayChannelExampleProvider(promotionPayChannelQuery);
        return promotionPayChannelMapper.countByExample(promotionPayChannelExample);
    }
    
    private PromotionPayChannelExample promotionPayChannelExampleProvider(@NotNull PromotionPayChannelQuery promotionPayChannelQuery) {
        PromotionPayChannelExample PromotionPayChannelExample = new PromotionPayChannelExample();
        PromotionPayChannelExample.Criteria criteria = PromotionPayChannelExample.createCriteria();
        
        if (null != promotionPayChannelQuery.getOffType()) {
            criteria.andOffTypeEqualTo(promotionPayChannelQuery.getOffType());
        }
        if (null != promotionPayChannelQuery.getOrgId()) {
            criteria.andOrgIdEqualTo(promotionPayChannelQuery.getOrgId());
        }
        if (null != promotionPayChannelQuery.getStoreId()) {
            criteria.andStoreIdEqualTo(promotionPayChannelQuery.getStoreId());
        }
        if (null != promotionPayChannelQuery.getPayChannel()) {
            criteria.andPayChannelEqualTo(promotionPayChannelQuery.getPayChannel());
        }
        if (promotionPayChannelQuery.getIsFilteredByRole() && !"sa".equals(RequestHeaderUtil.getRole())) {
            criteria.andOrgIdEqualTo(RequestHeaderUtil.getOrgId());
        }
        if(promotionPayChannelQuery.getIsStoreIdNull()) {
            criteria.andStoreIdEqualTo(0);
        }
        return PromotionPayChannelExample;
    }
}
