package com.kingdee.eas.custom.sendmessage.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.logs.IMessageLogs;
import com.kingdee.eas.custom.logs.MessageLogsCollection;
import com.kingdee.eas.custom.logs.MessageLogsFactory;
import com.kingdee.eas.custom.logs.MessageLogsInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.util.ToolUtils;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class MessageLogsListHandler extends ListHandler {

    /**
     * 作废链接
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     * @throws BOSException
     */
    public void cancelLinkAction (HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
            throws SHRWebException, BOSException, EASBizException {
        Context ctx = SHRContext.getInstance().getContext();
        String billIds = StringUtils.cnulls(request.getParameter("validIds"));
        Map<String, Object> resultMap = new HashMap ();
        if(StringUtils.isEmpty(billIds)){
//            resultMap.put("successCount", 0);
//            resultMap.put("message", "选择的数据为空！");
            writeErrorData("选择的数据为空！");
        }
        //字符串转数组
        String[] billIdArr = billIds.split(",");
        IMessageLogs localInstance = MessageLogsFactory.getLocalInstance(ctx);
        MessageLogsCollection msgLogsCol = localInstance.getMessageLogsCollection("where id in ("+ ToolUtils.aryToStr(billIdArr)+")");
        //for循环作废
        CoreBaseCollection coreBaseCollection = new CoreBaseCollection();
        for (int i = 0; i < msgLogsCol.size(); i++) {
            MessageLogsInfo messageLogsInfo = msgLogsCol.get(i);
            if(exeCancel(messageLogsInfo)){
                messageLogsInfo.setIsVaild(false);
                coreBaseCollection.add(messageLogsInfo);
            }
        }

        if(coreBaseCollection.size() > 0){
            localInstance.updateBatchData(coreBaseCollection);
            resultMap.put("successCount", coreBaseCollection.size());
            resultMap.put("message", "处理成功"+coreBaseCollection.size()+"条数据！");
        }
        writeSuccessData(resultMap);
    }


    /**
     * 执行作废
     */
    private boolean  exeCancel(MessageLogsInfo msgLogsInfo){
       if(  msgLogsInfo.isIsVaild() ){

           String dirName = msgLogsInfo.getUrldir();
           String fileName = msgLogsInfo.getFileName();
           // 删除硬盘上urldir文件夹下的fileName文件
           if (!StringUtils.isEmpty(dirName) && !StringUtils.isEmpty(fileName)) {
               String home = System.getProperty("EAS_HOME"); // 获取 EAS 根目录
               String fileDir = home + "/server/deploy/easweb.ear/shr_web.war/tmp/electronicAttachment/" + dirName;
               // 尝试删除文件
               Path filePath = Paths.get(fileDir, fileName);
               try {
                   Files.deleteIfExists(filePath); // 删除文件，如果文件存在
                   return true; // 返回成功状态
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
        return false; // 如果未成功执行作废或文件未找到，
    }


}
