/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.maintain.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.UserClent;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.DispatchEnum;
import com.tofly.gpsboot.common.system.DispatchStatusEnum;
import com.tofly.gpsboot.common.system.FileTableEnum;
import com.tofly.gpsboot.mapper.maintain.FloodSeasonWMapper;
import com.tofly.gpsboot.service.comm.IFileInfoService;
import com.tofly.gpsboot.service.maintain.DispatchService;
import com.tofly.gpsboot.service.maintain.FloodSeasonWService;
import com.tofly.gpsboot.util.ToflyJsonUtil;
import com.tofly.pojo.User;
import com.tofly.pojo.maintain.Dispatch;
import com.tofly.pojo.maintain.FloodSeasonW;
import com.tofly.pojo.maintain.vo.DispatchVo;
import com.tofly.pojo.maintain.vo.FloodSeasonWVo;
import com.tofly.pojo.maintain.vo.UserViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author wangzhun
 * @date Wed May 11 00:00:00 CST 2022
 */
@Service("floodSeasonWService")
public class FloodSeasonWServiceImpl extends ServiceImpl<FloodSeasonWMapper, FloodSeasonW>
        implements FloodSeasonWService {
    @Autowired
    private UserClent userClent;
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IFileInfoService iFileInfoService;
     @Value("${xrty.ftp.eventMange}")
    private String eventMangeFilePath;
     @Autowired
     private DispatchService dispatchService;

  /*  @Override
    public List<FloodSeasonWVo> viewConversion(List<FloodSeasonW> infoList) {
        List<FloodSeasonWVo> infoVoList = new ArrayList<>();
        infoList.forEach(info ->{

            infoVoList.add(entityToVo(info));
        });
        return infoVoList;
    }*/

    @Override
    public FloodSeasonWVo entityToVo(FloodSeasonW floodSeasonW) {
        FloodSeasonWVo floodSeasonWVo = JSONObject.parseObject(JSON.toJSONString(floodSeasonW),FloodSeasonWVo.class);

        if (!StringUtils.isEmpty(floodSeasonW.getUploadFileids())) {
            floodSeasonWVo.setFilePathList(iFileInfoService.getFilePathList(Arrays.stream(floodSeasonW.getUploadFileids().split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList())));
        }
        if(floodSeasonW.getCreateUserid()!=null){
            User result = userClent.getById(floodSeasonW.getCreateUserid().longValue()).getResult();
            Optional.ofNullable(result).orElse(new User());
            UserViewVo createUserDetail = new UserViewVo();
            createUserDetail.setUserName(result.getUsername());
            createUserDetail.setRealName(result.getRealName());
            createUserDetail.setPhone(result.getPhone());
            createUserDetail.setWorknumber(result.getWorknumber());
            floodSeasonWVo.setCreateUserDetail(createUserDetail);
         }


        Dispatch one = dispatchService.getOne(Wrappers.<Dispatch>lambdaQuery()
                .eq(Dispatch::getType, DispatchEnum.FLOOD_SEASON.getCode()).eq(Dispatch::getSourceId, floodSeasonW.getId()));
        if(null !=one){
            DispatchVo dispatchVo = ToflyJsonUtil.fromObjectToEntity(one, DispatchVo.class);
            if(null != dispatchVo.getResolveFileIds()){
                dispatchVo.setFilePathList(iFileInfoService.getFilePathList(Arrays.stream(one.getResolveFileIds().split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                        .collect(Collectors.toList())));
            }


            floodSeasonWVo.setDispatchVo(dispatchVo);
        }
        return floodSeasonWVo;
    }

    @Override
    public FloodSeasonWVo saveWithFile(FloodSeasonW floodSeasonW, List<MultipartFile> fileList) {
        StringBuilder fileIds=null;
        try {
            if (null !=fileList && !fileList.isEmpty()) {
                fileIds = fileInfoService.saveFileList(fileList, eventMangeFilePath, FileTableEnum.TF_YWPN_EVENT_MANGE.getTableName());
                floodSeasonW.setUploadFileids(fileIds.toString());
            }
        }catch (Exception e){
            throw new ToflyDeniedException(e.getMessage() + ":文件上传失败");
        }
        floodSeasonW.setStatus(DispatchStatusEnum.UNDISPATCH.getCode());

        this.save(floodSeasonW);
        FloodSeasonWVo floodSeasonWVo1 = ToflyJsonUtil.fromObjectToEntity(floodSeasonW, FloodSeasonWVo.class);
        return  floodSeasonWVo1;
    }

    @Override
    public FloodSeasonWVo updateFloodByID(FloodSeasonW floodSeasonW, List<MultipartFile> fileList) {
        StringBuilder fileIds=null;
        try {
            if (null !=fileList&&!fileList.isEmpty()) {
                fileIds = fileInfoService.saveFileList(fileList, eventMangeFilePath, FileTableEnum.TF_YWPN_EVENT_MANGE.getTableName());
                String oldFile ="";
                String newFileIds = fileIds.toString();
                if(!StringUtils.isEmpty(oldFile=floodSeasonW.getUploadFileids())){
                    oldFile = oldFile+","+newFileIds;
                }
                floodSeasonW.setUploadFileids(oldFile);
            }
        }catch (Exception e){
            throw new ToflyDeniedException(e.getMessage() + ":文件上传失败");
        }

        this.saveOrUpdate(floodSeasonW);
        FloodSeasonWVo floodSeasonWVo1 = ToflyJsonUtil.fromObjectToEntity(floodSeasonW, FloodSeasonWVo.class);
        return  floodSeasonWVo1;
    }


    List<UserViewVo> findUserByIds(String ids){
        if(StringUtils.isEmpty(ids)){
            return  Collections.emptyList();
        }
        List<UserViewVo> list = new ArrayList<>();
        String[] splitIds = ids.split(",");
        if(splitIds!=null && splitIds.length>0){
            for (String  splitId:splitIds){
                UserViewVo userViewVo = new UserViewVo();
                User result = userClent.getById(Long.parseLong(splitId)).getResult();
                userViewVo.setId(result.getId());
                userViewVo.setUserName(result.getRealName());
                userViewVo.setPhone(result.getPhone());
                list.add(userViewVo);
            }
        }
        return  list;
    }
}
