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

import cn.iocoder.yudao.framework.common.enums.AssetsTypeCodeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.factoryforklift.vo.FactoryForkliftPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift.vo.FactoryForkliftRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift.vo.FactoryForkliftSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryforklift.FactoryForkliftDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.factoryforklift.FactoryForkliftMapper;
import cn.iocoder.yudao.module.system.api.assets.AssetsTypeApi;
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 FactoryForkliftServiceImpl implements FactoryForkliftService {

    @Resource
    private FactoryForkliftMapper factoryForkliftMapper;
    @Resource
    private AssetsTypeApi assetsTypeApi;

    @Override
    public Long createFactoryForklift(FactoryForkliftSaveReqVO createReqVO) {
        // 插入
        FactoryForkliftDO factoryForklift = BeanUtils.toBean(createReqVO, FactoryForkliftDO.class);
        factoryForkliftMapper.insert(factoryForklift);
        // 返回
        return factoryForklift.getId();
    }

    @Override
    public void updateFactoryForklift(FactoryForkliftSaveReqVO updateReqVO) {
        // 更新
        FactoryForkliftDO updateObj = BeanUtils.toBean(updateReqVO, FactoryForkliftDO.class);
        factoryForkliftMapper.updateById(updateObj);
    }

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

    @Override
    public FactoryForkliftDO getFactoryForklift(Long id) {
        return factoryForkliftMapper.selectById(id);
    }

    @Override
    public PageResult<FactoryForkliftRespVO> getFactoryForkliftPage(FactoryForkliftPageReqVO pageReqVO) {
        // -- 通过资产类型中的code - 获取到资产类型的ids - 完了后当作参数传过去
        CommonResult<List<Long>> forklift = assetsTypeApi.getAssetsTypeIdsByCode(AssetsTypeCodeEnum.FORKLIFT);
        pageReqVO.setTypeIds(forklift.getData());
        IPage<FactoryForkliftRespVO> vos = factoryForkliftMapper.getFactoryForkliftPage(pageReqVO, MyBatisUtils.buildPage(pageReqVO));
        return new PageResult<>(vos.getRecords(), vos.getTotal());
    }

}
