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

import com.xisc.pm.api.dto.PmAutoSubSettingDTO;
import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.xisc.pm.app.service.PmAutoSubSettingService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmAutoSubSetting;
import com.xisc.pm.domain.repository.PmAutoSubSettingRepository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 自动替代配置表(PmAutoSubSetting)应用服务
 *
 * @author BKGY
 * @since 2024-12-27 08:56:23
 */
@Service
public class PmAutoSubSettingServiceImpl implements PmAutoSubSettingService {
    @Autowired
    private PmAutoSubSettingRepository pmAutoSubSettingRepository;

    @Override
    public Page<PmAutoSubSetting> selectList(PageRequest pageRequest, PmAutoSubSetting pmAutoSubSetting) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmAutoSubSettingRepository.selectList(pmAutoSubSetting));
    }

    @Override
    public void saveData(PmAutoSubSettingDTO pmAutoSubSettingDTO) {

        PmAutoSubSetting pmAutoSubSetting = new PmAutoSubSetting();

        // 将 pmAutoSubSetting 的属性值复制到 pmAutoSubSettingEntity
        BeanUtils.copyProperties(pmAutoSubSettingDTO, pmAutoSubSetting, "overshootCondition","priMode");

        String overshootCondition = pmAutoSubSettingDTO.getOvershootCondition().stream().collect(Collectors.joining(","));
        String priMode = pmAutoSubSettingDTO.getPriMode().stream().collect(Collectors.joining(","));
        String useableDstock = pmAutoSubSettingDTO.getUseableDstock().stream().collect(Collectors.joining(","));

        pmAutoSubSetting.setOvershootCondition(overshootCondition);
        pmAutoSubSetting.setPriMode(priMode);
        pmAutoSubSetting.setUseableDstock(useableDstock);

        if(pmAutoSubSetting.getId() == null){
            pmAutoSubSettingRepository.insert(pmAutoSubSetting);
        }else{
            pmAutoSubSettingRepository.updateByPrimaryKeySelective(pmAutoSubSetting);
        }
//        List<PmAutoSubSetting> insertList = pmAutoSubSettings.stream().filter(line -> line.getId() == null).collect(Collectors.toList());
//        List<PmAutoSubSetting> updateList = pmAutoSubSettings.stream().filter(line -> line.getId() != null).collect(Collectors.toList());
//        pmAutoSubSettingRepository.bacthI(insertList);
//        pmAutoSubSettingRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public PmAutoSubSettingDTO selectPmAutoSubSetting() {
        PmAutoSubSetting pmAutoSubSetting = pmAutoSubSettingRepository.selectPmAutoSubSetting();
        PmAutoSubSettingDTO pmAutoSubSettingDTO = new PmAutoSubSettingDTO();
        if(pmAutoSubSetting != null){
            BeanUtils.copyProperties(pmAutoSubSetting, pmAutoSubSettingDTO, "overshootCondition","priMode");
            pmAutoSubSettingDTO.setOvershootCondition( Arrays.asList(pmAutoSubSetting.getOvershootCondition().split(",")));
            pmAutoSubSettingDTO.setPriMode(Arrays.asList(pmAutoSubSetting.getPriMode().split(",")));
            pmAutoSubSettingDTO.setUseableDstock(Arrays.asList(pmAutoSubSetting.getUseableDstock().split(",")));

//        List<PmAutoSubSettingDTO.StockCode> stockCodeList = new ArrayList<>() ;
//        for(String stockCode: pmAutoSubSetting.getUseableDstock().split(",")){
//            PmAutoSubSettingDTO.StockCode stockCodeClass = new PmAutoSubSettingDTO.StockCode();
//            stockCodeClass.stockCode = stockCode;
//            stockCodeList.add(stockCodeClass);
//        }
            //处理值集 将值集转换为对象返回前端
            List<PmAutoSubSettingDTO.StockCode> stockCodeList = Arrays.stream(pmAutoSubSetting.getUseableDstock().split(","))
                    .map(String::trim)
                    .filter(code -> !code.isEmpty())
                    .map(code -> {
                        PmAutoSubSettingDTO.StockCode stockCodeClass = new PmAutoSubSettingDTO.StockCode();
                        stockCodeClass.stockCode = code;
                        return stockCodeClass;
                    })
                    .collect(Collectors.toList());
            pmAutoSubSettingDTO.setUseableDstockLov(stockCodeList);
        }
        return pmAutoSubSettingDTO;
    }
}

