/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  jakarta.servlet.http.HttpServletRequest
 *  jakarta.servlet.http.HttpSession
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.beans.factory.annotation.Value
 *  org.springframework.stereotype.Service
 *  org.springframework.transaction.annotation.Transactional
 *  org.springframework.ui.Model
 *  org.springframework.web.multipart.MultipartFile
 */
package com.madeu.crm.procedureReview.svc;

import com.madeu.crm.procedureReview.dto.ProcedureReviewDTO;
import com.madeu.crm.procedureReview.mapper.ProcedureReviewMapper;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
import com.madeu.util.RequestLogUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.io.File;
import java.lang.invoke.CallSite;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ProcedureReviewService {
    private static final Logger log = LoggerFactory.getLogger(ProcedureReviewService.class);
    @Autowired
    private ProcedureReviewMapper procedureReviewMapper;
    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;
    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
    @Autowired
    private WebLogHistoryService webLogHistoryService;
    @Value(value="${file.upload.path.procedureReviewPath}")
    private String procedureReviewPath;
    @Value(value="${url.cdn}")
    private String urlCdn;

    private void saveLogHistory(HttpServletRequest request, ProcedureReviewDTO dto, String funcName, String serviceName, Object responseValue, String errorMsg) {
        try {
            HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
            HashMap<String, Object> insertMap = new HashMap<String, Object>();
            insertMap.put("url", request.getRequestURI());
            insertMap.put("func", request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1).replace(".do", ""));
            insertMap.put("funcName", funcName);
            insertMap.put("service", "procedureReviewService");
            insertMap.put("serviceName", serviceName);
            insertMap.put("requestValue", String.valueOf(dto));
            insertMap.put("responseValue", String.valueOf(responseValue));
            insertMap.put("tId", dto.getTId());
            if (errorMsg == null || errorMsg.isEmpty()) {
                insertMap.put("resultCode", "SUCCESS");
            } else {
                insertMap.put("resultCode", "ERROR");
            }
            insertMap.put("resultMsg", errorMsg != null ? errorMsg : "");
            insertMap.put("muMemberId", dto.getLoginMemberId());
            this.webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean fillMenuAuth(ProcedureReviewDTO dto) {
        try {
            HashMap<String, Object> checkMap = new HashMap<String, Object>();
            checkMap.put("loginMemberId", dto.getLoginMemberId());
            List<Map<String, Object>> userListMap = this.webMemberSqlMapDAO.checkMember(checkMap);
            if (userListMap.size() == 1) {
                dto.setMenuClassAuthId(String.valueOf(userListMap.get(0).get("muAuthId")));
                HashMap<String, Object> authCheckMap = new HashMap<String, Object>();
                authCheckMap.put("menuClass", dto.getMenuClass());
                authCheckMap.put("muAuthId", dto.getMenuClassAuthId());
                List<Map<String, Object>> authList = this.webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckMap);
                if (authList.size() == 1) {
                    dto.setSelectUseYn(String.valueOf(authList.get(0).get("selectUseYn")));
                    dto.setInsertUseYn(String.valueOf(authList.get(0).get("insertUseYn")));
                    dto.setUpdateUseYn(String.valueOf(authList.get(0).get("updateUseYn")));
                    dto.setDeleteUseYn(String.valueOf(authList.get(0).get("deleteUseYn")));
                    dto.setDownloadUseYn(String.valueOf(authList.get(0).get("downloadUseYn")));
                    return true;
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void resolveMoveProcedureReviewList(HttpSession session, HttpServletRequest request, Model model, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        if (this.fillMenuAuth(dto)) {
            model.addAttribute("selectUseYn", (Object)dto.getSelectUseYn());
            model.addAttribute("insertUseYn", (Object)dto.getInsertUseYn());
            model.addAttribute("updateUseYn", (Object)dto.getUpdateUseYn());
            model.addAttribute("deleteUseYn", (Object)dto.getDeleteUseYn());
            model.addAttribute("downloadUseYn", (Object)dto.getDownloadUseYn());
        }
    }

    public ProcedureReviewDTO getProcedureReviewList(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        try {
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getSelectUseYn())) {
                if (dto.getDir() != null && "B".equals(dto.getDir())) {
                    dto.setDir("ASC");
                } else {
                    dto.setDir("DESC");
                }
                dto.setUseYn("Y");
                List<Map<String, Object>> countList = this.procedureReviewMapper.selectTotalProcedureReviewCount(dto);
                int total = Integer.parseInt(String.valueOf(countList.get(0).get("totalCount")));
                dto.setTotalCount(total);
                if (total > 0) {
                    dto.setRows(this.procedureReviewMapper.selectListProcedureReview(dto));
                } else {
                    dto.setRows(new ArrayList());
                }
                dto.setMsgCode("0");
                dto.setSuccess("true");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uc870\ud68c \uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\uc5d0\ub7ec \ubc1c\uc0dd: " + e.getMessage());
        }
        return dto;
    }

    public ProcedureReviewDTO getProcedureReview(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        try {
            if (dto.getMuProcedureReviewId() == null || dto.getMuProcedureReviewId().trim().isEmpty()) {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uc2dc\uc220\ud6c4\uae30 \uc815\ubcf4\uac00 \uc5c6\uc2b5\ub2c8\ub2e4.");
                return dto;
            }
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getSelectUseYn())) {
                List<Map<String, Object>> res = this.procedureReviewMapper.selectProcedureReview(dto);
                dto.setRows(res);
                dto.setMsgCode("0");
                dto.setSuccess("true");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\uc870\ud68c \uc911 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.");
        }
        return dto;
    }

    public ProcedureReviewDTO getCategoryList(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        try {
            dto.setCategoryNo("07");
            List<Map<String, Object>> cats = this.procedureReviewMapper.selectListPhotoCategory(dto);
            dto.setRows(cats);
            dto.setMsgCode("0");
            dto.setSuccess("true");
        }
        catch (Exception e) {
            e.printStackTrace();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\uce74\ud14c\uace0\ub9ac \uc815\ubcf4 \uc870\ud68c \uc2e4\ud328");
        }
        return dto;
    }

    public void resolveMoveProcedureReviewInsert(HttpSession session, HttpServletRequest request, Model model, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        if (this.fillMenuAuth(dto)) {
            model.addAttribute("selectUseYn", (Object)dto.getSelectUseYn());
            model.addAttribute("insertUseYn", (Object)dto.getInsertUseYn());
            model.addAttribute("updateUseYn", (Object)dto.getUpdateUseYn());
            model.addAttribute("deleteUseYn", (Object)dto.getDeleteUseYn());
            model.addAttribute("downloadUseYn", (Object)dto.getDownloadUseYn());
        }
    }

    public void resolveMoveProcedureReviewUpdate(HttpSession session, HttpServletRequest request, Model model, ProcedureReviewDTO dto) {
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        if (this.fillMenuAuth(dto)) {
            model.addAttribute("selectUseYn", (Object)dto.getSelectUseYn());
            model.addAttribute("insertUseYn", (Object)dto.getInsertUseYn());
            model.addAttribute("updateUseYn", (Object)dto.getUpdateUseYn());
            model.addAttribute("deleteUseYn", (Object)dto.getDeleteUseYn());
            model.addAttribute("downloadUseYn", (Object)dto.getDownloadUseYn());
        }
    }

    @Transactional(rollbackFor={Exception.class})
    public ProcedureReviewDTO putProcedureReviewFile(HttpSession session, HttpServletRequest request, MultipartFile file, ProcedureReviewDTO dto) {
        String errorMsg = "";
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        dto.setRegId(dto.getLoginMemberId());
        dto.setModId(dto.getLoginMemberId());
        try {
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getInsertUseYn()) && file != null && !file.isEmpty()) {
                String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String savedName = UUID.randomUUID().toString() + ext;
                String folder = "/procedureReview/" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + "/";
                File outDir = new File(this.procedureReviewPath + folder);
                if (!outDir.exists()) {
                    outDir.mkdirs();
                }
                file.transferTo(new File(outDir, savedName));
                HashMap<String, CallSite> resultRow = new HashMap<String, CallSite>();
                resultRow.put("filePath", (CallSite)((Object)(this.urlCdn + folder + savedName)));
                dto.setRows(resultRow);
                dto.setMsgCode("0");
                dto.setSuccess("true");
                dto.setMsgDesc("\uc815\uc0c1\uc801\uc73c\ub85c \ub4f1\ub85d\ub418\uc5c8\uc2b5\ub2c8\ub2e4.");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uad8c\ud55c\uc774 \uc5c6\uac70\ub098 \ud30c\uc77c\uc774 \ube44\uc5b4\uc788\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            errorMsg = e.toString();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\ub4f1\ub85d \uc911 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.");
        }
        this.saveLogHistory(request, dto, "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \uc774\ubbf8\uc9c0\ud30c\uc77c \uc800\uc7a5", "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \ub4f1\ub85d", dto, errorMsg);
        return dto;
    }

    @Transactional(rollbackFor={Exception.class})
    public ProcedureReviewDTO putProcedureReview(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        String errorMsg = "";
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        dto.setRegId(dto.getLoginMemberId());
        dto.setModId(dto.getLoginMemberId());
        try {
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getInsertUseYn())) {
                this.procedureReviewMapper.insertProcedureReview(dto);
                dto.setMsgCode("0");
                dto.setSuccess("true");
                dto.setMsgDesc("\uc815\uc0c1\uc801\uc73c\ub85c \ub4f1\ub85d\ub418\uc5c8\uc2b5\ub2c8\ub2e4.");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            errorMsg = e.toString();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\ub4f1\ub85d \uc911 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.");
        }
        this.saveLogHistory(request, dto, "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \ub4f1\ub85d", "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \ub4f1\ub85d", dto, errorMsg);
        return dto;
    }

    @Transactional(rollbackFor={Exception.class})
    public ProcedureReviewDTO modProcedureReview(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        String errorMsg = "";
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        dto.setRegId(dto.getLoginMemberId());
        dto.setModId(dto.getLoginMemberId());
        try {
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getUpdateUseYn())) {
                this.procedureReviewMapper.updateProcedureReview(dto);
                dto.setMsgCode("0");
                dto.setSuccess("true");
                dto.setMsgDesc("\uc815\uc0c1\uc801\uc73c\ub85c \uc218\uc815\ub418\uc5c8\uc2b5\ub2c8\ub2e4.");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            errorMsg = e.toString();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\uc218\uc815 \uc911 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.");
        }
        this.saveLogHistory(request, dto, "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \uc218\uc815", "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \uc218\uc815", dto, errorMsg);
        return dto;
    }

    @Transactional(rollbackFor={Exception.class})
    public ProcedureReviewDTO delProcedureReview(HttpSession session, HttpServletRequest request, ProcedureReviewDTO dto) {
        String errorMsg = "";
        dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
        dto.setRegId(dto.getLoginMemberId());
        dto.setModId(dto.getLoginMemberId());
        try {
            if (this.fillMenuAuth(dto) && "Y".equals(dto.getDeleteUseYn())) {
                String[] ids;
                for (String id : ids = dto.getMuProcedureReviewId().split(",")) {
                    dto.setMuProcedureReviewId(id);
                    this.procedureReviewMapper.deleteProcedureReview(dto);
                }
                dto.setMsgCode("0");
                dto.setSuccess("true");
                dto.setMsgDesc("\uc815\uc0c1\uc801\uc73c\ub85c \uc0ad\uc81c\ub418\uc5c8\uc2b5\ub2c8\ub2e4.");
            } else {
                dto.setMsgCode("-1");
                dto.setMsgDesc("\uc0ad\uc81c \uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            errorMsg = e.toString();
            dto.setMsgCode("-1");
            dto.setMsgDesc("\uc0ad\uc81c \uc911 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.");
        }
        this.saveLogHistory(request, dto, "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \uc0ad\uc81c", "\ud648\ud398\uc774\uc9c0 \uc2dc\uc220\ud6c4\uae30 \uc0ad\uc81c", dto, errorMsg);
        return dto;
    }
}
