package cn.iocoder.yudao.module.member.service.storecomplain;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.storecomplain.vo.StoreComplainPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storecomplain.vo.StoreComplainSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.storecomplain.vo.AppStoreComplainSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storecomplain.StoreComplainDO;
import cn.iocoder.yudao.module.member.dal.mysql.storecomplain.StoreComplainMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.STORE_COMPLAIN_NOT_EXISTS;

/**
 * 投诉 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class StoreComplainServiceImpl implements StoreComplainService {

    @Resource
    private StoreComplainMapper storeComplainMapper;

    @Override
    public Long createStoreComplain(StoreComplainSaveReqVO createReqVO) {
        // 插入
        StoreComplainDO storeComplain = BeanUtils.toBean(createReqVO, StoreComplainDO.class);
        storeComplainMapper.insert(storeComplain);
        // 返回
        return storeComplain.getId();
    }

    @Override
    public void updateStoreComplain(StoreComplainSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreComplainExists(updateReqVO.getId());
        // 更新
        StoreComplainDO updateObj = BeanUtils.toBean(updateReqVO, StoreComplainDO.class);
        storeComplainMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreComplain(Long id) {
        // 校验存在
        validateStoreComplainExists(id);
        // 删除
        storeComplainMapper.deleteById(id);
    }

    private void validateStoreComplainExists(Long id) {
        if (storeComplainMapper.selectById(id) == null) {
            throw exception(STORE_COMPLAIN_NOT_EXISTS);
        }
    }

    @Override
    public StoreComplainDO getStoreComplain(Long id) {
        return storeComplainMapper.selectById(id);
    }

    @Override
    public PageResult<StoreComplainDO> getStoreComplainPage(StoreComplainPageReqVO pageReqVO) {
        return storeComplainMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createStoreComplainApp(@Valid AppStoreComplainSaveReqVO createReqVO) {
        // 插入
        StoreComplainDO storeComplain = BeanUtils.toBean(createReqVO, StoreComplainDO.class);
        storeComplainMapper.insert(storeComplain);
        // 返回
        return storeComplain.getId();
    }
}