package com.mcwl.resource.service.impl;

import com.mcwl.common.constant.DictConstants;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.Collect;
import com.mcwl.resource.domain.response.ResponseCollect;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.CollectMapper;
import com.mcwl.resource.service.CollectService;
import com.mcwl.system.init.DictInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 收藏实现层
 * @author DaiZibo
 * @date 2025/3/5
 * @apiNote
 */

@Service
public class CollectServiceImpl implements CollectService {


    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private DownloadRecordServiceImpl downloadRecordService;

    @Override
    public R addCollect(Collect collect) {

        Collect collect1 = collectMapper.selectCollect(SecurityUtils.getUserId(),collect.getProductId(),collect.getCollectType());
        collect.setUserId(SecurityUtils.getUserId());
        collect.setCreateTime(new Date());
        if (collect1 == null){
            //执行收藏
            collectMapper.insert(collect);
            return R.ok(1);
        }

        //执行删除/取消收藏
        collectMapper.deleteById(collect1.getId());
        return R.ok(0);

    }

    @Override
    public Collect selectCollectById(Long modelId, Long userIdMax,Integer type) {

        return collectMapper.selectCollectById(modelId,userIdMax,type);
    }

    @Override
    public R selectCollect(PageVo pageVo) {

        //查询个人收藏
        Long userIdMax = SecurityUtils.getUserIdMax();

        if (pageVo.getType() == 999){

            //查询工作流
            List<ResponseCollect> responseCollectList = collectMapper.selectCollectWorkFlow(userIdMax);



            return R.ok(responseCollectList);

        }else {

            //查询模型
            List<ResponseCollect> responseCollectList = collectMapper.selectCollectModel(userIdMax,pageVo.getType());
            //校验是否下载
            for (ResponseCollect collect : responseCollectList) {

                //校验是否加密
                if (collect.getIsEncrypt() == 1){
                    collect.setFileName(collect.getEncryptionFileName());
                }

                collect.setIsDownload(downloadRecordService.selectDownloadByUser(userIdMax,collect.getId(),0));
                //翻译类型
                collect.setType(DictInit.getDictValue(DictConstants.MODE_VERSION_TYPE,collect.getType()));
                if (collect.getTriggerWords() == null){
                    collect.setTriggerWords("-");
                }
            }

            return R.ok(responseCollectList);
        }

    }
}
