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

import cn.iocoder.yudao.framework.common.enums.AssetsTypeCodeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.system.api.assets.AssetsTypeApi;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.smartfactory.controller.admin.linefixedassets.vo.*;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.linefixedassets.LineFixedAssetsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.iocoder.yudao.module.smartfactory.dal.mysql.linefixedassets.LineFixedAssetsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.*;

/**
 * 打包线固定资产 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class LineFixedAssetsServiceImpl extends ServiceImpl<LineFixedAssetsMapper, LineFixedAssetsDO> implements LineFixedAssetsService {

    @Resource
    private LineFixedAssetsMapper lineFixedAssetsMapper;
    @Resource
    private AssetsTypeApi assetsTypeApi;

    @Override
    public Long createLineFixedAssets(LineFixedAssetsSaveReqVO createReqVO) {
        // 插入
        LineFixedAssetsDO lineFixedAssets = BeanUtils.toBean(createReqVO, LineFixedAssetsDO.class);
        lineFixedAssetsMapper.insert(lineFixedAssets);
        // 返回
        return lineFixedAssets.getId();
    }

    @Override
    public void updateLineFixedAssets(LineFixedAssetsSaveReqVO updateReqVO) {
        // 更新
        LineFixedAssetsDO updateObj = BeanUtils.toBean(updateReqVO, LineFixedAssetsDO.class);
        lineFixedAssetsMapper.updateById(updateObj);
    }

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


    @Override
    public LineFixedAssetsDO getLineFixedAssets(Long id) {
        return lineFixedAssetsMapper.selectById(id);
    }

    @Override
    public PageResult<LineFixedAssetsDO> getLineFixedAssetsPage(LineFixedAssetsPageReqVO pageReqVO) {
        CommonResult<List<Long>> forklift = assetsTypeApi.getAssetsTypeIdsByCode(AssetsTypeCodeEnum.PRODUCTION_MACHINERY);
        pageReqVO.setTypeIds(forklift.getData());
        IPage<LineFixedAssetsDO> page = lineFixedAssetsMapper.getLineFixedAssetsPage(MyBatisUtils.buildPage(pageReqVO),pageReqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

}
