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

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.ai.entity.PreReviewRecordsDetail;
import org.jeecg.modules.ai.entity.PreReviewRecordsDetailTemplate;
import org.jeecg.modules.ai.mapper.PreReviewRecordsDetailMapper;
import org.jeecg.modules.ai.qo.PreReviewRecordsDetailQuery;
import org.jeecg.modules.ai.service.IPreReviewRecordsDetailService;
import org.jeecg.modules.ai.service.IPreReviewRecordsDetailTemplateService;
import org.jeecg.modules.ai.vo.PreReviewRecordsDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 办事预审记录详情
 * @Author: jeecg-boot
 * @Date:   2023-11-25
 * @Version: V1.0
 */
@Service
public class PreReviewRecordsDetailServiceImpl extends ServiceImpl<PreReviewRecordsDetailMapper, PreReviewRecordsDetail> implements IPreReviewRecordsDetailService {

	@Resource
	private PreReviewRecordsDetailMapper preReviewRecordsDetailMapper;
	@Value(value = "${jeecg.path.upload}")
	private String uploadpath;
	@Autowired
	private IPreReviewRecordsDetailTemplateService iPreReviewRecordsDetailTemplateService;
	
	/**
	 * 根据参数关联查询事项信息
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public IPage<PreReviewRecordsDetailVo> getPreReviewRecordsDetailList(Page page, PreReviewRecordsDetailQuery query){
		return preReviewRecordsDetailMapper.getPreReviewRecordsDetailList(page, query);
	}
	
	/**
	 * 获取已提交的资料信息
	 * @param eventCode
	 * @param userId
	 * @return
	 */
	@Override
	public List<PreReviewRecordsDetailVo> getSubmittedMaterialList(String eventCode,String userId){
		//获取已提交事项
		PreReviewRecordsDetailQuery query = new PreReviewRecordsDetailQuery();
		query.setUserId(userId);
		query.setEventCode(eventCode);
		query.setReviewResult("通过");
		List<PreReviewRecordsDetailVo> list = preReviewRecordsDetailMapper.getPreReviewRecordsDetailList(query);
		for(PreReviewRecordsDetailVo vo:list) {
			LambdaQueryWrapper<PreReviewRecordsDetailTemplate> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(PreReviewRecordsDetailTemplate::getRecordDetailId, vo.getId());
			queryWrapper.eq(PreReviewRecordsDetailTemplate::getMaterialCode, vo.getMaterialCode());
			List<PreReviewRecordsDetailTemplate> listPreReviewRecordsDetailTemplate = iPreReviewRecordsDetailTemplateService.list(queryWrapper);
			vo.setListPreReviewRecordsDetailTemplate(listPreReviewRecordsDetailTemplate);
		}
		return list;
	}
	
	/**
     * 本地文件上传
     * @param mf 文件
     * @param bizPath  自定义路径
     * @return
     */
	@Override
    public String uploadLocal(MultipartFile mf,String bizPath){
        try {
        	String ctxPath = uploadpath;
            String fileName = null;
            File file = new File(ctxPath + File.separator + bizPath + File.separator );
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String orgName = mf.getOriginalFilename();// 获取文件名
            orgName = CommonUtils.getFileName(orgName);
            if(orgName.indexOf(".")!=-1){
                fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
            }else{
                fileName = orgName+ "_" + System.currentTimeMillis();
            }
            String savePath = file.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);
            String dbpath = null;
            if(oConvertUtils.isNotEmpty(bizPath)){
                dbpath = bizPath + File.separator + fileName;
            }else{
                dbpath = fileName;
            }
            if (dbpath.contains("\\")) {
                dbpath = dbpath.replace("\\", "/");
            }
            return dbpath;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }
	
	public String getOcrResutl() {
		
		return null;
	}
}
