package com.yc.ship.module.miniapplet.service.problemtype;

import cn.hutool.core.collection.CollUtil;
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.miniapplet.controller.admin.problemtype.vo.ProblemTypePageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypeSaveReqVO;
import com.yc.ship.module.miniapplet.controller.app.problem.vo.AppProblemReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.problemtype.ProblemTypeDO;
import com.yc.ship.module.miniapplet.dal.mysql.problemtype.ProblemTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.miniapplet.enums.ErrorCodeConstants.PROBLEM_TYPE_NOT_EXISTS;


/**
 * 问题类型 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProblemTypeServiceImpl implements ProblemTypeService {

    @Resource
    private ProblemTypeMapper problemTypeMapper;

    @Override
    public Long createProblemType(ProblemTypeSaveReqVO createReqVO) {
        // 插入
        ProblemTypeDO problemType = BeanUtils.toBean(createReqVO, ProblemTypeDO.class);
        problemType.setId(IdWorker.getId(problemType));
        problemTypeMapper.insert(problemType);
        // 返回
        return problemType.getId();
    }

    @Override
    public void updateProblemType(ProblemTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateProblemTypeExists(updateReqVO.getId());
        // 更新
        ProblemTypeDO updateObj = BeanUtils.toBean(updateReqVO, ProblemTypeDO.class);
        problemTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteProblemType(Long id) {
        // 校验存在
        validateProblemTypeExists(id);
        // 删除
        problemTypeMapper.deleteById(id);
    }

    private void validateProblemTypeExists(Long id) {
        if (problemTypeMapper.selectById(id) == null) {
            throw exception(PROBLEM_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ProblemTypeDO getProblemType(Long id) {
        return problemTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ProblemTypeDO> getProblemTypePage(ProblemTypePageReqVO pageReqVO) {
        return problemTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProblemTypeDO> getProblemTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return problemTypeMapper.selectBatchIds(ids);
    }

    @Override
    public List<ProblemTypeDO> getProblemTypeListByStatus(Integer systemType,Integer status) {
        return problemTypeMapper.getProblemTypeListByStatus(systemType,status);
    }

    @Override
    public List<ProblemTypeDO> getProblemTypeListByApp(AppProblemReqVO reqVO) {
        return problemTypeMapper.getProblemTypeListByApp(reqVO);
    }

}
