package com.zt.plat.module.contractorder.service.originalmaterialprocessing;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.base.dal.dataobject.base.ElementDO;
import com.zt.plat.module.base.service.base.ElementService;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailRespVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.entrustorder.EntrustOrderDetailDO;
import com.zt.plat.module.contractorder.dal.dataobject.originalmaterialprocessing.OriginalMaterialProcessingDetailDO;
import com.zt.plat.module.contractorder.dal.mysql.originalmaterialprocessing.OriginalMaterialProcessingDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.contractorder.enums.originalmaterialprocessing.ErrorCodeConstants.ORIGINAL_MATERIAL_PROCESSING_DETAIL_NOT_EXISTS;


/**
 * 来料加工订单明细 Service 实现类
 *
 * @author 易门采购员测试账号
 */
@Service
@Validated
@Slf4j
public class OriginalMaterialProcessingDetailServiceImpl implements OriginalMaterialProcessingDetailService {

    @Resource
    private OriginalMaterialProcessingDetailMapper originalMaterialProcessingDetailMapper;
    @Resource
    private ElementService elementService;

    @Override
    public OriginalMaterialProcessingDetailRespVO createOriginalMaterialProcessingDetail(OriginalMaterialProcessingDetailSaveReqVO createReqVO) {
        // 插入
        OriginalMaterialProcessingDetailDO originalMaterialProcessingDetail = BeanUtils.toBean(createReqVO, OriginalMaterialProcessingDetailDO.class);
        setElemInfo(originalMaterialProcessingDetail);
        originalMaterialProcessingDetailMapper.insert(originalMaterialProcessingDetail);
        // 返回
        return BeanUtils.toBean(originalMaterialProcessingDetail, OriginalMaterialProcessingDetailRespVO.class);
    }

    @Override
    public void updateOriginalMaterialProcessingDetail(OriginalMaterialProcessingDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateOriginalMaterialProcessingDetailExists(updateReqVO.getId());
        // 更新
        OriginalMaterialProcessingDetailDO updateObj = BeanUtils.toBean(updateReqVO, OriginalMaterialProcessingDetailDO.class);
        setElemInfo(updateObj);
        originalMaterialProcessingDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteOriginalMaterialProcessingDetail(Long id) {
        // 校验存在
        validateOriginalMaterialProcessingDetailExists(id);
        // 删除
        originalMaterialProcessingDetailMapper.deleteById(id);
    }

    @Override
        public void deleteOriginalMaterialProcessingDetailListByIds(List<Long> ids) {
        // 校验存在
        validateOriginalMaterialProcessingDetailExists(ids);
        // 删除
        originalMaterialProcessingDetailMapper.deleteByIds(ids);
        }

    private void validateOriginalMaterialProcessingDetailExists(List<Long> ids) {
        List<OriginalMaterialProcessingDetailDO> list = originalMaterialProcessingDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ORIGINAL_MATERIAL_PROCESSING_DETAIL_NOT_EXISTS);
        }
    }

    private void validateOriginalMaterialProcessingDetailExists(Long id) {
        if (originalMaterialProcessingDetailMapper.selectById(id) == null) {
            throw exception(ORIGINAL_MATERIAL_PROCESSING_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public OriginalMaterialProcessingDetailDO getOriginalMaterialProcessingDetail(Long id) {
        return originalMaterialProcessingDetailMapper.selectById(id);
    }

    @Override
    public PageResult<OriginalMaterialProcessingDetailDO> getOriginalMaterialProcessingDetailPage(OriginalMaterialProcessingDetailPageReqVO pageReqVO) {
        return originalMaterialProcessingDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OriginalMaterialProcessingDetailRespVO> insertBatchSomeColumn(List<OriginalMaterialProcessingDetailSaveReqVO> originalMaterialProcessingDetailSaveReqVOS) {
        List<OriginalMaterialProcessingDetailDO> originalMaterialProcessingDetails = BeanUtils.toBean(originalMaterialProcessingDetailSaveReqVOS, OriginalMaterialProcessingDetailDO.class);
        originalMaterialProcessingDetails.forEach(this::setElemInfo);
        originalMaterialProcessingDetailMapper.insertBatch(originalMaterialProcessingDetails);
        return BeanUtils.toBean(originalMaterialProcessingDetails, OriginalMaterialProcessingDetailRespVO.class);
    }

    @Override
    public void updateBatchSomeColumn(List<OriginalMaterialProcessingDetailSaveReqVO> originalMaterialProcessingDetailSaveReqVOS) {
        List<OriginalMaterialProcessingDetailDO> updateList = BeanUtils.toBean(originalMaterialProcessingDetailSaveReqVOS, OriginalMaterialProcessingDetailDO.class);
        updateList.forEach(this::setElemInfo);
        originalMaterialProcessingDetailMapper.insertOrUpdate(updateList);
    }

    @Override
    public List<OriginalMaterialProcessingDetailRespVO> getOriginalMaterialProcessingDetailListByOrderId(Long orderId) {
        List<OriginalMaterialProcessingDetailDO> originalMaterialProcessingDetails = originalMaterialProcessingDetailMapper.selectList(OriginalMaterialProcessingDetailDO::getOrderId, orderId);
        return BeanUtils.toBean(originalMaterialProcessingDetails, OriginalMaterialProcessingDetailRespVO.class);
    }

    @Override
    public void deleteOriginalMaterialProcessingDetailByOrderIds(List<Long> orderIds) {
        originalMaterialProcessingDetailMapper.delete(OriginalMaterialProcessingDetailDO::getOrderId,orderIds);
    }
    private void setElemInfo(OriginalMaterialProcessingDetailDO originalMaterialProcessingDetailDO) {
        if (originalMaterialProcessingDetailDO.getElementAbbreviation() == null) {
            return;
        }
        ElementDO elementBySymbol = elementService.getElementBySymbol(originalMaterialProcessingDetailDO.getElementAbbreviation());
        if (elementBySymbol != null) {
            originalMaterialProcessingDetailDO.setElementName(elementBySymbol.getName());
            originalMaterialProcessingDetailDO.setElementNumber(elementBySymbol.getCoding());
        } else {
            log.warn("元素【{}】不存在,订单ID【{}】", originalMaterialProcessingDetailDO.getElementAbbreviation(), originalMaterialProcessingDetailDO.getOrderId());
        }

    }

}