package cn.iocoder.hake.module.pay.service.serviceregist;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.hake.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.hake.module.pay.controller.admin.serviceregist.vo.*;
import cn.iocoder.hake.module.pay.dal.dataobject.serviceregist.ServiceRegistDO;
import cn.iocoder.hake.framework.common.pojo.PageResult;
import cn.iocoder.hake.framework.common.util.object.BeanUtils;

import cn.iocoder.hake.module.pay.dal.mysql.serviceregist.ServiceRegistMapper;

import static cn.iocoder.hake.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.hake.module.pay.enums.ErrorCodeConstants.*;

/**
 * 商户服务注册 Service 实现类
 *
 * @author hake
 */
@Service
@Validated
public class ServiceRegistServiceImpl implements ServiceRegistService {

    @Resource
    private ServiceRegistMapper serviceRegistMapper;


    @Override
    public Long createServiceRegist(ServiceRegistSaveReqVO createReqVO) {
        // 插入
        ServiceRegistDO serviceRegist = BeanUtils.toBean(createReqVO, ServiceRegistDO.class);
        serviceRegist.setCreator(SecurityFrameworkUtils.getLoginUserId().toString());
        serviceRegist.setUpdater(SecurityFrameworkUtils.getLoginUserId().toString());
        serviceRegist.setDeleted(false);
        serviceRegistMapper.insert(serviceRegist);
        // 返回
        return serviceRegist.getId();
    }

    @Override
    public void updateServiceRegist(ServiceRegistSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceRegistExists(updateReqVO.getId());
        // 更新
        ServiceRegistDO updateObj = BeanUtils.toBean(updateReqVO, ServiceRegistDO.class);
        updateObj.setUpdater(SecurityFrameworkUtils.getLoginUserId().toString());
        updateObj.setUpdateTime(LocalDateTime.now());
        serviceRegistMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceRegist(Long id) {
        // 校验存在
        validateServiceRegistExists(id);
        // 删除
        serviceRegistMapper.deleteById(id);
    }

    @Override
    public void deleteServiceRegistListByIds(List<Long> ids) {
        // 校验存在
        validateServiceRegistExists(ids);
        // 删除
        serviceRegistMapper.deleteByIds(ids);
    }

    private void validateServiceRegistExists(List<Long> ids) {
        List<ServiceRegistDO> list = serviceRegistMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SERVICE_REGIST_NOT_EXISTS);
        }
    }

    private void validateServiceRegistExists(Long id) {
        if (serviceRegistMapper.selectById(id) == null) {
            throw exception(SERVICE_REGIST_NOT_EXISTS);
        }
    }

    @Override
    public ServiceRegistDO getServiceRegist(Long id) {
        return serviceRegistMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceRegistDO> getServiceRegistPage(ServiceRegistPageReqVO pageReqVO) {
        return serviceRegistMapper.selectPage(pageReqVO);
    }


    /**
     * 获得商户服务注册信息
     *
     * @param appId     应用编号
     * @param channelId 渠道编号
     * @param mchNo     商户编号
     * @return 商户服务注册信息
     */
    @Override
    public ServiceRegistDO getServiceRegist(String appId, String channelId, String mchNo) {
        return serviceRegistMapper.selectByCondition(appId, channelId, mchNo);
    }

}