package com.zt.plat.module.base.service.base;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.api.dto.AccountDTO;
import com.zt.plat.module.base.controller.admin.base.vo.AccountPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.AccountRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.AccountSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.AccountDO;
import com.zt.plat.module.base.dal.mysql.base.AccountMapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.ACCOUNT_NOT_EXISTS;

/**
 * 账户条款 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public AccountRespVO createAccount(AccountSaveReqVO createReqVO) {
        // 插入
        AccountDO account = BeanUtils.toBean(createReqVO, AccountDO.class);
        accountMapper.insert(account);
        // 返回
        return BeanUtils.toBean(account, AccountRespVO.class);
    }

    @Override
    public void updateAccount(AccountSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountExists(updateReqVO.getId());
        // 更新
        AccountDO updateObj = BeanUtils.toBean(updateReqVO, AccountDO.class);
        accountMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccount(Long id) {
        // 校验存在
        validateAccountExists(id);
        // 删除
        accountMapper.deleteById(id);
    }

    @Override
    public void deleteAccountListByIds(List<Long> ids) {
        // 校验存在
        validateAccountExists(ids);
        // 删除
        accountMapper.deleteByIds(ids);
    }

    private void validateAccountExists(List<Long> ids) {
        List<AccountDO> list = accountMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
    }

    private void validateAccountExists(Long id) {
        if (accountMapper.selectById(id) == null) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public AccountDO getAccount(Long id) {
        return accountMapper.selectById(id);
    }

    @Override
    public PageResult<AccountDO> getAccountPage(AccountPageReqVO pageReqVO) {
        return accountMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableAccountList(List<AccountSaveReqVO> entityList) {
        List<AccountDO> updateObj = BeanUtils.toBean(entityList, AccountDO.class);
        List<BatchResult> count =  accountMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public List<AccountDTO> getAccountNoPage(AccountDTO dto) {
        List<AccountDO> entityList = accountMapper.selectNoPage(dto);
        return BeanUtils.toBean(entityList, AccountDTO.class);
    }
}