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.domain.entity.PmProdLineSection;
import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import com.xisc.pm.app.service.PmSteelTypeGroupService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmSteelTypeGroup;
import com.xisc.pm.domain.repository.PmSteelTypeGroupRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 钢种组信息维护(PmSteelTypeGroup)应用服务
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-10-10 13:31:27
 */
@Service
public class PmSteelTypeGroupServiceImpl implements PmSteelTypeGroupService {
    @Autowired
    private PmSteelTypeGroupRepository pmSteelTypeGroupRepository;

    @Autowired
    private UserClient userClient;

    @Override
    public Page<PmSteelTypeGroup> selectList(PageRequest pageRequest, PmSteelTypeGroup pmSteelTypeGroup) {
        Page<PmSteelTypeGroup> page = PageHelper.doPageAndSort(pageRequest, () -> pmSteelTypeGroupRepository.selectByCondition(Condition.builder(PmSteelTypeGroup.class)
                .andWhere(Sqls.custom().andIn(PmSteelTypeGroup.FIELD_ST_NO, pmSteelTypeGroup.getStNoList(), true))
                .andWhere(Sqls.custom().andLike(PmSteelTypeGroup.FIELD_STEEL_GROUP_CODE, pmSteelTypeGroup.getSteelGroupCode(), true))
                .andWhere(Sqls.custom().andEqualTo(PmSteelTypeGroup.FIELD_TENANT_ID, pmSteelTypeGroup.getTenantId()))
                .build()));
        if (CollectionUtils.isEmpty(page.getContent())) {
            return page;
        }
        // 查询用户信息
        List<Long> userIdList = page.getContent().stream().map(PmSteelTypeGroup::getCreatedBy).distinct().collect(Collectors.toList());
        List<Long> updateByIdList = page.getContent().stream().map(PmSteelTypeGroup::getLastUpdatedBy).distinct().collect(Collectors.toList());
        userIdList.addAll(updateByIdList);
        Map<Long, MtUserInfo> mtUserInfoMap = userClient.userInfoBatchGet(pmSteelTypeGroup.getTenantId(), userIdList);
        for (PmSteelTypeGroup steelTypeGroup : page.getContent()) {
            // 用户信息
            if (mtUserInfoMap.containsKey(steelTypeGroup.getCreatedBy())) {
                MtUserInfo mtUserInfo = mtUserInfoMap.get(steelTypeGroup.getCreatedBy());
                steelTypeGroup.setCreatedByName(Strings.concat(mtUserInfo.getLoginName(), mtUserInfo.getRealName()));
            }
            if (mtUserInfoMap.containsKey(steelTypeGroup.getLastUpdatedBy())) {
                MtUserInfo updateUserInfo = mtUserInfoMap.get(steelTypeGroup.getLastUpdatedBy());
                steelTypeGroup.setLastUpdatedByName(Strings.concat(updateUserInfo.getLoginName(), updateUserInfo.getRealName()));
            }
        }
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveData(Long tenantId, PmSteelTypeGroup pmSteelTypeGroup) {
        if (pmSteelTypeGroup.getSteelGroupId() != null) {
            pmSteelTypeGroupRepository.updateByPrimaryKey(pmSteelTypeGroup);
        }else {
            pmSteelTypeGroupRepository.insertSelective(pmSteelTypeGroup);
        }
    }
}

