package cn.com.ty.lift.ud.userCollect.service.impl;

import cn.com.ty.lift.ud.userCollect.controller.query.UserCollectQuery;
import cn.com.ty.lift.ud.userCollect.mapper.entity.*;
import cn.com.ty.lift.ud.userCollect.service.IUserCollectService;
import cn.com.ty.lift.ud.userCollect.mapper.UserCollectMapper;
import cn.com.ty.lift.ud.userCollect.mapper.entity.*;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-28
 */
@Service
@Transactional
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollectEntity>
        implements IUserCollectService {


    @Autowired
    private UserCollectMapper userCollectMapper;


    /**
     * 查看是否被收藏过
     *
     * @param userCollect
     */
    @Override
    public UserCollectEntity isCollect(UserCollectDto userCollect) {
        UserCollectEntity collect = userCollectMapper.isCollect(userCollect.getUserId(),
                userCollect.getCollectType(), userCollect.getInfoId());
        return collect;
    }

	@Override
	public void deleteByUserAndDataId(Long userId, Long dataId, int type) {
		userCollectMapper.deleteByUserAndDataId(userId,dataId,type);
	}

    /**
     * 根据数据ID 和 类型删除
     * @param dataId
     * @param type
     */
    @Override
    public void deleteByTypeAndDataId(Long dataId, Integer type) {
        userCollectMapper.deleteByTypeAndDataId(dataId,type);
    }

    /**
     * 查询收藏的问题
     *
     * @param query
     * @return
     */
    @Override
    public RestResponse selectQuestionById(UserCollectQuery query) {
        IPage page = query.page();
        List<UserCollectQuestionVo> list = userCollectMapper.selectQuestionById(query.condition(), query,query.getUId());
        //清除为null的数据
//        List<UserCollectQuestionVo> questionList = new ArrayList<>();
//        for(UserCollectQuestionVo uqVo:list){
//            if(null != uqVo){
//                questionList.add(uqVo);
//            }
//        }
//        page.setRecords(questionList);
        page.setRecords(list);
        int total = userCollectMapper.countQuestionById(query.condition(),query.getUId());
        page.setTotal(total);
        return RestResponse.success(page, "查询成功");
    }

    /**
     * 查询收藏的视频
     *
     * @param query
     * @return
     */
    @Override
    public RestResponse selectSchoolVideoById(UserCollectQuery query) {
        IPage page = query.page();
        List<UserCollectSchoolVideoVo> list = userCollectMapper.selectSchoolVideoById(query.condition(), query,query.getUId());
        page.setRecords(list);
        int total = userCollectMapper.countSchoolVideoById(query.condition(),query.getUId());
        page.setTotal(total);
        return RestResponse.success(page, "查询成功");
    }

    /**
     * 查询收藏的职位
     *
     * @param query
     * @return
     */
    @Override
    public RestResponse selectRecruitmentInfoById(UserCollectQuery query) {
        IPage page = query.page();
        List<UserCollectRecruitmentInfoVo> list = userCollectMapper.selectRecruitmentInfoById(query.condition(), query,query.getUId());
        page.setRecords(list);
        int total = userCollectMapper.countRecruitmentInfoById(query.condition(),query.getUId());
        page.setTotal(total);
        return RestResponse.success(page, "查询成功");
    }

    /**
     * 查询收藏的商品
     *
     * @param query
     * @return
     */
    @Override
    public RestResponse selectShoppingById(UserCollectQuery query) {
        IPage page = query.page();
        List<UserCollectShoppingVo> list = userCollectMapper.selectShoppingById(query.condition(), query,query.getUId());
        page.setRecords(list);
        int total = userCollectMapper.countShoppingById(query.condition(),query.getUId());
        page.setTotal(total);
        return RestResponse.success(page, "查询成功");
    }

    /**
     * 查询收藏的资料
     * @param query
     * @return
     */
    @Override
    public RestResponse selectAttachmentFileById(UserCollectQuery query) {
        List<UserCollectAttachmentFileVo> list = userCollectMapper.selectAttachmentFileById(query);
        int count = userCollectMapper.countAttachmentFileById(query);
        IPage page = query.page();
        page.setRecords(list);
        page.setSize(count);

        return RestResponse.success(page);
    }

    /**
     * 删除信息
     * @param userCollectEntity
     */
    @Override
    public void noCollect(UserCollectEntity userCollectEntity) {
        userCollectMapper.noCollect(userCollectEntity);
    }
}
