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.ErpMonthProductivePlanCheckDetailRespVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckPageReqVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckRespVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckSaveReqVO;
import com.zt.plat.module.base.dal.dao.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckMapper;
import com.zt.plat.module.base.dal.dataobject.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

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


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

    @Resource
    private ErpMonthProductivePlanCheckMapper erpMonthProductivePlanCheckMapper;


    @Resource
    private ErpMonthProductivePlanCheckDetailService erpMonthProductivePlanCheckDetailService;

    @Override
    public ErpMonthProductivePlanCheckRespVO createErpMonthProductivePlanCheck(ErpMonthProductivePlanCheckSaveReqVO createReqVO) {
        // 插入
        ErpMonthProductivePlanCheckDO erpMonthProductivePlanCheck = BeanUtils.toBean(createReqVO, ErpMonthProductivePlanCheckDO.class);
        erpMonthProductivePlanCheckMapper.insert(erpMonthProductivePlanCheck);
        List<ErpMonthProductivePlanCheckDetailRespVO> erpMonthProductivePlanCheckDetail = new ArrayList<>();
        if (createReqVO.getDetails() != null && !createReqVO.getDetails().isEmpty()) {
            createReqVO.getDetails().forEach(detail -> detail.setMainId(erpMonthProductivePlanCheck.getId()));
            erpMonthProductivePlanCheckDetail = erpMonthProductivePlanCheckDetailService.createErpMonthProductivePlanCheckDetail(createReqVO.getDetails());
        }
        // 返回
        ErpMonthProductivePlanCheckRespVO erpMonthProductivePlanCheckRespVO = BeanUtils.toBean(erpMonthProductivePlanCheck, ErpMonthProductivePlanCheckRespVO.class);
        erpMonthProductivePlanCheckRespVO.setDetails(erpMonthProductivePlanCheckDetail);
        return erpMonthProductivePlanCheckRespVO;
    }

    @Override
    public void updateErpMonthProductivePlanCheck(ErpMonthProductivePlanCheckSaveReqVO updateReqVO) {
        // 校验存在
        validateErpMonthProductivePlanCheckExists(updateReqVO.getId());
        // 更新
        ErpMonthProductivePlanCheckDO updateObj = BeanUtils.toBean(updateReqVO, ErpMonthProductivePlanCheckDO.class);
        erpMonthProductivePlanCheckMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpMonthProductivePlanCheck(Long id) {
        // 校验存在
        validateErpMonthProductivePlanCheckExists(id);
        // 删除
        erpMonthProductivePlanCheckMapper.deleteById(id);
    }

    @Override
    public void deleteErpMonthProductivePlanCheckListByIds(List<Long> ids) {
        // 校验存在
        validateErpMonthProductivePlanCheckExists(ids);
        // 删除
        erpMonthProductivePlanCheckMapper.deleteByIds(ids);
    }

    private void validateErpMonthProductivePlanCheckExists(List<Long> ids) {
        List<ErpMonthProductivePlanCheckDO> list = erpMonthProductivePlanCheckMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ERP_MONTH_PRODUCTIVE_PLAN_CHECK_NOT_EXISTS);
        }
    }

    private void validateErpMonthProductivePlanCheckExists(Long id) {
        if (erpMonthProductivePlanCheckMapper.selectById(id) == null) {
            throw exception(ERP_MONTH_PRODUCTIVE_PLAN_CHECK_NOT_EXISTS);
        }
    }

    @Override
    public ErpMonthProductivePlanCheckDO getErpMonthProductivePlanCheck(Long id) {
        return erpMonthProductivePlanCheckMapper.selectById(id);
    }

    @Override
    public PageResult<ErpMonthProductivePlanCheckDO> getErpMonthProductivePlanCheckPage(ErpMonthProductivePlanCheckPageReqVO pageReqVO) {
        return erpMonthProductivePlanCheckMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBatchErpMonthProductivePlanCheck(List<ErpMonthProductivePlanCheckSaveReqVO> createReqVOList) {
        if (createReqVOList == null || createReqVOList.isEmpty()) {
            throw exception(DATA_NOT_ERROR);
        }
        createReqVOList.forEach(createReqVO -> {
            ErpMonthProductivePlanCheckDO erpMonthProductivePlanCheck = BeanUtils.toBean(createReqVO, ErpMonthProductivePlanCheckDO.class);
            erpMonthProductivePlanCheckMapper.insert(erpMonthProductivePlanCheck);
            if (createReqVO.getDetails() != null && !createReqVO.getDetails().isEmpty()) {
                createReqVO.getDetails().forEach(detail -> detail.setMainId(erpMonthProductivePlanCheck.getId()));
                erpMonthProductivePlanCheckDetailService.createErpMonthProductivePlanCheckDetail(createReqVO.getDetails());
            }
        });
    }

    @Override
    public List<ErpMonthProductivePlanCheckDetailRespVO> getErpMonthProductivePlanCheckDetail(Long id) {
        return setErpMonthProductivePlanCheckDetailData(id);
    }

   private List<ErpMonthProductivePlanCheckDetailRespVO> setErpMonthProductivePlanCheckDetailData(Long id){
        if (id == null){
            return new ArrayList<>();
        }
        return erpMonthProductivePlanCheckDetailService.getErpMonthProductivePlanCheckDetailListByMainId(id);
   }

}