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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.zt.plat.framework.common.exception.ServiceException;
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.MaterialCorrelationAssayPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialCorrelationAssayRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialCorrelationAssaySaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialBatchDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialCorrelationAssayDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialCorrelationAssayMapper;
import com.zt.plat.module.qms.resource.material.enums.MaterialAssayResultAssessmentType;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.qms.enums.ErrorCodeConstants.MATERIAL_CORRELATION_ASSAY_NOT_EXISTS;

/**
 * 物料检化验关联信息 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialCorrelationAssayServiceImpl implements MaterialCorrelationAssayService {

    @Resource
    private MaterialCorrelationAssayMapper materialCorrelationAssayMapper;

    @Override
    public MaterialCorrelationAssayRespVO createMaterialCorrelationAssay(MaterialCorrelationAssaySaveReqVO createReqVO) {
        // 插入
        MaterialCorrelationAssayDO materialCorrelationAssay = BeanUtils.toBean(createReqVO, MaterialCorrelationAssayDO.class);
        materialCorrelationAssayMapper.insert(materialCorrelationAssay);
        // 返回
        return BeanUtils.toBean(materialCorrelationAssay, MaterialCorrelationAssayRespVO.class);
    }

    @Transactional
    @Override
    public void updateMaterialCorrelationAssay(MaterialCorrelationAssaySaveReqVO updateReqVO) {
        // 校验存在
        MaterialCorrelationAssayDO assayDO = materialCorrelationAssayMapper.selectById(updateReqVO.getId());
        if (assayDO == null) throw exception(MATERIAL_CORRELATION_ASSAY_NOT_EXISTS);
        // 更新
        MaterialCorrelationAssayDO updateObj = BeanUtils.toBean(updateReqVO, MaterialCorrelationAssayDO.class);
        String qualified = updateObj.getQualified();
        if (StrUtil.isBlank(qualified)) throw new ServiceException(1_032_160_000, "判定结果不能为空");
        materialCorrelationAssayMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialCorrelationAssay(Long id) {
        // 校验存在
        validateMaterialCorrelationAssayExists(id);
        // 删除
        materialCorrelationAssayMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialCorrelationAssayListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialCorrelationAssayExists(ids);
        // 删除
        materialCorrelationAssayMapper.deleteByIds(ids);
        }

    private void validateMaterialCorrelationAssayExists(List<Long> ids) {
        List<MaterialCorrelationAssayDO> list = materialCorrelationAssayMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_CORRELATION_ASSAY_NOT_EXISTS);
        }
    }

    private void validateMaterialCorrelationAssayExists(Long id) {
        if (materialCorrelationAssayMapper.selectById(id) == null) {
            throw exception(MATERIAL_CORRELATION_ASSAY_NOT_EXISTS);
        }
    }

    @Override
    public MaterialCorrelationAssayDO getMaterialCorrelationAssay(Long id) {
        return materialCorrelationAssayMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialCorrelationAssayDO> getMaterialCorrelationAssayPage(MaterialCorrelationAssayPageReqVO pageReqVO) {
        return materialCorrelationAssayMapper.selectPage(pageReqVO);
    }

    @Override
    public void save(MaterialCorrelationAssayDO assayDO) {
        materialCorrelationAssayMapper.insert(assayDO);
    }

    @Override
    public void saveBatch(List<MaterialCorrelationAssayDO> assayDOS) {
        materialCorrelationAssayMapper.insertBatch(assayDOS);
    }

    @Override
    public List<MaterialCorrelationAssayDO> getListByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) return List.of();
        return materialCorrelationAssayMapper.selectByIds(ids);
    }

    @Override
    public void updateBatch(List<MaterialCorrelationAssayDO> needUpdateAssays) {
        if (CollUtil.isEmpty(needUpdateAssays)) return;
        materialCorrelationAssayMapper.updateBatch(needUpdateAssays);
    }

    @Override
    public List<MaterialCorrelationAssayRespVO> getRespListByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) return List.of();
        return materialCorrelationAssayMapper.selectRespListByIds(ids);
    }

    @Override
    public MaterialCorrelationAssayDO getByEntrustId(Long entrustId) {

        return materialCorrelationAssayMapper.selectOne(MaterialCorrelationAssayDO::getEntrustId, entrustId);
    }

    @Override
    public void updateById(MaterialCorrelationAssayDO assay) {
        materialCorrelationAssayMapper.updateById(assay);
    }

}