package com.xisc.pm.infra.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;

import com.xisc.boot.common.feign.pojo.MtUserInfo;
import com.xisc.boot.common.sys.UserClient;
import com.xisc.pm.domain.entity.PmRequestInfo;
import com.xisc.pm.domain.repository.PmRequestInfoRepository;
import com.xisc.pm.infra.enums.AutoPdRequestStatus;

import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 请求信息表 资源库实现
 *
 * @author jun.zeng 2024-11-25 14:51:07
 */
@Component
public class PmRequestInfoRepositoryImpl extends BaseRepositoryImpl<PmRequestInfo> implements PmRequestInfoRepository {
    
    @Autowired
    private UserClient userClient;

    @Override
    public boolean checkAutoPdExecStatus(Long tenantId) {
        Date date = DateUtils.addMinutes(new Date(), -5);
        return this.selectCountByCondition(Condition.builder(PmRequestInfo.class)
                .andWhere(Sqls.custom().andEqualTo(PmRequestInfo.FIELD_TENNT_ID, tenantId)
                        .andGreaterThanOrEqualTo(PmRequestInfo.FIELD_CREATION_DATE, date)
                        .andEqualTo(PmRequestInfo.FIELD_REQUEST_STTUS, AutoPdRequestStatus.EXECUTION.name()))
                .build()) > 0;
    }

    @Override
    @Transactional
    public int markExecFinshed(PmRequestInfo pmRequestInfo, Long tenantId) {
        PmRequestInfo record = new PmRequestInfo();
        record.setTenantId(tenantId);
        record.setRequestId(pmRequestInfo.getRequestId());
        record.setObjectVersionNumber(pmRequestInfo.getObjectVersionNumber());
        record.setRequestStatus(AutoPdRequestStatus.UNPUBLISHED.name());
        record.setExecEndDate(new Date());
        record.setTotSmallPlateNum(pmRequestInfo.getTotSmallPlateNum());
        return this.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int markExecFailed(PmRequestInfo pmRequestInfo, Long tenantId) {
        PmRequestInfo record = new PmRequestInfo();
        record.setTenantId(tenantId);
        record.setRequestId(pmRequestInfo.getRequestId());
        record.setObjectVersionNumber(pmRequestInfo.getObjectVersionNumber());
        record.setRequestStatus(AutoPdRequestStatus.EXEC_ERROR.name());
        return this.updateByPrimaryKeySelective(record);
    }

    @Override
    public int markPublishing(PmRequestInfo pmRequestInfo, Long tenantId) {
        PmRequestInfo record = new PmRequestInfo();
        record.setTenantId(tenantId);
        record.setRequestId(pmRequestInfo.getRequestId());
        record.setObjectVersionNumber(pmRequestInfo.getObjectVersionNumber());
        record.setRequestStatus(AutoPdRequestStatus.PUBLISHING.name());
        record.setPublishedVersion(pmRequestInfo.getPublishedVersion());
        return this.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int markPublished(PmRequestInfo pmRequestInfo, Long tenantId) {
        PmRequestInfo record = new PmRequestInfo();
        record.setTenantId(tenantId);
        record.setRequestId(pmRequestInfo.getRequestId());
        record.setObjectVersionNumber(pmRequestInfo.getObjectVersionNumber());
        record.setRequestStatus(AutoPdRequestStatus.COMPLETED.name());
        record.setPublishedVersion(pmRequestInfo.getPublishedVersion());
        return this.updateByPrimaryKeySelective(record);
    }

    @Override
    public Page<PmRequestInfo> selectList(Long organizationId, PmRequestInfo pmRequestInfo, PageRequest pageRequest) {
        Page<PmRequestInfo> result = PageHelper.doPageAndSort(pageRequest, () -> this.select(pmRequestInfo));
        List<Long> userIdList = new ArrayList<>();
        for (PmRequestInfo requestInfo : result) {
            if(requestInfo.getCreatedBy() != null) {
                userIdList.add(requestInfo.getCreatedBy());
            }
            if(requestInfo.getLastUpdatedBy() != null) {
                userIdList.add(requestInfo.getLastUpdatedBy());
            }
        }
        if(userIdList != null && !userIdList.isEmpty()) {
            Map<Long, MtUserInfo> map = userClient.userInfoBatchGet(organizationId, userIdList);
            if(map != null && !map.isEmpty()) {
                for (PmRequestInfo requestInfo : result) {
                    if(map.containsKey(requestInfo.getCreatedBy())) {
                        MtUserInfo userInfo = map.get(requestInfo.getCreatedBy());
                        requestInfo.setCreateByName(userInfo.getLoginName() + userInfo.getRealName());
                    }
                    if(map.containsKey(requestInfo.getLastUpdatedBy())) {
                        MtUserInfo userInfo = map.get(requestInfo.getLastUpdatedBy());
                        requestInfo.setUpdateByName(userInfo.getLoginName() + userInfo.getRealName());
                    }
                }
            }
        }
        return result;
    }

}
