package com.zt.plat.module.base.service.erpmonthproductiveplancheck;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckDetailPageReqVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckDetailRespVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckDetailSaveReqVO;
import com.zt.plat.module.base.dal.dao.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckDetailMapper;
import com.zt.plat.module.base.dal.dataobject.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckDetailDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erpmonthproductiveplancheck.ErrorCodeConstants.ERP_MONTH_PRODUCTIVE_PLAN_CHECK_DETAIL_NOT_EXISTS;


/**
 * ERP月度产品计划核对明细 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class ErpMonthProductivePlanCheckDetailServiceImpl implements ErpMonthProductivePlanCheckDetailService {

    @Resource
    private ErpMonthProductivePlanCheckDetailMapper erpMonthProductivePlanCheckDetailMapper;

    @Override
    public ErpMonthProductivePlanCheckDetailRespVO createErpMonthProductivePlanCheckDetail(ErpMonthProductivePlanCheckDetailSaveReqVO createReqVO) {
        // 插入
        ErpMonthProductivePlanCheckDetailDO erpMonthProductivePlanCheckDetail = BeanUtils.toBean(createReqVO, ErpMonthProductivePlanCheckDetailDO.class);
        erpMonthProductivePlanCheckDetailMapper.insert(erpMonthProductivePlanCheckDetail);
        // 返回
        return BeanUtils.toBean(erpMonthProductivePlanCheckDetail, ErpMonthProductivePlanCheckDetailRespVO.class);
    }

    @Override
    public List<ErpMonthProductivePlanCheckDetailRespVO> createErpMonthProductivePlanCheckDetail(List<ErpMonthProductivePlanCheckDetailSaveReqVO> createReqVOS) {
        if (CollUtil.isEmpty(createReqVOS)) {
            return List.of();
        }
        List<ErpMonthProductivePlanCheckDetailDO> erpMonthProductivePlanCheckDetails = BeanUtils.toBean(createReqVOS, ErpMonthProductivePlanCheckDetailDO.class);
        erpMonthProductivePlanCheckDetailMapper.insertBatch(erpMonthProductivePlanCheckDetails);
        return BeanUtils.toBean(erpMonthProductivePlanCheckDetails, ErpMonthProductivePlanCheckDetailRespVO.class);
    }

    @Override
    public void updateErpMonthProductivePlanCheckDetail(ErpMonthProductivePlanCheckDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateErpMonthProductivePlanCheckDetailExists(updateReqVO.getId());
        // 更新
        ErpMonthProductivePlanCheckDetailDO updateObj = BeanUtils.toBean(updateReqVO, ErpMonthProductivePlanCheckDetailDO.class);
        erpMonthProductivePlanCheckDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpMonthProductivePlanCheckDetail(Long id) {
        // 校验存在
        validateErpMonthProductivePlanCheckDetailExists(id);
        // 删除
        erpMonthProductivePlanCheckDetailMapper.deleteById(id);
    }

    @Override
        public void deleteErpMonthProductivePlanCheckDetailListByIds(List<Long> ids) {
        // 校验存在
        validateErpMonthProductivePlanCheckDetailExists(ids);
        // 删除
        erpMonthProductivePlanCheckDetailMapper.deleteByIds(ids);
        }

    private void validateErpMonthProductivePlanCheckDetailExists(List<Long> ids) {
        List<ErpMonthProductivePlanCheckDetailDO> list = erpMonthProductivePlanCheckDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ERP_MONTH_PRODUCTIVE_PLAN_CHECK_DETAIL_NOT_EXISTS);
        }
    }

    private void validateErpMonthProductivePlanCheckDetailExists(Long id) {
        if (erpMonthProductivePlanCheckDetailMapper.selectById(id) == null) {
            throw exception(ERP_MONTH_PRODUCTIVE_PLAN_CHECK_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ErpMonthProductivePlanCheckDetailDO getErpMonthProductivePlanCheckDetail(Long id) {
        return erpMonthProductivePlanCheckDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ErpMonthProductivePlanCheckDetailDO> getErpMonthProductivePlanCheckDetailPage(ErpMonthProductivePlanCheckDetailPageReqVO pageReqVO) {
        return erpMonthProductivePlanCheckDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ErpMonthProductivePlanCheckDetailRespVO> getErpMonthProductivePlanCheckDetailListByMainId(Long mainId) {

        return BeanUtils.toBean(erpMonthProductivePlanCheckDetailMapper.selectList(ErpMonthProductivePlanCheckDetailDO::getMainId, mainId), ErpMonthProductivePlanCheckDetailRespVO.class);
    }

}