package org.jeecg.modules.orderplatform.service.impl;

import cn.com.ctop.common.module.utils.CosUtils;
import cn.com.ctop.common.module.vo.ResFileDTO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.orderplatform.entity.PlatformScript;
import org.jeecg.modules.orderplatform.mapper.PlatformScriptMapper;
import org.jeecg.modules.orderplatform.service.IPlatformScriptService;
import org.jeecg.modules.wps.service.IWpsFileWatermarkService;
import org.jeecg.modules.wps.service.IWpsUserAclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 下单平台关联脚本
 */
@Service
public class PlatformScriptServiceImpl extends ServiceImpl<PlatformScriptMapper, PlatformScript> implements IPlatformScriptService {

    @Autowired
    private IWpsUserAclService wpsUserAclService;

    @Autowired
    private IWpsFileWatermarkService wpsFileWatermarkService;

    @Autowired
    private PlatformScriptMapper platformScriptMapper;

    @Override
    public PlatformScript saveScript(MultipartFile file,Long projectId) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //上传文件到cos
        ResFileDTO resFileDTO = CosUtils.uploadMultipartFile(file,"script-lib/wps/");
        //构造下单制脚本库参数
        PlatformScript script=new PlatformScript(resFileDTO.getFileName(),1,((int) resFileDTO.getFileSize()),
                user.getId(),user.getId(),System.currentTimeMillis(),System.currentTimeMillis(), resFileDTO.getFileUrl(),projectId);
        this.save(script);
        // 处理权限
        wpsUserAclService.saveUserFileAcl(user.getId(),script.getId());
        // 处理水印
        wpsFileWatermarkService.saveWatermark(script.getId());

        return script;

    }

    @Override
    public PageInfo<JSONObject> page(Long projectId, String name, String creator, int pageNo, int  pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<JSONObject> result = platformScriptMapper.pageQuery(projectId,name,creator);
        return new PageInfo<>(result);
    }
}
