package com.zt.plat.module.qms.resource.material.service;

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.qms.resource.material.controller.vo.MaterialInventoryInboundDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInfomationDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryInboundDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryInboundDetailDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialInventoryInboundDetailMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
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.qms.enums.ErrorCodeConstants.MATERIAL_INVENTORY_INBOUND_DETAIL_NOT_EXISTS;

/**
 * 入库明细,验收入库、盘盈入库等 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialInventoryInboundDetailServiceImpl implements MaterialInventoryInboundDetailService {

    @Resource
    private MaterialInventoryInboundDetailMapper materialInventoryInboundDetailMapper;

    @Override
    public MaterialInventoryInboundDetailRespVO createMaterialInventoryInboundDetail(MaterialInventoryInboundDetailSaveReqVO createReqVO) {
        // 插入
        MaterialInventoryInboundDetailDO materialInventoryInboundDetail = BeanUtils.toBean(createReqVO, MaterialInventoryInboundDetailDO.class);
        materialInventoryInboundDetailMapper.insert(materialInventoryInboundDetail);
        // 返回
        return BeanUtils.toBean(materialInventoryInboundDetail, MaterialInventoryInboundDetailRespVO.class);
    }

    @Override
    public void updateMaterialInventoryInboundDetail(MaterialInventoryInboundDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialInventoryInboundDetailExists(updateReqVO.getId());
        // 更新
        MaterialInventoryInboundDetailDO updateObj = BeanUtils.toBean(updateReqVO, MaterialInventoryInboundDetailDO.class);
        materialInventoryInboundDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialInventoryInboundDetail(Long id) {
        // 校验存在
        validateMaterialInventoryInboundDetailExists(id);
        // 删除
        materialInventoryInboundDetailMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialInventoryInboundDetailListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialInventoryInboundDetailExists(ids);
        // 删除
        materialInventoryInboundDetailMapper.deleteByIds(ids);
        }

    private void validateMaterialInventoryInboundDetailExists(List<Long> ids) {
        List<MaterialInventoryInboundDetailDO> list = materialInventoryInboundDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_INVENTORY_INBOUND_DETAIL_NOT_EXISTS);
        }
    }

    private void validateMaterialInventoryInboundDetailExists(Long id) {
        if (materialInventoryInboundDetailMapper.selectById(id) == null) {
            throw exception(MATERIAL_INVENTORY_INBOUND_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialInventoryInboundDetailDO getMaterialInventoryInboundDetail(Long id) {
        return materialInventoryInboundDetailMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialInventoryInboundDetailDO> getMaterialInventoryInboundDetailPage(MaterialInventoryInboundDetailPageReqVO pageReqVO) {
        return materialInventoryInboundDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<MaterialInventoryInboundDetailDO> detailList) {
        materialInventoryInboundDetailMapper.insertBatch(detailList);
    }


    @Override
    public PageResult<MaterialInventoryInboundDetailRespVO> getInventoryInboundDetailPageWithMaterialInfo(MaterialInventoryInboundDetailPageReqVO pageReqVO) {

        return materialInventoryInboundDetailMapper.selectPageWithMaterialInfo(pageReqVO);
    }

    @Override
    public List<MaterialInventoryInboundDetailRespVO> getMaterialInfoListByInboundId(Long inboundId) {

        return materialInventoryInboundDetailMapper.selectMaterialInfoListByInboundId(inboundId);
    }

    @Override
    public void saveInboundDetails(List<MaterialInfomationDO> infomationDOS, MaterialInventoryInboundDO inbound, Long batchId, Long gongduanId) {
        List<MaterialInventoryInboundDetailDO> detailList = new ArrayList<>();
        for (MaterialInfomationDO infomationDO : infomationDOS) {
            MaterialInventoryInboundDetailDO detail = new MaterialInventoryInboundDetailDO();
            detail
                    .setInboundId(inbound.getId())
                    .setBatchId(batchId)
                    .setBatchGongduanId(gongduanId)
                    .setMaterialInfomationId(infomationDO.getId())
                    .setInboundUserName(inbound.getApplyUser())
                    .setInboundUserId(inbound.getApplyUserId())
                    .setInboundDepartmentName(inbound.getApplyDepartment())
                    .setInboundDepartmentId(inbound.getApplyDepartmentId())
                    .setInboundTime(inbound.getApplyTime());
            detailList.add(detail);
        }
        this.saveBatch(detailList);
    }

}