package com.zt.plat.module.qms.office.supplier.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.module.infra.api.businessfile.dto.BusinessFileWithUrlRespDTO;
import com.zt.plat.module.qms.common.service.BusinessFileService;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierExtendRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierSaveReqVO;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierPropertiesDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.office.supplier.dal.mapper.SupplierMapper;

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.module.qms.enums.ErrorCodeConstants.*;

/**
 * 供应商 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Autowired
    private BusinessFileService businessFileService;

    @Override
    public SupplierRespVO createSupplier(SupplierSaveReqVO createReqVO) {
        // 插入
        SupplierDO supplier = BeanUtils.toBean(createReqVO, SupplierDO.class);
        supplierMapper.insert(supplier);
        // 返回
        return BeanUtils.toBean(supplier, SupplierRespVO.class);
    }

    @Override
    public void updateSupplier(SupplierSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierExists(updateReqVO.getId());
        // 更新
        SupplierDO updateObj = BeanUtils.toBean(updateReqVO, SupplierDO.class);
        supplierMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplier(Long id) {
        // 校验存在
        validateSupplierExists(id);
        // 删除
        supplierMapper.deleteById(id);
    }

    @Override
        public void deleteSupplierListByIds(List<Long> ids) {
        // 校验存在
        validateSupplierExists(ids);
        // 删除
        supplierMapper.deleteByIds(ids);
        }

    private void validateSupplierExists(List<Long> ids) {
        List<SupplierDO> list = supplierMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }

    private void validateSupplierExists(Long id) {
        if (supplierMapper.selectById(id) == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    public SupplierExtendRespVO getSupplier(Long id) {
        SupplierExtendRespVO supplier = supplierMapper.selectOneWithCertifications(id);
        List<SupplierPropertiesDO> supplierPropertiesList = supplier.getSupplierPropertiesList();
        if (CollUtil.isNotEmpty(supplierPropertiesList)) {
            List<String> certs = supplierPropertiesList.stream().map(SupplierPropertiesDO::getSubitemCode).toList();
            supplier.setCertifications(certs);
            List<String> certsOfExistFiles = new ArrayList<>();
            for (SupplierPropertiesDO propertiesDO : supplierPropertiesList) {
                CommonResult<List<BusinessFileWithUrlRespDTO>> result = businessFileService.getBusinessFileList(propertiesDO.getId());
                if (CollUtil.isNotEmpty(result.getData()))
                    certsOfExistFiles.add(propertiesDO.getSubitemCode());
            }
            supplier.setCertsOfExistFiles(certsOfExistFiles);
        }
        supplier.setTypes(supplier.getType() != null
                ? List.of(supplier.getType().split("、"))
                : Collections.emptyList());
        return supplier;
    }

    @Override
    public PageResult<SupplierDO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        return supplierMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplierDO> getSuppliersByNames(List<String> supplierNames) {

        return supplierMapper.selectList(Wrappers.lambdaQuery(SupplierDO.class)
                .in(SupplierDO::getName, supplierNames));
    }

}