package com.ruoyi.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cms.domain.CompanyCardCollection;
import com.ruoyi.cms.domain.vo.CompanyCardVO;
import com.ruoyi.cms.mapper.CompanyCardCollectionMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CompanyCardMapper;
import com.ruoyi.cms.domain.CompanyCard;
import com.ruoyi.cms.service.ICompanyCardService;

/**
 * 公司卡片Service业务层处理
 *
 * @author ${author}
 * @date 2025-02-18
 */
@Service
public class CompanyCardServiceImpl extends ServiceImpl<CompanyCardMapper,CompanyCard> implements ICompanyCardService
{
    @Autowired
    private CompanyCardMapper companyCardMapper;
    @Autowired
    private CompanyCardCollectionMapper companyCardCollectionMapper;
    @Autowired
    private CompanyCardCollectionServiceImpl companyCardCollectionService;
    /**
     * 查询公司卡片
     *
     * @param companyCardId 公司卡片主键
     * @return 公司卡片
     */
    @Override
    public CompanyCard selectCompanyCardByCompanyCardId(Long companyCardId)
    {
        return companyCardMapper.selectById(companyCardId);
    }

    /**
     * 查询公司卡片列表
     *
     * @param companyCard 公司卡片
     * @return 公司卡片
     */
    @Override
    public List<CompanyCard> selectCompanyCardList(CompanyCard companyCard)
    {
        return companyCardMapper.selectCompanyCardList(companyCard);
    }

    /**
     * 新增公司卡片
     *
     * @param companyCard 公司卡片
     * @return 结果
     */
    @Override
    public int insertCompanyCard(CompanyCard companyCard)
    {
        companyCard.setStatus(0);
        return companyCardMapper.insert(companyCard);
    }

    /**
     * 修改公司卡片
     *
     * @param companyCard 公司卡片
     * @return 结果
     */
    @Override
    public int updateCompanyCard(CompanyCard companyCard)
    {
        return companyCardMapper.updateById(companyCard);
    }

    /**
     * 批量删除公司卡片
     *
     * @param companyCardIds 需要删除的公司卡片主键
     * @return 结果
     */
    @Override
    public int deleteCompanyCardByCompanyCardIds(Long[] companyCardIds)
    {
        return companyCardMapper.deleteBatchIds(Arrays.asList(companyCardIds));
    }

    @Override
    public List<CompanyCardVO> cardApp() {
        CompanyCard companyCard = new CompanyCard();
        companyCard.setStatus(1);
        List<CompanyCardVO> companyCards = companyCardMapper.selectCompanyCardListVO(companyCard);
        if(SiteSecurityUtils.isLogin()){
            CompanyCardCollection companyCardCollection = new CompanyCardCollection();
            companyCardCollection.setUserId(SiteSecurityUtils.getUserId());
            List<CompanyCardCollection> companyCardCollections = companyCardCollectionMapper.selectCompanyCardCollectionList(companyCardCollection);
            for (CompanyCardVO item:companyCards){
                for(CompanyCardCollection collection:companyCardCollections){
                    if(Objects.equals(item.getCompanyCardId(), collection.getCompanyCardId())){
                        item.setIsCollection(1);
                    }
                }
            }
        }
        return companyCards;
    }

    @Override
    public void cardCollection(Long companyCardId) {
        CompanyCardCollection companyCardCollection = new CompanyCardCollection();
        companyCardCollection.setCompanyCardId(companyCardId);
        companyCardCollection.setUserId(SiteSecurityUtils.getUserId());
        companyCardCollectionService.save(companyCardCollection);
    }

    @Override
    public void cardCancel(Long companyCardId) {
        CompanyCardCollection companyCardCollection = new CompanyCardCollection();
        companyCardCollection.setCompanyCardId(companyCardId);
        companyCardCollection.setUserId(SiteSecurityUtils.getUserId());
        List<CompanyCardCollection> companyCardCollections = companyCardCollectionMapper.selectList(Wrappers.<CompanyCardCollection>lambdaQuery().eq(CompanyCardCollection::getCompanyCardId, companyCardId).eq(CompanyCardCollection::getUserId, SiteSecurityUtils.getUserId()));
        companyCardCollectionMapper.deleteBatchIds(companyCardCollections.stream().map(CompanyCardCollection::getCompanyCardCollectionId).collect(Collectors.toList()));
    }
}