package com.yc.ship.module.ota.service.distributorcategory;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.ota.controller.admin.distributorcategory.vo.DistributorCategoryPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorcategory.vo.DistributorCategorySaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributorcategory.DistributorCategoryDO;
import com.yc.ship.module.ota.dal.mysql.distributorcategory.DistributorCategoryMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;


import com.yc.ship.framework.common.util.object.BeanUtils;


import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.ota.enums.ErrorCodeConstants.DISTRIBUTOR_CATEGORY_NOT_EXISTS;

/**
 * 供应商分类 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DistributorCategoryServiceImpl implements DistributorCategoryService {

    @Resource
    private DistributorCategoryMapper distributorCategoryMapper;

    @Override
    public Long createDistributorCategory(DistributorCategorySaveReqVO createReqVO) {
        // 插入
        DistributorCategoryDO distributorCategory = BeanUtils.toBean(createReqVO, DistributorCategoryDO.class);
        distributorCategory.setId(IdWorker.getId(distributorCategory));
        distributorCategoryMapper.insert(distributorCategory);
        // 返回
        return distributorCategory.getId();
    }

    @Override
    public void updateDistributorCategory(DistributorCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorCategoryExists(updateReqVO.getId());
        // 更新
        DistributorCategoryDO updateObj = BeanUtils.toBean(updateReqVO, DistributorCategoryDO.class);
        distributorCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorCategory(Long id) {
        // 校验存在
        validateDistributorCategoryExists(id);
        // 删除
        distributorCategoryMapper.deleteById(id);
    }

    private void validateDistributorCategoryExists(Long id) {
        if (distributorCategoryMapper.selectById(id) == null) {
            throw exception(DISTRIBUTOR_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public DistributorCategoryDO getDistributorCategory(Long id) {
        return distributorCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<DistributorCategoryDO> getDistributorCategoryPage(DistributorCategoryPageReqVO pageReqVO) {
        return distributorCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        // 校验存在
        validateDistributorCategoryExists(id);
        // 更新状态
        DistributorCategoryDO updateObj = new DistributorCategoryDO();
        updateObj.setId(id);
        updateObj.setUseStatus(status);
        distributorCategoryMapper.updateById(updateObj);
    }

    @Override
    public List<DistributorCategoryDO> getCategoryList() {
        return  distributorCategoryMapper.selectList(new LambdaQueryWrapperX<DistributorCategoryDO>()
                .eq(DistributorCategoryDO::getUseStatus, 1)
                .eq(DistributorCategoryDO::getDeleted, 0));
    }

}