package org.dromara.work.service.impl;

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.springframework.stereotype.Service;
import org.dromara.work.domain.bo.TzSharedAccountBo;
import org.dromara.work.domain.vo.TzSharedAccountVo;
import org.dromara.work.domain.TzSharedAccount;
import org.dromara.work.mapper.TzSharedAccountMapper;
import org.dromara.work.service.ITzSharedAccountService;

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

/**
 * 共享账号Service业务层处理
 *
 * @author Maosw
 * @date 2025-05-06
 */
@RequiredArgsConstructor
@Service
public class TzSharedAccountServiceImpl implements ITzSharedAccountService {

    private final TzSharedAccountMapper baseMapper;

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

    /**
     * 分页查询共享账号列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 共享账号分页列表
     */
    @Override
    public TableDataInfo<TzSharedAccountVo> queryPageList(TzSharedAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TzSharedAccount> lqw = buildQueryWrapper(bo);
        Page<TzSharedAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询共享账号数量
     *
     * @param bo 查询条件
     * @return 共享账号数量
     */
    @Override
    public Map<String, Object> queryCount(TzSharedAccountBo bo) {
        List<TzSharedAccountVo> list = queryList(bo);
        //账号总数量
        Long count = (long) list.size();
        //账号未使用数量
        Long notUsedCount = list.stream().filter(item -> item.getStatus() == 0).count();
        //账号使用中数量
        Long usingCount = list.stream().filter(item -> item.getStatus() == 1).count();

        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("notUsedCount", notUsedCount);
        map.put("usingCount", usingCount);
        return map;
    }

    /**
     * 查询符合条件的共享账号列表
     *
     * @param bo 查询条件
     * @return 共享账号列表
     */
    @Override
    public List<TzSharedAccountVo> queryList(TzSharedAccountBo bo) {
        LambdaQueryWrapper<TzSharedAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TzSharedAccount> buildQueryWrapper(TzSharedAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TzSharedAccount> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(TzSharedAccount::getCreateTime);
        lqw.eq(bo.getUserId() != null, TzSharedAccount::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), TzSharedAccount::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), TzSharedAccount::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getLoginName()), TzSharedAccount::getLoginName, bo.getLoginName());
        lqw.eq(bo.getStatus() != null, TzSharedAccount::getStatus, bo.getStatus());
        lqw.between(params.get("beginStartTime") != null && params.get("endStartTime") != null,
            TzSharedAccount::getStartTime ,params.get("beginStartTime"), params.get("endStartTime"));
        lqw.between(params.get("beginEndTime") != null && params.get("endEndTime") != null,
            TzSharedAccount::getEndTime ,params.get("beginEndTime"), params.get("endEndTime"));
        lqw.like(StringUtils.isNotBlank(bo.getBindName()), TzSharedAccount::getBindName, bo.getBindName());
        lqw.eq(StringUtils.isNotBlank(bo.getBindPhone()), TzSharedAccount::getBindPhone, bo.getBindPhone());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TzSharedAccount::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

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

    /**
     * 修改共享账号
     *
     * @param bo 共享账号
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TzSharedAccountBo bo) {
        TzSharedAccount update = MapstructUtils.convert(bo, TzSharedAccount.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量更新共享账号
     *
     * @param list1 共享账号列表
     */
    @Override
    public void updateBatchById(List<TzSharedAccount> list1) {
        baseMapper.updateBatchById(list1);
    }

    /**
     * 校验并批量删除共享账号信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
