package com.mcwl.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.DictConstants;
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.Collect;
import com.mcwl.resource.domain.ModelProduct;
import com.mcwl.resource.domain.ResponseModelPurchaseRecord;
import com.mcwl.resource.domain.ModelVersion;
import com.mcwl.resource.mapper.ModelMapper;
import com.mcwl.resource.mapper.ModelVersionMapper;
import com.mcwl.resource.service.ModelVersionService;
import com.mcwl.system.init.DictInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**模型版本 业务实现层
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.resource.service.impl
 * @Filename：ModelVersionServiceImpl
 * @Description TODO
 * @Date：2025/1/9 16:53
 */
@Service
public class ModelVersionServiceImpl  extends ServiceImpl< ModelVersionMapper,ModelVersion> implements ModelVersionService {

    @Autowired
    private ModelVersionService modelVersionService;

    @Autowired
    private CollectServiceImpl collectService;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ModelVersionMapper modelVersionMapper;


    @Override
    public List<ModelVersion> selectLogininforList(ModelVersion modelVersion) {

        // 创建一个 LambdaQueryWrapper 实例
        LambdaQueryWrapper<ModelVersion> queryWrapper = new LambdaQueryWrapper<>();

        // 使用非空检查来添加查询条件
        if (StringUtils.isNotNull(modelVersion.getIsExclusiveModel())) {
            queryWrapper.eq(ModelVersion::getIsExclusiveModel, modelVersion.getIsExclusiveModel());
        }


        if (StringUtils.isNotNull(modelVersion.getVersionName())) {
            queryWrapper.eq(ModelVersion::getVersionName, modelVersion.getVersionName());
        }

        return modelVersionMapper.selectList(queryWrapper);
    }

    @Override
    public R<List<ModelVersion>> selectByModelId(Long modelId) {

        //根据模型ID查询所有的版本信息
        LambdaQueryWrapper<ModelVersion> modelVersionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getModelId,modelId);
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getDelFlag,0);

        List<ModelVersion> modelVersions = modelVersionMapper.selectList(modelVersionLambdaQueryWrapper);
        for (ModelVersion modelVersion : modelVersions) {

            ArrayList<String> arrayList = new ArrayList<>();

            //获取
            String[] split = modelVersion.getHigh().split(",");

            for (String s : split) {
                arrayList.add(DictInit.getDictValue(DictConstants.DICT_TYPE_MODEL_VERSION_HIGH,s));
            }

            modelVersion.setHighList(arrayList);
        }
        return R.ok(modelVersions);
    }

    @Override
    public List<ModelVersion> finbyid(Long id) {

        LambdaQueryWrapper<ModelVersion> modelVersionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getModelId,id);
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getDelFlag,0);

        List<ModelVersion> modelVersions = baseMapper.selectList(modelVersionLambdaQueryWrapper);

        Long userIdMax = SecurityUtils.getUserIdMax();
        if (userIdMax != 0){

            //擦掉敏感数据
            for (ModelVersion modelVersion : modelVersions) {
                modelVersion.setObjectKey("");
                modelVersion.setEncryptionObjectKey("");
                modelVersion.setFilePath("");
                modelVersion.setEncryptionFilePath("");
                modelVersion.setKeyRate("");

                //模型字典 0 工作流字典 1
                Integer type = 0;
                //校验是否收藏
                Collect collect = collectService.selectCollectById(modelVersion.getId(),userIdMax,type);
                if (collect != null ){
                    modelVersion.setIsCollect(1);
                }else {
                    modelVersion.setIsCollect(0);
                }
            }

        }


        return modelVersions;
    }

    @Override
    public R modelFileDownload(Long id) {

        ModelVersion modelVersion = modelVersionMapper.selectById(id);

        if (modelVersion == null){
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"文件为空");
        }

        if (modelVersion.getAllowDownloadImage().equals(0)){
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"此文件不可下载");
        }

        //查询主表信息
        ModelProduct modelProduct = modelMapper.selectById(modelVersion.getModelId());
        if (modelProduct.getIsFree() == 0){
            //校验是否收费
            ResponseModelPurchaseRecord modelPurchaseRecord = modelMapper.selectIsBuy(modelProduct.getId(),SecurityUtils.getUserId());
            if (StringUtils.isNull(modelPurchaseRecord)){
                //校验是否是自己的
                if (!SecurityUtils.getUserId().equals(modelProduct.getUserId())){
                    return R.fail(HttpStatus.SHOW_ERROR_MSG,"请先购买");
                }
            }
        }

        if (modelVersion.getIsEncrypt() == 1){
            if (StringUtils.isEmpty(modelVersion.getEncryptionFilePath())){

                return R.fail(HttpStatus.SHOW_ERROR_MSG,"该模型加密未完成,请稍后再试");
            }

            updateModel(modelProduct);
            return R.ok(modelVersion.getEncryptionFilePath(),modelVersion.getEncryptionFileName());
        }
        updateModel(modelProduct);
        return R.ok(modelVersion.getFilePath(),modelVersion.getFileName());
    }

    /**
     * 修改下载次数
     * @param modelProduct
     */
    private void updateModel(ModelProduct modelProduct){

        modelProduct.setNumbers(modelProduct.getNumbers()+1);
        modelMapper.updateById(modelProduct);
    }
}
