package com.younike.system.service.impl;

import java.util.List;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

import com.younike.common.utils.SecurityUtils;
import com.younike.system.domain.vo.MerchantCategoryModifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.MerchantCategoryMapper;
import com.younike.system.domain.MerchantCategory;
import com.younike.system.service.IMerchantCategoryService;
import org.springframework.util.CollectionUtils;

import static java.util.stream.Collectors.toList;

/**
 * 店铺服务分类关联Service业务层处理
 *
 * @author elev
 * @date 2022-01-11
 */
@Service
public class MerchantCategoryServiceImpl implements IMerchantCategoryService {
    @Autowired
    private MerchantCategoryMapper merchantCategoryMapper;

    /**
     * 查询店铺服务分类关联
     *
     * @param id 店铺服务分类关联主键
     * @return 店铺服务分类关联
     */
    @Override
    public MerchantCategory selectMerchantCategoryById(Long id) {
        return merchantCategoryMapper.selectMerchantCategoryById(id);
    }

    /**
     * 查询店铺服务分类关联列表
     *
     * @param merchantCategory 店铺服务分类关联
     * @return 店铺服务分类关联
     */
    @Override
    public List<MerchantCategory> selectMerchantCategoryList(MerchantCategory merchantCategory) {
        return merchantCategoryMapper.selectMerchantCategoryList(merchantCategory);
    }

    /**
     * 新增店铺服务分类关联
     *
     * @param merchantCategory 店铺服务分类关联
     * @return 结果
     */
    @Override
    public int insertMerchantCategory(MerchantCategory merchantCategory) {
        merchantCategory.setCreateTime(LocalDateTime.now());
        return merchantCategoryMapper.insertMerchantCategory(merchantCategory);
    }

    /**
     * 修改店铺服务分类关联
     *
     * @param merchantCategory 店铺服务分类关联
     * @return 结果
     */
    @Override
    public int updateMerchantCategory(MerchantCategory merchantCategory) {
        merchantCategory.setUpdateTime(LocalDateTime.now());
        return merchantCategoryMapper.updateMerchantCategory(merchantCategory);
    }

    /**
     * 批量删除店铺服务分类关联
     *
     * @param ids 需要删除的店铺服务分类关联主键
     * @return 结果
     */
    @Override
    public int deleteMerchantCategoryByIds(Long[] ids) {
        return merchantCategoryMapper.deleteMerchantCategoryByIds(ids);
    }

    /**
     * 删除店铺服务分类关联信息
     *
     * @param id 店铺服务分类关联主键
     * @return 结果
     */
    @Override
    public int deleteMerchantCategoryById(Long id)
    {
        return merchantCategoryMapper.deleteMerchantCategoryById(id);
    }

    @Override
    public boolean merchantCategoryModify(MerchantCategoryModifyVO merchantCategoryModifyVO) {
        merchantCategoryMapper.removeMerchantCategoryByMerchantId(merchantCategoryModifyVO.getMerchantId());
        List<Long> categoryIds = merchantCategoryModifyVO.getCategoryIds();
        if (CollectionUtils.isEmpty(categoryIds)){
            return true;
        }
        List<MerchantCategory> merchantCategoryList = categoryIds.stream().map(categoryId -> {
            MerchantCategory merchantCategory = new MerchantCategory();
            merchantCategory.setMerchantId(merchantCategoryModifyVO.getMerchantId());
            merchantCategory.setCategoryId(categoryId);
            merchantCategory.setCreateBy(SecurityUtils.getUserId().toString());
            return merchantCategory;
        }).collect(toList());
        return merchantCategoryMapper.insertBatch(merchantCategoryList) == merchantCategoryList.size();
    }

    @Override
    public List<MerchantCategory> merchantCategoryList(Long merchantId) {
        return merchantCategoryMapper.merchantCategoryList(merchantId);
    }
}