package com.hfkj.service.group.impl;

import com.hfkj.dao.BsStoreGroupContentMapper;
import com.hfkj.dao.BsStoreGroupDetailMapper;
import com.hfkj.dao.BsStoreGroupServeRuleMapper;
import com.hfkj.entity.*;
import com.hfkj.service.group.BsStoreGroupProductService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

@Service("bsStoreGroupProductService")
public class BsStoreGroupProductServiceImpl implements BsStoreGroupProductService {

    @Resource
    private BsStoreGroupDetailMapper storeGroupDetailMapper;

    @Resource
    private BsStoreGroupContentMapper storeGroupContentMapper;

    @Resource
    private BsStoreGroupServeRuleMapper storeGroupServeRuleMapper;

    @Override
    public void insertStoreGroupContent(BsStoreGroupContent bsStoreGroupContent) {
        storeGroupContentMapper.insert(bsStoreGroupContent);
    }

    @Override
    public void editStoreGroupContent(BsStoreGroupContent bsStoreGroupContent) {
        storeGroupContentMapper.updateByPrimaryKey(bsStoreGroupContent);
    }

    @Override
    public BsStoreGroupContent findStoreGroupContent(Long id) {
        return storeGroupContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsStoreGroupContent> getStoreGroupContentByList(Map<String, Object> map) {
        BsStoreGroupContentExample example = new BsStoreGroupContentExample();
        BsStoreGroupContentExample.Criteria criteria = example.createCriteria();

        if (org.apache.commons.collections4.MapUtils.getLong(map, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(map, "storeId"));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }


        return storeGroupContentMapper.selectByExample(example);
    }

    @Override
    public void insertGroupDetail(BsStoreGroupDetail storeGroupDetail) {
        storeGroupDetailMapper.insert(storeGroupDetail);
    }

    @Override
    public void editGroupDetail(BsStoreGroupDetail storeGroupDetail) {
        storeGroupDetailMapper.updateByPrimaryKeySelective(storeGroupDetail);
    }

    @Override
    public BsStoreGroupDetail findGroupDetail(Long id) {
        return storeGroupDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsStoreGroupDetail findGroupDetail(String productId , Long groupId) {
        BsStoreGroupDetailExample example = new BsStoreGroupDetailExample();
        BsStoreGroupDetailExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(1).andStoreProductIdEqualTo(productId).andGroupIdEqualTo(groupId);

        List<BsStoreGroupDetail> list = storeGroupDetailMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public void deleteGroupDetail(Long id) {
        storeGroupDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<BsStoreGroupDetail> getGroupDetailByList(Map<String, Object> map) {

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

        if (MapUtils.getLong(map, "groupId") != null) {
            criteria.andGroupIdEqualTo(MapUtils.getLong(map, "groupId"));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }



        return storeGroupDetailMapper.selectByExample(example);
    }

    @Override
    public void insertGroupServeRule(BsStoreGroupServeRule storeGroupServeRule) {
        storeGroupServeRuleMapper.insert(storeGroupServeRule);
    }

    @Override
    public void editGroupServeRule(BsStoreGroupServeRule storeGroupServeRule) {
        storeGroupServeRuleMapper.updateByPrimaryKeySelective(storeGroupServeRule);
    }

    @Override
    public BsStoreGroupServeRule findGroupServeRule(Long id) {
        return storeGroupServeRuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsStoreGroupServeRule findGroupServeRuleByGroup(Long groupId) {
        BsStoreGroupServeRuleExample example = new BsStoreGroupServeRuleExample();
        BsStoreGroupServeRuleExample.Criteria criteria = example.createCriteria();

        criteria.andGroupIdEqualTo(groupId);

        List<BsStoreGroupServeRule> list = storeGroupServeRuleMapper.selectByExample(example);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public List<BsStoreGroupServeRule> getGroupServeRuleByList(Map<String, Object> map) {
        return null;
    }
}
