package org.dromara.zhishu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysOss;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.UserAccountBo;
import org.dromara.zhishu.domain.vo.UserAccountVo;
import org.dromara.zhishu.domain.UserAccount;
import org.dromara.zhishu.mapper.UserAccountMapper;
import org.dromara.zhishu.service.IUserAccountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 账号管理Service业务层处理
 *
 * @author yxy
 * @date 2026-02-25
 */
@RequiredArgsConstructor
@Service
public class UserAccountServiceImpl implements IUserAccountService {

    private final UserAccountMapper baseMapper;

    /**
     * 查询账号管理
     *
     * @param id 主键
     * @return 账号管理
     */
    @Override
    public UserAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询账号管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 账号管理分页列表
     */
    @Override
    public TableDataInfo<UserAccountVo> queryPageList(UserAccountBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId != 1){
            bo.setCreateBy(userId);
        }
        LambdaQueryWrapper<UserAccount> lqw = buildQueryWrapper(bo);
        Page<UserAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的账号管理列表
     *
     * @param bo 查询条件
     * @return 账号管理列表
     */
    @Override
    public List<UserAccountVo> queryList(UserAccountBo bo) {
        Long userId = LoginHelper.getUserId();
        if (userId != 1){
            bo.setCreateBy(userId);
        }
        LambdaQueryWrapper<UserAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<UserAccountVo> queryListAdmin(UserAccountBo bo){
        bo.setCreateBy(1L);
        LambdaQueryWrapper<UserAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserAccount> buildQueryWrapper(UserAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserAccount> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(UserAccount::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getType()), UserAccount::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountType()), UserAccount::getAccountType, bo.getAccountType());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), UserAccount::getAccountName, bo.getAccountName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), UserAccount::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), UserAccount::getIsDefault, bo.getIsDefault());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), UserAccount::getStatus, bo.getStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getCreateBy()), UserAccount::getCreateBy, bo.getCreateBy());
        return lqw;
    }

    /**
     * 新增账号管理
     *
     * @param bo 账号管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserAccountBo bo) {
        UserAccount add = MapstructUtils.convert(bo, UserAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改账号管理
     *
     * @param bo 账号管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserAccountBo bo) {
        UserAccount update = MapstructUtils.convert(bo, UserAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除账号管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
