package com.takensoft.cms.dept.web;

import com.takensoft.cms.author.service.AuthorService;
import com.takensoft.cms.author.vo.AuthorVO;
import com.takensoft.cms.dept.service.DeptService;
import com.takensoft.cms.dept.vo.DeptMbrVO;
import com.takensoft.cms.dept.vo.DeptVO;
import com.takensoft.common.HierachyVO;
import com.takensoft.common.message.MessageCode;
import com.takensoft.common.util.ResponseData;
import com.takensoft.common.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author takensoft
 * @since 2024.04.24
 * @modification
 *     since    |    author    | description
 *  2024.04.24  |  takensoft   | 최초 등록
 *
 * 부서 정보 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/admin/dept")
public class DeptController {

    private final ResponseUtil resUtil;
    private final DeptService deptService;
    private final AuthorService authorService;

    /**
     * @author takensoft
     * @since 2024.04.25
     * @param deptVO
     * @return
     * @throws Exception
     * 
     * 부서 등록
     */
    @PostMapping("/saveProc.json")
    public ResponseEntity<?> saveProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 등록
        HashMap<String, Object> result = deptService.deptSave(deptVO);
        int insertResult = (int) result.get("insertResult");

        // 응답 처리
        if(insertResult > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_INSERT_FAIL);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.29
     * @param deptMbrVO
     * @return
     * @throws Exception
     *
     * 부서 사용자 등록
     */
    @PostMapping("/deptMbrSaveProc.json")
    public ResponseEntity<?> deptMbrSaveProc(@RequestBody DeptMbrVO deptMbrVO) throws Exception {
        // 부서 사용자 등록
        int result = deptService.deptMbrSave(deptMbrVO);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_INSERT_FAIL);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.25
     * @param
     * @return
     * @throws Exception
     *
     * 부서 목록 조회
     */
    @GetMapping(value = "/findAll.json")
    public ResponseEntity<?> findAll() throws Exception {
        // Tree용
        List<HierachyVO> hierachyList = deptService.findByTopNode();
        // 권한 목록 조회
        List<AuthorVO> authList = authorService.findAllSystem();

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("hierachyList", hierachyList);
        result.put("authList", authList);
        result.put("newDept", new DeptVO());

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param
     * @return
     * @throws Exception
     *
     * 부서정보 상세 조회
     */
    @PostMapping(value = "/findByDept.json")
    public ResponseEntity<?> findByDept(@RequestBody HashMap<String, Object> params) throws Exception {
        // 부서정보 조회
        Map<String, Object> result = deptService.findByDept(params);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param deptVO
     * @return
     * @throws Exception
     *
     * 부서 수정
     */
    @PostMapping(value = "/updateProc.json")
    public ResponseEntity<?> updateProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 수정
        int result = deptService.deptUpdate(deptVO);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_UPDATE_FAIL);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param deptVO
     * @return
     * @throws Exception
     *
     * 부서 삭제
     */
    @PostMapping(value = "/deleteProc.json")
    public ResponseEntity<?> deleteProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 수정
        int result = deptService.deptDelete(deptVO);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_DELETE_FAIL);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param
     * @return
     * @throws Exception
     *
     * 부서 사용자 삭제
     */
    @PostMapping(value = "/deptMbrDelProc.json")
    public ResponseEntity<?> deptMbrDelProc(@RequestBody List<DeptMbrVO> deptMbrList) throws Exception {
        // 부서 사용자 삭제
        int result = deptService.deptMbrDelete(deptMbrList);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_DELETE_FAIL);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.29
     * @param
     * @return
     * @throws Exception
     *
     * 부서에 등록되지 않는 사용자 조회
     */
    @PostMapping("/findByMbr.json")
    public ResponseEntity<?> findByMbr(@RequestBody Map<String, String> params) throws Exception {
        // 사용자 목록 조회
        Map<String, Object> result = deptService.findByMbr(params);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author 박정하
     * @since 2024.05.09
     * @param deptList
     * @return
     * @throws Exception
     *
     * 부서 목록 수정
     */
    @PostMapping(value = "/updateListProc.json")
    public ResponseEntity<?> updateListProc(@RequestBody List<HierachyVO> deptList) throws Exception {
        // 부서 목록 수정
        int result = deptService.updateList(deptList);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_UPDATE_FAIL);
        }
    }
}
