package com.oqpo.api.service.system;


import com.oqpo.api.entity.system.JobEntity;
import com.oqpo.api.entity.oper.MemberEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.system.JobMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.job.JobResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class JobService extends CommonService {
    @Autowired
    private JobMapper jobMapper;

    /*
     작업내역 리스트 조회
    */
    public GridResponse jobGridList(String fromDt,String toDt, String menuNo, String actTpCd, String userId, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();
        int gridRecords = jobMapper.selectJobGridListCnt(fromDt, toDt, menuNo, actTpCd, userId);
        //int gridRecords = jobMapper.selectJobInfoGridListCnt();
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<JobEntity> entities = jobMapper.selectJobGridList(fromDt, toDt, menuNo, actTpCd, userId, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> JobResponse.builder()
                        .viewCd("R")
                        .jobNo(m.getJobNo())
                        .userId(m.getUserId())
                        .userNm(m.getUserNm())
                        .menuNo(m.getMenuNo())
                        .menuNm(m.getMenuNm())
                        .actTpCd(m.getActTpCd())
                        .actTpNm(m.getActTpNm())
                        .actNm(m.getActNm())
                        .ip(m.getIp())
                        .addDt(m.getAddDt())
                        .addId(m.getAddId())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }


    /*
      채번관리 - 저장
     */
    @Transactional
    public Boolean saveJob(MemberEntity memberEntity, String path) throws Exception {
        Boolean isAuthChk = true;
        try {
            // 사용권한 체크
            JobEntity jobEntity = jobMapper.selectConnectUserAuth(memberEntity.getUserId(), path) ;

            if ("N".equals(memberEntity.getSystemAdminYn()) ) {
                if (jobEntity == null )
                {
                    isAuthChk =  false ;
                }
            }

            if (isAuthChk) {
                String jobNo = fnGetKeyNo(2);

                JobEntity entity = new JobEntity();
                entity.setJobNo(jobNo);
                entity.setActNm(jobEntity.getActNm());
                entity.setActTpCd(jobEntity.getActTpCd());
                entity.setIp(getIpAddress());
                entity.setMenuNo(jobEntity.getMenuNo());
                entity.setUserId(memberEntity.getUserId());
                jobMapper.insertJobInfo(entity);
            }

        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
        return isAuthChk;
    }


    public String getIpAddress() {
        InetAddress local;
        String ip = "";
        try {
            local = InetAddress.getLocalHost();
            ip = local.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }
}
