package cn.superdesk.uniorder.service.channel.impl;

import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelProfitXVirt;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelProfitXVirtExample;
import cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitXVirtMapper;
import cn.superdesk.uniorder.service.channel.UnifiedChannelProfitXVirtService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 8/28/20 5:13 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 8/28/20 5:13 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UnifiedChannelProfitXVirtServiceImpl implements UnifiedChannelProfitXVirtService {


    @Autowired
    private UnifiedChannelProfitXVirtMapper unifiedChannelProfitXVirtMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(UnifiedChannelProfitXVirt entity) {
        return unifiedChannelProfitXVirtMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedChannelProfitXVirt entity) {
        return unifiedChannelProfitXVirtMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedChannelProfitXVirt findById(Long id) {
        return unifiedChannelProfitXVirtMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<UnifiedChannelProfitXVirt> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<UnifiedChannelProfitXVirt> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /////////////////


    /****************************************** extends BaseService 部分   end ******************************************/


    public void addUnifiedChannelProfitXVirt(UnifiedChannelProfitXVirt entity) {
        unifiedChannelProfitXVirtMapper.insertSelective(entity);
    }

    public void updateUnifiedChannelProfitXVirt(UnifiedChannelProfitXVirt entity) {
        UnifiedChannelProfitXVirt originEntity = unifiedChannelProfitXVirtMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedChannelProfitXVirtMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedChannelProfitXVirt(Long id) {
        unifiedChannelProfitXVirtMapper.deleteByPrimaryKey(id);
    }

    public UnifiedChannelProfitXVirt findUnifiedChannelProfitXVirtById(Long id) {
        UnifiedChannelProfitXVirt entity = unifiedChannelProfitXVirtMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public R<List<UnifiedChannelProfitXVirt>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        // 样例 保留
//		UnifiedChannelProfitXVirt unifiedChannelProfitXVirtConditions = BeanUtils.mapToBean(conditions,UnifiedChannelProfitXVirt.class);

        UnifiedChannelProfitXVirtExample          example  = new UnifiedChannelProfitXVirtExample();
        UnifiedChannelProfitXVirtExample.Criteria criteria = example.createCriteria();

        PageHelper.startPage(pageNun, pageSize);
        List<UnifiedChannelProfitXVirt>     resultList = unifiedChannelProfitXVirtMapper.selectByExample(example);
        PageInfo<UnifiedChannelProfitXVirt> page       = new PageInfo<UnifiedChannelProfitXVirt>(resultList);
        PageHelper.clearPage();

        return new R<List<UnifiedChannelProfitXVirt>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    //////

    public List<Long> getVirtsByChannelId(Long channelId) {

        List<Long> result = null;

        UnifiedChannelProfitXVirtExample example = new UnifiedChannelProfitXVirtExample();
        example.createCriteria()
                .andChannelIdEqualTo(channelId);

        List<UnifiedChannelProfitXVirt> resultSearch = unifiedChannelProfitXVirtMapper.selectByExample(example);

        if (AnCollectionUtils.isNotEmpty(resultSearch)) {
            result = resultSearch.stream()
                    .map(UnifiedChannelProfitXVirt::getVirtId)
                    .collect(Collectors.toList());
        }

        return result;

    }

    public List<UnifiedChannelProfitXVirt> findVirtsByChannelId(Long channelId) {

        UnifiedChannelProfitXVirtExample example = new UnifiedChannelProfitXVirtExample();
        example.createCriteria()
                .andChannelIdEqualTo(channelId);

        return unifiedChannelProfitXVirtMapper.selectByExample(example);

    }


}
