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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.ota.controller.admin.distributortype.vo.DistributorTypePageReqVO;
import com.yc.ship.module.ota.controller.admin.distributortype.vo.DistributorTypeSaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributortype.DistributorTypeDO;
import com.yc.ship.module.ota.dal.mysql.distributortype.DistributorTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

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

/**
 * 分销商类型 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DistributorTypeServiceImpl implements DistributorTypeService {

    @Resource
    private DistributorTypeMapper distributorTypeMapper;

    @Override
    public Long createDistributorType(DistributorTypeSaveReqVO createReqVO) {
        // 插入
        DistributorTypeDO distributorType = BeanUtils.toBean(createReqVO, DistributorTypeDO.class);
        distributorType.setId(IdWorker.getId(distributorType));
        distributorTypeMapper.insert(distributorType);
        // 返回
        return distributorType.getId();
    }

    @Override
    public void updateDistributorType(DistributorTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorTypeExists(updateReqVO.getId());
        // 更新
        DistributorTypeDO updateObj = BeanUtils.toBean(updateReqVO, DistributorTypeDO.class);
        distributorTypeMapper.updateById(updateObj);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        // 校验存在
        validateDistributorTypeExists(id);
        // 更新状态
        DistributorTypeDO updateObj = new DistributorTypeDO();
        updateObj.setId(id);
        updateObj.setIsUse(status);
        distributorTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorType(Long id) {
        // 校验存在
        validateDistributorTypeExists(id);
        // 删除
        distributorTypeMapper.deleteById(id);
    }

    private void validateDistributorTypeExists(Long id) {
        if (distributorTypeMapper.selectById(id) == null) {
            throw exception(DISTRIBUTOR_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public DistributorTypeDO getDistributorType(Long id) {
        return distributorTypeMapper.selectById(id);
    }

    @Override
    public PageResult<DistributorTypeDO> getDistributorTypePage(DistributorTypePageReqVO pageReqVO) {
        return distributorTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Map> getSimpleList() {
        return distributorTypeMapper.getSimpleList();
    }
}