package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzShopMapper;
import org.linlinjava.jz.db.domain.JzShop;
import org.linlinjava.jz.db.domain.JzShopExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 *  商城 购买门店维护处理类
 */
@Service
public class JzShopService {

    @Resource
    private JzShopMapper shopMapper;

    public List<JzShop> querySelective(String title, int city, Integer page, Integer limit, String sort, String order) {
        JzShopExample example = new JzShopExample();
        JzShopExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andShopNameLike("%" + title + "%");
        }
        if (city>0) {
            criteria.andShopCityEqualTo(city);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return shopMapper.selectByExample(example);
    }

    public int add(JzShop shop) {
        shop.setCreateTime(LocalDateTime.now());
        shop.setUpdateTime(LocalDateTime.now());
        return shopMapper.insertSelective(shop);
    }

    public List<JzShop> queryByCityId(int cityId)
    {
        JzShopExample example = new JzShopExample();
        example.or().andShopCityEqualTo(cityId).andDeletedEqualTo(false);
        return shopMapper.selectByExample(example);
    }
    public int deleteById(JzShop shop) {
        shop.setUpdateTime(LocalDateTime.now());
        shop.setDeleted(true);
       return   shopMapper.updateByPrimaryKeySelective(shop);
    }

    public int updateById(JzShop shop) {
        shop.setUpdateTime(LocalDateTime.now());
        return shopMapper.updateByPrimaryKeySelective(shop);
    }

    /**
     * 将对应id，转化为盒马门店标号
     * @param shops
     * @return
     */
    public List<String> queryById(List<Integer> shops){
        JzShopExample example = new JzShopExample();
        example.or().andIdIn(shops).andDeletedEqualTo(false);

        List<JzShop> jzShops=shopMapper.selectByExample(example);

        List<String> list= Lists.newArrayList();
        for(JzShop shop:jzShops){
            list.add(shop.getShopNo());
        }
        return list;
    }

    public JzShop findById(Integer id) {
        return shopMapper.selectByPrimaryKey(id);
    }

}
