package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.ApsCraftRouteOutMaterRequestDao;
import com.rongwei.bscommon.sys.dao.ApsCraftRouteStepEquDao;
import com.rongwei.bscommon.sys.dao.ApsCraftStepDao;
import com.rongwei.bscommon.sys.service.ApsCraftRouteStepService;
import com.rongwei.bscommon.sys.service.ApsCraftStepService;
import com.rongwei.bsentity.domain.ApsCraftRouteStepDo;
import com.rongwei.bsentity.domain.ApsCraftStepDo;
import com.rongwei.bsentity.vo.UpdateApsRouteAboutVo;
import com.rongwei.rwcommon.base.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 *
 */
@Service
public class ApsCraftStepServiceImpl extends ServiceImpl<ApsCraftStepDao, ApsCraftStepDo>
        implements ApsCraftStepService {
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private ApsCraftRouteStepEquDao apsCraftRouteStepEquDao;
    @Autowired
    private ApsCraftRouteOutMaterRequestDao apsCraftRouteOutMaterRequestDao;
    @Autowired
    private ApsCraftRouteStepService apsCraftRouteStepService;

    // 删除标识
    public static final String DELTED_ROW_STATUS = "delete";
    // 更新标识
    public static final String ADD_ROW_STATUS = "add";
    // 修改标识
    public static final String EDIT_ROW_STATUS = "edit";


    /**
     * 保存工艺路线时 更新相关数据
     *
     * @param updateApsRouteAboutVos
     * @return
     */
    @Override
    public R updateApsRouteAboutVos(List<UpdateApsRouteAboutVo> updateApsRouteAboutVos, String routeId) {
        log.debug("开始更新该工艺路线相关信息");
        if (updateApsRouteAboutVos == null || updateApsRouteAboutVos.isEmpty()) {
            return R.ok();
        }
        // 获取需要删除工艺步骤ID 数据更新先删除在更新
        List<String> deletedIds = updateApsRouteAboutVos.stream()
                .filter(info -> DELTED_ROW_STATUS.equals(info.getRowstatus()) || EDIT_ROW_STATUS.equals(info.getRowstatus()))
                .map(ApsCraftRouteStepDo::getId).collect(Collectors.toList());
        if (!deletedIds.isEmpty()) {
            apsCraftRouteStepEquDao.removeByIdList(deletedIds, routeId);
            apsCraftRouteOutMaterRequestDao.removeByIdList(deletedIds, routeId);
        }
        // 如下工艺步骤需要新增对应的  步骤设备和步骤输出规则
        List<UpdateApsRouteAboutVo> addStepAboutData = updateApsRouteAboutVos.stream()
                .filter(info -> ADD_ROW_STATUS.equals(info.getRowstatus()) || EDIT_ROW_STATUS.equals(info.getRowstatus()))
                .collect(Collectors.toList());

        if (!addStepAboutData.isEmpty()) {
            List<ApsCraftRouteStepDo> updateList=new ArrayList<>();
            updateApsRouteAboutVos.forEach(data->{
                ApsCraftRouteStepDo apsCraftRouteStepDo=  new ApsCraftRouteStepDo();
                BeanUtils.copyProperties(data,apsCraftRouteStepDo);
                updateList.add(apsCraftRouteStepDo);
            });
            apsCraftRouteStepService.saveOrUpdateBatch(updateList);
            apsCraftRouteStepEquDao.copyDataById(addStepAboutData, routeId);
            apsCraftRouteOutMaterRequestDao.copyDataById(addStepAboutData, routeId);
        }

        return R.ok();
    }
}




