package com.njzscloud.dispose.mfg.craft.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.utils.GroupUtil;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.mfg.bom.pojo.result.SearchBomDetailResult;
import com.njzscloud.dispose.mfg.bom.pojo.result.SearchBomResult;
import com.njzscloud.dispose.mfg.craft.mapper.CraftWayMapper;
import com.njzscloud.dispose.mfg.craft.pojo.entity.CraftWayEntity;
import com.njzscloud.dispose.mfg.craft.pojo.param.AddAllCraftWayParam;
import com.njzscloud.dispose.mfg.craft.pojo.result.SearchCraftOperationResult;
import com.njzscloud.dispose.mfg.craft.pojo.result.SearchCraftWayResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 工艺路线
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CraftWayService extends ServiceImpl<CraftWayMapper, CraftWayEntity> implements IService<CraftWayEntity> {

    /**
     * 新增
     */
    public void add(CraftWayEntity craftWayEntity) {
        this.save(craftWayEntity);
    }

    /**
     * 修改
     */
    public void modify(CraftWayEntity craftWayEntity) {
        this.updateById(craftWayEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public CraftWayEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<CraftWayEntity> paging(PageParam pageParam, CraftWayEntity craftWayEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<CraftWayEntity>query(craftWayEntity)));
    }

    @Transactional(rollbackFor = Exception.class)
    public void addAll(AddAllCraftWayParam addAllCraftWayParam) {
        Long craftId = addAllCraftWayParam.getCraftId();
        this.remove(Wrappers.<CraftWayEntity>lambdaQuery().eq(CraftWayEntity::getCraftId, craftId));
        List<CraftWayEntity> collect = addAllCraftWayParam.getCraftWayList()
            .stream()
            .map(it -> BeanUtil.copyProperties(it, CraftWayEntity.class).setCraftId(craftId))
            .collect(Collectors.toList());
        this.saveBatch(collect);
    }

    public List<SearchCraftWayResult> listAll(Long craftId) {
        List<CraftWayEntity> craftWayEntityList = this.list(Wrappers.lambdaQuery(CraftWayEntity.class).eq(CraftWayEntity::getCraftId, craftId));
        List<SearchCraftWayResult> collect = craftWayEntityList
            .stream()
            .map(it -> BeanUtil.copyProperties(it, SearchCraftWayResult.class))
            .collect(Collectors.toList());
        List<Long> operationIdList = craftWayEntityList.stream().map(CraftWayEntity::getOperationId).collect(Collectors.toList());
        List<SearchCraftOperationResult> craftOperationResultList = baseMapper.listOperations(Wrappers.query().in("a.id", operationIdList));
        Map<Long, SearchCraftOperationResult> map1 = GroupUtil.k_o(craftOperationResultList, SearchCraftOperationResult::getId);
        for (SearchCraftWayResult searchCraftWayResult : collect) {
            searchCraftWayResult.setOperation(map1.get(searchCraftWayResult.getOperationId()));
        }
        List<Long> bomIdList = craftWayEntityList.stream().map(CraftWayEntity::getBomId).collect(Collectors.toList());
        List<SearchBomResult> bomList = baseMapper.listBom(Wrappers.query().in("a.id", bomIdList));
        List<SearchBomDetailResult> bomDetailList = baseMapper.listBomDetail(Wrappers.query().in("a.id", bomIdList));
        Map<Long, List<SearchBomDetailResult>> map2 = GroupUtil.k_a(bomDetailList, SearchBomDetailResult::getBomId);
        for (SearchBomResult searchBomResult : bomList) {
            searchBomResult.setDetails(map2.get(searchBomResult.getId()));
        }
        return collect;
    }
}
