package org.dromara.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.CaseImportUser;
import org.dromara.business.domain.bo.CaseImportUserBo;
import org.dromara.business.domain.vo.CaseImportUserVo;
import org.dromara.business.mapper.CaseImportUserMapper;
import org.dromara.business.service.ICaseImportUserService;
import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 用户导入记录Service业务层处理
 *
 * @author wuzhigang
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class CaseImportUserServiceImpl implements ICaseImportUserService {

    private final CaseImportUserMapper baseMapper;

    /**
     * 查询用户导入记录
     */
    @Override
    public CaseImportUserVo queryById(Long userId) {
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户导入记录列表
     */
    @Override
    public TableDataInfo<CaseImportUserVo> queryPageList(CaseImportUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CaseImportUser> lqw = buildQueryWrapper(bo);
        // 状态倒序
        lqw.orderByDesc(CaseImportUser::getUserStatus);
        Page<CaseImportUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户导入记录列表
     */
    @Override
    public List<CaseImportUserVo> queryList(CaseImportUserBo bo) {
        LambdaQueryWrapper<CaseImportUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaseImportUser> buildQueryWrapper(CaseImportUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaseImportUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), CaseImportUser::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), CaseImportUser::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), CaseImportUser::getPhonenumber, bo.getPhonenumber());
        lqw.like(StringUtils.isNotBlank(bo.getOrganizationName()), CaseImportUser::getOrganizationName, bo.getOrganizationName());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), CaseImportUser::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getImportBatch()), CaseImportUser::getImportBatch, bo.getImportBatch());
        return lqw;
    }

    /**
     * 新增用户导入记录
     */
    @Override
    public Boolean insertByBo(CaseImportUserBo bo) {
        CaseImportUser add = MapstructUtils.convert(bo, CaseImportUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户导入记录
     */
    @Override
    public Boolean updateByBo(CaseImportUserBo bo) {
        CaseImportUser update = MapstructUtils.convert(bo, CaseImportUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除用户导入记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * csv用户批量导入
     *
     * @param boList
     * @return
     */
    @Override
    public boolean insertBatch(List<CaseImportUserBo> boList) {
        List<CaseImportUser> csvUserList = new ArrayList<>();
        for (CaseImportUserBo bo : boList
        ) {
            CaseImportUser csvUser = MapstructUtils.convert(bo, CaseImportUser.class);
            csvUserList.add(csvUser);
        }
        return baseMapper.insertBatch(csvUserList);
    }

    /**
     * 批量更新导入状态
     *
     * @param voList
     * @return
     */
    @Override
    public boolean updateStatusBatch(List<CaseImportUserVo> voList) {
        List<CaseImportUser> csvUserList = new ArrayList<>();
        for (CaseImportUserVo vo : voList
        ) {
            CaseImportUser csvUser = new CaseImportUser();
            csvUser.setUserId(vo.getUserId());
            // 已导入
            csvUser.setImportStatus(UserConstants.IMPORTED);
            csvUserList.add(csvUser);
        }
        return baseMapper.updateBatchById(csvUserList);
    }
}
