package com.chushang.inspection.project.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.web.Result;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.mapper.TemplateMapper;
import com.chushang.inspection.project.po.Template;
import com.chushang.inspection.project.service.TbTemplateService;
import com.chushang.oss.entity.dto.UploadBytesDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.feign.RemoteOssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @auther: zhao
 * @date: 2024/7/2 11:05
 */
@Slf4j
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TbTemplateService {
    @Resource
    RemoteOssService remoteOssService;

    @Override
    public PageResult pageList(Template template) {
        CommonParam commonParam = CommonParam.buildPageRequest();
        LambdaQueryWrapper<Template> templateSql = WrapperUtils.builder(template, commonParam);
        Page<Template> page = this.page(new Page<>(commonParam.getPage(), commonParam.getLimit()), templateSql);
        return new PageResult(page);
    }

    @Override
    public Long saveTemplate(Template template) {
        // 模板别名
        String templateAlias = template.getTemplateAlias();

        long count = count(new LambdaQueryWrapper<Template>()
                .eq(Template::getTemplateAlias, templateAlias)
        );
        AssertUtil.invalidate(count > 0, "模板别名重复");
        template.setTemplateId(null);
        save(template);
        return template.getTemplateId();
    }

    @Override
    public Long updateTemplate(Template template) {
        // 模板别名
        String templateAlias = template.getTemplateAlias();
        Long templateId = template.getTemplateId();

        long count = count(new LambdaQueryWrapper<Template>()
                .eq(Template::getTemplateAlias, templateAlias)
                .ne(Template::getTaskId, templateId)
        );
        AssertUtil.invalidate(count > 0, "模板别名重复");
        updateById(template);
        return templateId;
    }

    @Override
    public FileSourceVo uploadFile(MultipartFile file, String templateType) throws IOException {
        // 上传模板信息
        Result<List<FileSourceVo>> listResult =
                remoteOssService.uploadBytes(
                        UploadBytesDTO.builder()
                                .bytes(file.getBytes())
                                .build(),
                        file.getOriginalFilename(),
                        false, null, templateType, SecurityConstants.INNER);
        if (listResult.isSuccess() && CollectionUtil.isNotEmpty(listResult.getData())){
            FileSourceVo fileSourceVo = listResult.getData().get(0);
            Template template = new Template();
            template.setTemplateFid(fileSourceVo.getFid());
            template.setTemplateUrl(fileSourceVo.getFilePath());
            return listResult.getData().get(0);
        }
        throw new ResultException("上传失败");
    }
}
