package com.xisc.pm.app.service.impl;

import com.xisc.boot.common.feign.pojo.MtUserInfo;
import com.xisc.boot.common.sys.UserClient;
import com.xisc.pm.app.service.PmCdRuleItemService;
import com.xisc.pm.domain.entity.PmCdRuleItem;
import com.xisc.pm.domain.entity.PmCdRuleSet;
import com.xisc.pm.domain.repository.PmCdRuleItemRepository;
import com.xisc.pm.domain.repository.PmCdRuleSetRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 充当规则库(PmCdRuleItem)应用服务
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-08-16 10:57:46
 */
@Service
public class PmCdRuleItemServiceImpl implements PmCdRuleItemService {
    @Autowired
    private PmCdRuleItemRepository pmCdRuleItemRepository;

    @Autowired
    private UserClient userClient;

    @Autowired
    private PmCdRuleSetRepository pmCdRuleSetRepository;

    @Override
    public Page<PmCdRuleItem> selectList(PageRequest pageRequest, PmCdRuleItem pmCdRuleItem) {
        Page<PmCdRuleItem> page = PageHelper.doPageAndSort(pageRequest, () -> pmCdRuleItemRepository.selectList(pmCdRuleItem));
        if (CollectionUtils.isNotEmpty(page.getContent())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            List<Long> userIdList = page.getContent().stream().map(item -> item.getCreatedBy()).collect(Collectors.toList());
            List<Long> updateUserIdList = page.getContent().stream().map(item -> item.getLastUpdatedBy()).collect(Collectors.toList());
            Map<Long, MtUserInfo> userInfoMap = userClient.userInfoBatchGet(page.getContent().get(0).getTenantId(), userIdList);
            Map<Long, MtUserInfo> updateUserInfoMap = userClient.userInfoBatchGet(page.getContent().get(0).getTenantId(), updateUserIdList);
            page.getContent().forEach(item -> {
                //记录创建责任者
                MtUserInfo mtUserInfo = userInfoMap.get(item.getCreatedBy());
                if (Objects.nonNull(mtUserInfo)) {
                    item.setRecCreator(mtUserInfo.getLoginName() + mtUserInfo.getRealName());
                }
                //记录创建时刻 格式年年年年月月日日时时分分秒秒
                item.setRecCreateTime(sdf.format(item.getCreationDate()));
                //记录修改责任者
                MtUserInfo updateUserInfo = updateUserInfoMap.get(item.getLastUpdatedBy());
                if (Objects.nonNull(updateUserInfo)) {
                    item.setRecRevisor(updateUserInfo.getLoginName() + updateUserInfo.getRealName());
                }
                //记录修改时刻 年年年年月月日日时时分分秒秒
                item.setRecReviseTime(sdf.format(item.getLastUpdateDate()));
            });
        }
        return page;

    }

    @Override
    public void saveData(List<PmCdRuleItem> pmCdRuleItems) {
        List<PmCdRuleItem> insertList = pmCdRuleItems.stream().filter(line -> line.getRuleItemId() == null).collect(Collectors.toList());
        List<PmCdRuleItem> updateList = pmCdRuleItems.stream().filter(line -> line.getRuleItemId() != null).collect(Collectors.toList());
        insertList.forEach(item -> item.setValidFlag(1L));
        pmCdRuleItemRepository.batchInsertSelective(insertList);
        if (CollectionUtils.isNotEmpty(updateList)) {
            PmCdRuleItem ruleItem = updateList.get(0);
            Long validFlag = ruleItem.getValidFlag();
            Long ruleNo = ruleItem.getRuleNo();
            PmCdRuleSet query = new PmCdRuleSet();
            query.setRuleNo(ruleNo);
            query.setTenantId(ruleItem.getTenantId());
            List<PmCdRuleSet> pmCdRuleSetList = pmCdRuleSetRepository.selectList(query);
            for (PmCdRuleSet pmCdRuleSet : pmCdRuleSetList) {
                pmCdRuleSet.setValidFlag(validFlag);
            }
            pmCdRuleSetRepository.batchUpdateByPrimaryKeySelective(pmCdRuleSetList);
        }
        pmCdRuleItemRepository.batchUpdateByPrimaryKeySelective(updateList);
    }
}

