package com.tofly.xrtygis.custom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtygis.config.FilePathBean;
import com.tofly.xrtygis.custom.service.CustomQuestionMarkServicee;
import com.tofly.xrtygis.custom.service.QuestionMsgService;
import com.tofly.xrtygis.custom.util.service.FileInfoService;
import com.tofly.xrtygis.entity.File;
import com.tofly.xrtygis.entity.Questionmark;
import com.tofly.xrtygis.service.FileService;
import com.tofly.xrtygis.service.QuestionmarkService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 自定义疑问标识
 *
 * @author 陈浩
 */
@Service
@AllArgsConstructor
public class CustomQuestionMarkServiceImpl implements CustomQuestionMarkServicee {

    private final static String TABLE_NAME = "TF_YWPN_QUESTIONMARK_W";
    /**
     * 文件路径
     */
    private final FilePathBean filePathBean;
    /**
     * 公共文件表
     */
    private final FileService fileService;

    private final FileInfoService fileInfoService;
    /**
     * 疑问标识表
     */
    private final QuestionmarkService questionmarkService;

    private final QuestionMsgService questionMsgService;

    /**
     * 上传疑问标识
     *
     * @param fileList fileList
     * @param mark    疑问标识
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upLoadMark(List<MultipartFile> fileList, Questionmark mark) {
        if (fileList.isEmpty()) {
            throw new ToflyDeniedException("上传文件不能为空");
        }
        StringBuilder fileIds = fileInfoService.saveFileList(fileList, filePathBean.getPollSourceFile(), TABLE_NAME);

        mark.setFileId(fileIds.toString());
        mark.setStatus("2");
        questionmarkService.save(mark);

        return true;
    }

    /**
     * 删除疑问标识接口
     *
     * @param markId 标识id
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMark(String markId) {
        if (markId == null){
            throw new ToflyDeniedException("标识id不能为空");
        }
        String[] markIds = markId.split(",");
        for (String id : markIds) {
            //根据标识id查询记录获取
            Questionmark questionmark = questionmarkService.getById(markId);

            if (questionmark != null){
                String[] split = questionmark.getFileId().split(",");
                for (String fileId : split) {
                    File file = fileService.getById(fileId);
                    if (null != file){
                        String filePath = file.getFilePath();
                        //删除服务器上文件
                        Boolean flag = FtpUtil.deleteFile(filePath);
                        if (!flag) {
                            throw new ToflyDeniedException("删除未成功");
                        }
                        fileService.removeById(fileId);
                    }
                }
            }
        }

        //清除疑问标识表和文件表的内容
        return questionmarkService.removeByIds(Arrays.asList(markIds));
    }

    /**
     * 分页获取当前用户创建的标记
     *
     * @param page page
     * @return page
     */
    @Override
    public Page getMeMark(Page page) {
        return questionmarkService.page(page, Wrappers.<Questionmark>lambdaQuery()
                .eq(Questionmark::getCreateUser, SecurityUtils.getUserId())
                .orderByDesc(Questionmark::getCreateDate));
    }

    /**
     * 疑问标识上报
     *
     * @param ids 多个id
     * @return boolean
     */
    @Override
    public boolean handleMark(String ids,Long userId) {

        for (String id : ids.split(",")) {
            Questionmark que = new Questionmark();
            que.setStatus("0");
            que.setId(Long.parseLong(id));
            que.setHandleUser(userId);
            questionmarkService.updateById(que);
            questionMsgService.queMsgPush(JSONObject.toJSONString(questionmarkService.getById(id)));
        }
        return true;
    }

}
