package cn.iocoder.yudao.module.mes.service.paigongrecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.mes.controller.admin.feedingrecord.vo.FeedingRecordRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.feedingrecord.FeedingRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.itemrequisition.ItemRequisitionDetailDO;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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.mes.controller.admin.paigongrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.paigongrecord.PaigongRecordDO;
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 cn.iocoder.yudao.module.mes.dal.mysql.paigongrecord.PaigongRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;

/**
 * 计划派工记录 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class PaigongRecordServiceImpl implements PaigongRecordService {

    @Resource
    private PaigongRecordMapper paigongRecordMapper;

    @Override
    public Long createPaigongRecord(PaigongRecordSaveReqVO createReqVO) {
        // 插入
        PaigongRecordDO paigongRecord = BeanUtils.toBean(createReqVO, PaigongRecordDO.class);
        paigongRecordMapper.insert(paigongRecord);
        // 返回
        return paigongRecord.getId();
    }

    @Override
    public void updatePaigongRecord(PaigongRecordSaveReqVO updateReqVO) {
        // 校验存在
        validatePaigongRecordExists(updateReqVO.getId());
        // 更新
        PaigongRecordDO updateObj = BeanUtils.toBean(updateReqVO, PaigongRecordDO.class);
        paigongRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePaigongRecord(Long id) {
        // 校验存在
        validatePaigongRecordExists(id);
        // 删除
        paigongRecordMapper.deleteById(id);
    }

    private void validatePaigongRecordExists(Long id) {
        if (paigongRecordMapper.selectById(id) == null) {
            throw exception(PAIGONG_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public PaigongRecordDO getPaigongRecord(Long id) {
        return paigongRecordMapper.selectById(id);
    }

    @Override
    public PageResult<PaigongRecordDO> getPaigongRecordPage(PaigongRecordPageReqVO pageReqVO) {
        return paigongRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<PaigongRecordRespVO> getPage(PaigongRecordPageReqVO pageReqVO) {
        PageResult<PaigongRecordDO> pageResult = paigongRecordMapper.selectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(pageResult.getList(), PaigongRecordRespVO.class),pageResult.getTotal());
    }


    // ==================== 子表（派工记录明细） ====================

    @Override
    public List<PaigongRecordDO> getPaigongRecordListByPlanId(Long planId) {
        return paigongRecordMapper.selectListByPlanId(planId);
    }

}