package com.fjhx.service.bom.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.bom.BomDetails;
import com.fjhx.mapper.bom.BomDetailsMapper;
import com.fjhx.params.bom.BomDetailsEx;
import com.fjhx.params.bom.BomDetailsVo;
import com.fjhx.service.bom.BomDetailsService;
import com.fjhx.util.MaterialAttributeAssignUtil;
import com.fjhx.utils.Assert;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * bom明细 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-01
 */
@Service
public class BomDetailsServiceImpl extends ServiceImpl<BomDetailsMapper, BomDetails> implements BomDetailsService {

    @Override
    public List<BomDetailsEx> getList(Map<String, Long> condition) {

        Long bomId = condition.get("bomId");
        Assert.notEmpty(bomId, "bomId不能为空");
        Long linkId = condition.get("linkId");
        Assert.notEmpty(linkId, "linkId不能为空");

        List<BomDetails> bomDetailsList = list(q -> q.eq(BomDetails::getBomId, bomId).eq(BomDetails::getWorkingRouteProcedureId, linkId));

        List<BomDetailsEx> bomDetailsExList = BeanUtil.copyToList(bomDetailsList, BomDetailsEx.class);

        // 赋值物料属性
        MaterialAttributeAssignUtil
                .init(bomDetailsExList)
                .assignFun(BomDetails::getMaterialId, (entity, material) -> {
                    entity.setMaterialCode(material.getCode());
                    entity.setMaterialName(material.getName());
                    entity.setMaterialUnit(material.getUnit());
                })
                .assignFun(BomDetails::getReplaceMaterialId, (entity, material) -> {
                    entity.setReplaceMaterialCode(material.getCode());
                    entity.setReplaceMaterialName(material.getName());
                    entity.setReplaceMaterialUnit(material.getUnit());
                })
                .execute();

        return bomDetailsExList;
    }

    @Override
    public void add(BomDetailsVo bomDetailsVo) {
        save(bomDetailsVo);
    }

    @Override
    public void edit(BomDetailsVo bomDetailsVo) {
        updateById(bomDetailsVo);
    }

    @Override
    public void delete(BomDetailsVo bomDetailsVo) {
        removeById(bomDetailsVo.getId());
    }

    @Override
    public BomDetails detail(BomDetailsVo bomDetailsVo) {
        BomDetails result = getById(bomDetailsVo.getId());
        return result;
    }

}
