package cn.iocoder.yudao.module.erp.service.mold;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductUnitDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpWarehouseDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.mold.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
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.common.dal.mysql.mold.MoldMapper;

import javax.annotation.Resource;

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.erp.enums.ErrorCodeConstants.*;

/**
 * 模具 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class MoldServiceImpl implements MoldService {

    @Resource
    private MoldMapper moldMapper;

    @Override
    public Long createMold(MoldSaveReqVO createReqVO) {
        // 插入
//        MoldDO mold = BeanUtils.toBean(createReqVO, MoldDO.class);
        String Code = createReqVO.getCode();
        if (StrUtil.isBlank(Code)) {
            throw new ServiceException(ErrorCodeConstants.MOLD_CODE_EMPTY);
        }
        boolean exists = moldMapper.exists(
                new LambdaQueryWrapperX<MoldDO>().eq(MoldDO::getCode, Code)
        );
        if (exists) {
            throw new ServiceException(ErrorCodeConstants.MOLD_CODE_DUPLICATE);
        }
        MoldDO mold = BeanUtils.toBean(createReqVO, MoldDO.class);
        moldMapper.insert(mold);
        // 返回
        return mold.getId();
    }

    @Override
    public void updateMold(MoldSaveReqVO updateReqVO) {
        // 校验存在
        validateMoldExists(updateReqVO.getId());
        // 更新
        MoldDO updateObj = BeanUtils.toBean(updateReqVO, MoldDO.class);
        moldMapper.updateById(updateObj);
    }

    @Override
    public void deleteMold(Long id) {
        // 校验存在
        validateMoldExists(id);
        // 删除
        moldMapper.deleteById(id);
    }

    private void validateMoldExists(Long id) {
        if (moldMapper.selectById(id) == null) {
            throw exception(MOLD_NOT_EXISTS);
        }
    }

    @Override
    public MoldDO getMold(Long id) {
        return moldMapper.selectById(id);
    }

    @Override
    public PageResult<MoldDO> getMoldPage(MoldPageReqVO pageReqVO) {
        return moldMapper.selectPage(pageReqVO, pageReqVO.getBrandId());
    }
    @Override
    public List<MoldDO> getList(Collection<Long> ids) {
        return moldMapper.selectBatchIds(ids);
    }

    @Override
    public List<MoldDO> validMoldList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return moldMapper.selectBatchIds(ids);
    }

    @Override
    public List<MoldRespVO> getMoldVOList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<MoldDO> list = moldMapper.selectBatchIds(ids);
        return buildMoldVOList(list);
    }

    @Override
    public List<MoldRespVO> buildMoldVOList(List<MoldDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BeanUtils.toBean(list, MoldRespVO.class);
    }

    @Override
    public List<MoldDO> getInTransitMoldAllList() {
        return moldMapper.selectList(Wrappers.<MoldDO>lambdaQuery().eq(MoldDO::getStatus,3));
    }

    @Override
    public List<MoldDO> getMoldListByStatus(Long status) {
        LambdaQueryWrapperX<MoldDO> queryWrapper = new LambdaQueryWrapperX<MoldDO>()
                .eqIfPresent(MoldDO::getStatus, status);
        return moldMapper.selectList(queryWrapper);
    }
}