package cn.iocoder.yudao.module.smartfactory.service.factorypackingline;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLinePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLineRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLineSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorypackingline.FactoryPackingLineDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.factorypackingline.FactoryPackingLineMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 打包线配置 Service 实现类
 *
 * @author 姚君
 */
@Service
@Validated
public class FactoryPackingLineServiceImpl implements FactoryPackingLineService {

    @Resource
    private FactoryPackingLineMapper factoryPackingLineMapper;

    @Override
    public Long createFactoryPackingLine(FactoryPackingLineSaveReqVO createReqVO) {
        // 插入
        FactoryPackingLineDO factoryPackingLine = BeanUtils.toBean(createReqVO, FactoryPackingLineDO.class);
        factoryPackingLineMapper.insert(factoryPackingLine);
        // 返回
        return factoryPackingLine.getId();
    }

    @Override
    public void updateFactoryPackingLine(FactoryPackingLineSaveReqVO updateReqVO) {
        // 更新
        FactoryPackingLineDO updateObj = BeanUtils.toBean(updateReqVO, FactoryPackingLineDO.class);
        factoryPackingLineMapper.updateById(updateObj);
    }

    @Override
    public void deleteFactoryPackingLine(Long id) {
        // 删除
        factoryPackingLineMapper.deleteById(id);
    }

    @Override
    public FactoryPackingLineDO getFactoryPackingLine(Long id) {
        return factoryPackingLineMapper.selectById(id);
    }

    @Override
    public PageResult<FactoryPackingLineRespVO> getFactoryPackingLinePage(FactoryPackingLinePageReqVO pageReqVO) {
        IPage<FactoryPackingLineRespVO> pageResult = factoryPackingLineMapper.getFactoryPackingLinePage(pageReqVO, MyBatisUtils.buildPage(pageReqVO));
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());

    }

    @Override
    public List<FactoryPackingLineDO> getFactoryPackingLineByFactoryId(List<Long> factoryIds) {
        return factoryPackingLineMapper.selectList(new LambdaQueryWrapper<FactoryPackingLineDO>()
                .in(CollectionUtil.isNotEmpty(factoryIds), FactoryPackingLineDO::getFactoryId, factoryIds));
    }

}
