package com.mcwl.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.DownloadRecord;
import com.mcwl.resource.domain.request.RequestDownload;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.*;
import com.mcwl.resource.service.DownloadRecordService;
import com.mcwl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 下载记录实现层
 *
 * @author DaiZibo
 * @date 2025/3/6
 * @apiNote
 */

@Service
public class DownloadRecordServiceImpl implements DownloadRecordService {

    @Autowired
    private DownloadRecordMapper downloadRecordMapper;

    @Autowired
    private ModelVersionMapper modelVersionMapper;

    @Autowired
    private WorkFlowVersionMapper workFlowVersionMapper;

    @Autowired
    private WorkFlowMapper workFlowMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public R addDownloadRecord(DownloadRecord downloadRecord) {

        //获取下载人
        downloadRecord.setUserId(SecurityUtils.getUserId());
        downloadRecord.setCreateTime(new Date());

//        //获取作品信息
//        if (downloadRecord.getProductType() == 0){
//
//            //查询文件/名称信息
//            ModelVersion modelVersion = modelVersionMapper.selectById(downloadRecord.getProductId());
//            downloadRecord.setFileName(modelVersion.getFileName());
//            downloadRecord.setFileSize(modelVersion.getFileSize());
//            downloadRecord.setModelVersionType(modelVersion.getModelVersionType()+"");
//
//            //根据版本查找封面信息
//            ModelProduct modelProduct = modelMapper.selectById(modelVersion.getModelId());
//            downloadRecord.setCover(modelProduct.getSurfaceUrl());
//            downloadRecord.setProductName(modelProduct.getModelName());
//
//            //查询作者信息
//            SysUser sysUser = sysUserMapper.selectUserById(modelProduct.getUserId());
//            downloadRecord.setAuthorName(sysUser.getNickName());
//
//        }else {
//
//            //查询文件/名称信息
//            WorkFlowVersion workFlowVersion = workFlowVersionMapper.selectById(downloadRecord.getProductId());
//            downloadRecord.setFileName(workFlowVersion.getFileName());
//            downloadRecord.setFileSize(workFlowVersion.getFileSize());
//
//
//            //根据版本查找封面信息
//            WorkFlow workFlow = workFlowMapper.selectById(workFlowVersion.getWorkFlowId());
//            downloadRecord.setCover(workFlow.getCoverPath());
//            downloadRecord.setProductName(workFlow.getWorkflowName());
//
//
//            //查询作者信息
//            SysUser sysUser = sysUserMapper.selectUserById(workFlow.getUserId());
//            downloadRecord.setAuthorName(sysUser.getNickName());
//
//        }

        downloadRecordMapper.insert(downloadRecord);

        return R.ok();
    }

    @Override
    public R selectDownloadRecord(PageVo pageVo) {

        //分页查询数据
        Page<DownloadRecord> page = new Page<>();
        page.setSize(pageVo.getPageSize());
        page.setPages(pageVo.getPageNumber());

        LambdaQueryWrapper<DownloadRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DownloadRecord::getUserId,SecurityUtils.getUserId());
        wrapper.eq(pageVo.getType() != null,DownloadRecord::getProductType,pageVo.getType());

        Page<DownloadRecord> downloadRecordPage = downloadRecordMapper.selectPage(page, wrapper);

        return R.ok(downloadRecordPage);
    }

    @Override
    public R updateDownloadRecord(DownloadRecord downloadRecord) {

        downloadRecordMapper.updateDownloadRecord(downloadRecord);

        return R.ok();
    }

    @Override
    public R deleteDownloadRecord(String ids) {

        if (StringUtils.isNotBlank(ids)) {
            List<Long> idList = Arrays.stream(ids.split(","))
                    .filter(StringUtils::isNotBlank)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());

            if (!idList.isEmpty()) {
                downloadRecordMapper.deleteBatchIds(idList);
            }
            return R.ok();
        }

        return R.fail(HttpStatus.SHOW_ERROR_MSG,"请选择要删除的数据");
    }

    @Override
    public Integer selectDownloadByUser(Long userIdMax, Long id, int i) {

        DownloadRecord downloadRecord = downloadRecordMapper.selectDownloadByUser(userIdMax,id,i);
        if (downloadRecord != null){
            return 1;
        }

        return 0;
    }

    @Override
    public R selectByFileName(RequestDownload requestDownload) {

        LambdaQueryWrapper<DownloadRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DownloadRecord::getUserId,SecurityUtils.getUserId());
        if (requestDownload.getType() == 999){
            //查询工作流
            wrapper.eq(DownloadRecord::getDownloadType,1);
            wrapper.in(DownloadRecord::getFileName,requestDownload.getFileNameList());
        }else {
            wrapper.eq(DownloadRecord::getDownloadType,0);
            wrapper.eq(DownloadRecord::getProductType,requestDownload.getType());
            wrapper.in(DownloadRecord::getFileName,requestDownload.getFileNameList());
        }

        List<DownloadRecord> downloadRecords = downloadRecordMapper.selectList(wrapper);
//        if (requestDownload.getType() != 999){
//            //翻译底模
//            for (DownloadRecord downloadRecord : downloadRecords) {
//                downloadRecord.setModelVersionType(DictInit.getDictValue(DictConstants.MODE_VERSION_TYPE,downloadRecord.getModelVersionType()));
//            }
//        }

        return R.ok(downloadRecords);
    }
}
