package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.module.person.model.PersonEntity;
import com.mortals.xhx.module.person.model.PersonQuery;
import com.mortals.xhx.module.person.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 同步注册用户的证件照片。
 */
@Slf4j
@Service("SyncRegisterUserPicTask")
public class SyncRegisterUserPicTaskImpl implements ITaskExcuteService {

    @Autowired
    private PersonService personService;
    @Autowired
    private UploadService uploadService;

    @Value("${php.host:''}")
    private String host;


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步注册用户照片任务");
        syncRegisterUsersPhotos();
        log.info("同步注册用户任务照片完成");

    }


    private void syncRegisterUsersPhotos() {
        String url = host.endsWith("/") ? host : host + "/";
        List<PersonEntity> collect = personService.find(new PersonQuery()).stream()
                .filter(item -> ObjectUtils.isEmpty(item.getPhoto()))
                .filter(item -> !ObjectUtils.isEmpty(item.getSourcePhotoUri())).collect(Collectors.toList());
        for (PersonEntity personEntity : collect) {
            //下载图片到本地保存并更新
            String downUrl = url + personEntity.getSourcePhotoUri();
            //String downUrl = "http://192.168.0.98:11078/file/fileupload/1679215657433.jpg";
            log.info("downUrl:{}",downUrl);
            byte[] bytes = HttpUtil.downloadBytes(downUrl);
            if(bytes.length>0){
                InputStream inputStream = new ByteArrayInputStream(bytes);
                MultipartFile file = ServletUtils.getMultipartFile(inputStream, "photo.jpg");
                String filePath = uploadService.saveFileUpload(file, "file/fileupload",null);
                personEntity.setPhoto(filePath);
                personService.update(personEntity);
            }
        }
    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }

    public static void main(String[] args) {


        System.out.println(DateUtil.parse("19951071", "yyyyMMdd"));
    }


}
