package com.yh.saas.plugin.news.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.winsea.svc.base.base.constant.SystemConstant;
import com.winsea.svc.base.base.entity.CommonParameterCompany;
import com.winsea.svc.base.base.service.ICommonParameterCompanyService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.news.entity.AnnoReadUserLike;
import com.yh.saas.plugin.news.mapper.NewsAnnoReadUserLikeMapper;
import com.yh.saas.plugin.news.service.INewsAnnoReadUserLikeService;

/**
 * <p>
 * 职员新闻喜好类型 服务实现类
 * </p>
 *
 * @author Wangs
 * @since 2020-03-16
 */
@Service
public class NewsAnnoReadUserLikeServiceImpl extends ServiceImpl<NewsAnnoReadUserLikeMapper, AnnoReadUserLike> implements INewsAnnoReadUserLikeService {

    @SofaReference
    private ICommonParameterCompanyService parameterCompanyService;


    /**
     * 查询当前职员是否设置过新闻喜好类型
     *
     * @param staffId
     * @return
     */
    @Override
    public Boolean getReadUserLike(String staffId) {
        AnnoReadUserLike info = this.selectOne(new EntityWrapper<AnnoReadUserLike>().eq(AnnoReadUserLike.QueryFiles.STAFF_ID, staffId));
        return info != null;
    }

    /**
     * 创建职员喜好新闻类型 （第一次登陆用）
     *
     * @param idArr
     */
    @Override
    public void createLikeList(String idArr) {
        List<AnnoReadUserLike> userLikeList = new ArrayList<>();
        AnnoReadUserLike userLike;
        List<String> idList = Arrays.asList(idArr.split(","));
        CommonParameterCompany selectInfo;
        if (CollectionUtils.isNotEmpty(idList)) {
            for (String id : idList) {
                if (StringUtils.isNotBlank(id)) {
                    userLike = new AnnoReadUserLike();
                    userLike.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                    userLike.setTenantId(AuthSecurityUtils.getCurrentUserInfo().getTenantId());
                    userLike.setStaffId(AuthSecurityUtils.getCurrentUserInfo().getUserId());
                    userLike.setTypeId(id);
                    selectInfo = parameterCompanyService.selectOne(new EntityWrapper<CommonParameterCompany>()
                            .eq(CommonParameterCompany.QueryFields.PARAMETER_ID, id));
                    userLike.setTypeName(selectInfo.getParameterValue());
                    userLikeList.add(userLike);
                }
            }
        }
        this.insertOrUpdateBatch(userLikeList);
    }

    /**
     * 查询当前职员的选中新闻类型
     *
     * @param staffId 职员id
     * @return
     */
    @Override
    public List<AnnoReadUserLike> getReadUserLikeList(String staffId) {

        List<AnnoReadUserLike> userLikeList = this.getBasicReadUserLike(staffId);
        AnnoReadUserLike selectInfo;
        if (CollectionUtils.isNotEmpty(userLikeList)) {
            for (AnnoReadUserLike info : userLikeList) {
                selectInfo = this.selectOne(new EntityWrapper<AnnoReadUserLike>().eq(AnnoReadUserLike.QueryFiles.STAFF_ID, staffId)
                        .eq(AnnoReadUserLike.QueryFiles.TYPE_ID, info.getTypeId()));
                if (selectInfo != null) {
                    info.setChooseFlag(true);
                    info.setId(selectInfo.getId());
                }
            }
        }
        return userLikeList;
    }

    private List<AnnoReadUserLike> getBasicReadUserLike(String staffId) {
        CommonParameterCompany parameterCompany = new CommonParameterCompany();
        parameterCompany.setCompId(SystemConstant.SYSTEM_COMP_ID);
        parameterCompany.setConstCode("NEWS_TYPE");
        // 查询新闻类型
        List<CommonParameterCompany> parameterList = parameterCompanyService.queryCommonParameterTenant(parameterCompany);
        if (CollectionUtils.isEmpty(parameterList)) {
            return null;
        }
        List<AnnoReadUserLike> likeList = new ArrayList<>();
        AnnoReadUserLike info;
        for (CommonParameterCompany parameter : parameterList) {
            info = new AnnoReadUserLike();
            info.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
            info.setTenantId(AuthSecurityUtils.getCurrentUserInfo().getTenantId());
            info.setTypeId(parameter.getParameterId());
            info.setTypeName(parameter.getParameterValue());
            info.setStaffId(staffId);
            info.setChooseFlag(false);
            likeList.add(info);
        }
        return likeList;
    }

    /**
     * 更新职员选中新闻类型
     *
     * @param userLikeList
     */
    @Override
    public void updateLikeList(List<AnnoReadUserLike> userLikeList) {
        if (CollectionUtils.isEmpty(userLikeList)) {
            return;
        }
        // 查询之前选中的
        List<AnnoReadUserLike> oldLikeList = this.selectList(new EntityWrapper<AnnoReadUserLike>().eq(AnnoReadUserLike.QueryFiles.STAFF_ID,
                AuthSecurityUtils.getCurrentUserInfo().getUserId()));
        this.insertOrUpdateBatch(userLikeList);// 更新这次新选择的
        // 取得新选中的id
        List<String> newIdList = userLikeList.stream().filter(e -> StringUtils.isNotBlank(e.getId())).map(AnnoReadUserLike::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(oldLikeList)) {
            List<String> oleIdList = oldLikeList.stream().map(AnnoReadUserLike::getId).collect(Collectors.toList());
            // 取到本次去掉的类型
            oleIdList.removeAll(newIdList);
            if (CollectionUtils.isNotEmpty(oleIdList)) {
                this.deleteBatchIds(oleIdList);
            }
        }
    }
}
