package com.hfkj.service.group.impl;

import com.hfkj.dao.BsStoreExtMsgMapper;
import com.hfkj.dao.BsStoreImgMapper;
import com.hfkj.dao.BsStoreProductMapper;
import com.hfkj.entity.*;
import com.hfkj.service.group.BsStoreGroupService;
import com.hfkj.service.group.BsStoreProductCategoryService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("bsStoreGroupService")
public class BsStoreGroupServiceImpl implements BsStoreGroupService {

    @Resource
    private BsStoreImgMapper bsStoreImgMapper;

    @Resource
    private BsStoreProductMapper storeProductMapper;

    @Resource
    private BsStoreExtMsgMapper storeExtMsgMapper;

    @Resource
    private BsStoreProductCategoryService storeProductCategoryService;

    @Override
    public void insertStoreImg(BsStoreImg bsStoreImg) {
        bsStoreImgMapper.insert(bsStoreImg);
    }

    @Override
    public void editStoreImg(BsStoreImg bsStoreImg) {
        bsStoreImgMapper.updateByPrimaryKey(bsStoreImg);
    }

    @Override
    public BsStoreImg findStoreImg(Long id) {
        return bsStoreImgMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsStoreImg findStoreImgByStoreId(Long storeId) {
        BsStoreImgExample example = new BsStoreImgExample();
        BsStoreImgExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId);

        List<BsStoreImg> list = bsStoreImgMapper.selectByExample(example);

        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsStoreImg> getBsStoreImgByList(Map<String, Object> map) {
        BsStoreImgExample example = new BsStoreImgExample();
        BsStoreImgExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(map, "storeId"));
        }
        if (MapUtils.getInteger(map, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map, "type"));
        }

        return bsStoreImgMapper.selectByExample(example);
    }

    @Override
    public void insertStoreProduct(BsStoreProduct bsStoreProduct) {
        storeProductMapper.insert(bsStoreProduct);

        // 刷新缓存
        storeProductCategoryService.getStoreCategoryCache(bsStoreProduct.getStoreId(), true);
    }

    @Override
    public void editStoreProduct(BsStoreProduct bsStoreProduct) {
        storeProductMapper.updateByPrimaryKey(bsStoreProduct);

        // 刷新缓存
        storeProductCategoryService.getStoreCategoryCache(bsStoreProduct.getStoreId(), true);
    }

    @Override
    public BsStoreProduct findStoreProduct(Long id) {
        return storeProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsStoreProduct> getStoreProductByList(Map<String, Object> map) {
        BsStoreProductExample example = new BsStoreProductExample();
        BsStoreProductExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(map, "storeId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "name"))) {
            criteria.andNameLike("%" + MapUtils.getString(map, "name") + "%");
        }

        return storeProductMapper.selectByExample(example);
    }

    @Override
    public void insertStoreExtMsg(BsStoreExtMsg bsStoreExtMsg) {
        storeExtMsgMapper.insert(bsStoreExtMsg);
    }

    @Override
    public void editStoreExtMsg(BsStoreExtMsg bsStoreExtMsg) {
        storeExtMsgMapper.updateByPrimaryKey(bsStoreExtMsg);
    }

    @Override
    public BsStoreExtMsg findStoreExtMsg(Long id) {
        return storeExtMsgMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsStoreExtMsg findStoreExtMsgByStoreId(Long storeId) {
        BsStoreExtMsgExample example = new BsStoreExtMsgExample();
        BsStoreExtMsgExample.Criteria criteria = example.createCriteria();

        criteria.andStoreIdEqualTo(storeId).andStatusEqualTo(1);

        List<BsStoreExtMsg> list = storeExtMsgMapper.selectByExample(example);

        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

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