package com.yc.ship.module.resource.service.suppliercategory;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.suppliercategory.vo.SupplierCategoryPageReqVO;
import com.yc.ship.module.resource.controller.admin.suppliercategory.vo.SupplierCategorySaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.suppliercategory.SupplierCategoryDO;
import com.yc.ship.module.resource.dal.mysql.suppliercategory.SupplierCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.SUPPLIER_CATEGORY_NOT_EXISTS;


/**
 * 资源供应商与资源类型关联 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplierCategoryServiceImpl implements SupplierCategoryService {

    @Resource
    private SupplierCategoryMapper supplierCategoryMapper;

    @Override
    public Long createSupplierCategory(SupplierCategorySaveReqVO createReqVO) {
        // 插入
        SupplierCategoryDO supplierCategory = BeanUtils.toBean(createReqVO, SupplierCategoryDO.class);
        supplierCategoryMapper.insert(supplierCategory);
        // 返回
        return supplierCategory.getSupplierId();
    }

    @Override
    public void updateSupplierCategory(SupplierCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierCategoryExists(updateReqVO.getSupplierId(), updateReqVO.getCategoryId());
        // 更新
        SupplierCategoryDO updateObj = BeanUtils.toBean(updateReqVO, SupplierCategoryDO.class);
        supplierCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplierCategory(Long supplierId) {
        // 校验存在
//        validateSupplierCategoryExists(id);
        // 删除
        supplierCategoryMapper.delete(SupplierCategoryDO::getSupplierId, supplierId);
    }

    private void validateSupplierCategoryExists(Long supplierId, Long categoryId) {
        if (supplierCategoryMapper.selectOne(SupplierCategoryDO::getSupplierId, supplierId, SupplierCategoryDO::getCategoryId, categoryId) == null) {
            throw exception(SUPPLIER_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public SupplierCategoryDO getSupplierCategory(Long id) {
        return supplierCategoryMapper.selectById(id);
    }

    @Override
    public List<SupplierCategoryDO> getSupplierCategoryList(Long id) {
        return supplierCategoryMapper.selectList(SupplierCategoryDO::getSupplierId, id);
    }

    @Override
    public PageResult<SupplierCategoryDO> getSupplierCategoryPage(SupplierCategoryPageReqVO pageReqVO) {
        return supplierCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<Long> resourceList, Long supplierId) {
        for (Long resource : resourceList) {
            SupplierCategoryDO categoryDO = new SupplierCategoryDO();
            categoryDO.setCategoryId(resource);
            categoryDO.setSupplierId(supplierId);
            supplierCategoryMapper.insert(categoryDO);
        }
    }
}