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

import com.xisc.pm.api.dto.export.PmRetainStDTO;
import com.xisc.pm.domain.entity.PmIrsubableSlab;
import com.xisc.pm.domain.entity.PmSampleReserveBt;
import com.xisc.pm.infra.mapper.PmIrsubableContMapper;
import com.xisc.pm.infra.mapper.PmRetainStMapper;
import io.choerodon.core.domain.Page;
import io.choerodon.core.exception.CommonException;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
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.PmRetainStService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmRetainSt;
import com.xisc.pm.domain.repository.PmRetainStRepository;

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

/**
 * 钢种信息配置表(PmRetainSt)应用服务
 *
 * @author BKGY
 * @since 2024-12-24 09:11:15
 */
@Service
public class PmRetainStServiceImpl implements PmRetainStService {
    @Autowired
    private PmRetainStRepository pmRetainStRepository;

    @Autowired
    private PmRetainStMapper pmRetainStMapper;

    @Override
    public Page<PmRetainSt> selectList(PageRequest pageRequest, PmRetainSt pmRetainSt) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmRetainStRepository.selectList(pmRetainSt));
    }

    @Override
    public void saveData(List<PmRetainSt> pmRetainSts) {

        if(CollectionUtils.isEmpty(pmRetainSts)){
            return;
        }
        Long tenantId = pmRetainSts.get(0).getTenantId();
        // 校验传入数据唯一性
        List<String> stNoList = pmRetainSts.stream().map(PmRetainSt::getStNo).distinct().collect(Collectors.toList());
        if (stNoList.size() < pmRetainSts.size()) {
            throw new CommonException("pm.retain_st_001");
        }
        List<Long> idList = pmRetainSts.stream().filter(item -> item.getId() != null).map(PmRetainSt::getId).collect(Collectors.toList());



        List<PmRetainSt> existList = pmRetainStRepository.selectByCondition(Condition.builder(PmRetainSt.class)
                .andWhere(Sqls.custom().andIn(PmRetainSt.FIELD_ST_NO, stNoList))
                .andWhere(Sqls.custom().andNotIn(PmRetainSt.FIELD_ID, idList, true))
                .andWhere(Sqls.custom().andEqualTo(PmRetainSt.FIELD_TENANT_ID, tenantId)).build());
        if(CollectionUtils.isNotEmpty(existList)){
            throw new CommonException("pm.retain_st_002",existList.get(0).getStNo());
        }

        List<PmRetainSt> insertList = pmRetainSts.stream().filter(line -> line.getId() == null).collect(Collectors.toList());
        List<PmRetainSt> updateList = pmRetainSts.stream().filter(line -> line.getId() != null).collect(Collectors.toList());
        pmRetainStRepository.batchInsertSelective(insertList);
        pmRetainStRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public List<PmRetainStDTO> exportList(Long organizationId, PmRetainSt pmRetainSt) {
        pmRetainSt.setTenantId(organizationId);
        return pmRetainStMapper.exportList(pmRetainSt);
    }
}

