package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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 lombok.extern.slf4j.Slf4j;
import lombok.val;
import net.ferrum.business.domain.QsSupplier;
import net.ferrum.business.domain.QsUserSupplier;
import net.ferrum.business.domain.bo.QsSupplierBo;
import net.ferrum.business.domain.vo.QsSupplierVo;
import net.ferrum.business.domain.vo.QsUserSupplierVo;
import net.ferrum.business.mapper.QsSupplierMapper;
import net.ferrum.business.mapper.QsUserSupplierMapper;
import net.ferrum.business.service.IQsSupplierService;
import net.ferrum.common.core.domain.PageQuery;
import net.ferrum.common.core.page.TableDataInfo;
import net.ferrum.common.utils.StringUtils;
import net.ferrum.system.domain.SysUserRole;
import net.ferrum.system.domain.vo.SysUserBasicInfoVo;
import net.ferrum.system.mapper.SysUserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 供应商信息Service业务层处理
 *
 * @author ferrum
 * @version 1.0.0
 * @since 2025-03-05
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class QsSupplierServiceImpl implements IQsSupplierService {

    private final QsSupplierMapper supplierMapper;
    private final QsUserSupplierMapper userSupplierMapper;
    private final SysUserMapper userMapper;

    /**
     * 查询供应商信息
     */
    @Override
    public QsSupplierVo queryById(Long supplierId){
        QsSupplierVo result = supplierMapper.selectVoById(supplierId);
        List<QsUserSupplierVo> relations = userSupplierMapper.selectUsersbySupplierId(supplierId);
//        List<QsUserSupplier> relations = userSupplierMapper.selectList(new LambdaQueryWrapper<QsUserSupplier>().eq(QsUserSupplier::getSupplierId, supplierId));
//        List<SysUserBasicInfoVo> userInfos = new ArrayList<>();
//        if (!CollectionUtils.isEmpty(relations)) {
//            relations.forEach(relation -> {
//                userInfos.add(userMapper.selectUserBasicInfoById(relation.getUserId()));
//            });
//        }
//        result.setDockerUsers(userInfos);
        result.setDockerUsers(relations);
        return result;
    }

    /**
     * 查询供应商信息列表
     */
    @Override
    public TableDataInfo<QsSupplierVo> queryPageList(QsSupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsSupplier> lqw = buildQueryWrapper(bo);
        Page<QsSupplierVo> result = supplierMapper.selectVoPage(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    /**
     * 查询供应商信息列表
     */
    @Override
    public List<QsSupplierVo> queryList(QsSupplierBo bo) {
        LambdaQueryWrapper<QsSupplier> lqw = buildQueryWrapper(bo);
        return supplierMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsSupplier> buildQueryWrapper(QsSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsSupplier> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), QsSupplier::getSupplierName, bo.getSupplierName());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierAddress()), QsSupplier::getSupplierAddress, bo.getSupplierAddress());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierContact()), QsSupplier::getSupplierContact, bo.getSupplierContact());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierContactNum()), QsSupplier::getSupplierContactNum, bo.getSupplierContactNum());
        return lqw;
    }

    /**
     * 新增供应商信息
     */
    @Override
    public Boolean insertByBo(QsSupplierBo bo) {
        QsSupplier add = BeanUtil.toBean(bo, QsSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = supplierMapper.insert(add) > 0;
        if (flag) {
            bo.setSupplierId(add.getSupplierId());
        }
        return flag;
    }

    /**
     * 修改供应商信息
     */
    @Override
    public Boolean updateByBo(QsSupplierBo bo) {
        QsSupplier update = BeanUtil.toBean(bo, QsSupplier.class);
        validEntityBeforeSave(update);
        return supplierMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除供应商信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return supplierMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 申领供应商
     *
     * @param supplierIds 供应商主键集合
     * @param userId 当前登录用户Id
     * @return 是否成功申领
     */
    @Override
    public Boolean claimSuppliers(Collection<Long> supplierIds, Long userId) {
        List<QsUserSupplier> relations = new ArrayList<>(0);
        supplierIds.forEach(id -> {
            //relations.add(new QsUserSupplier(userId, id));

            QsUserSupplier t = new QsUserSupplier();
            t.setUserId(userId);
            t.setSupplierId(id);
            relations.add(t);

        });
        return userSupplierMapper.insertBatch(relations);
    }

    @Override
    public Boolean resignSuppliers(Collection<Long> userIds, Long supplierId) {
        List<QsUserSupplier> relations = new ArrayList<>(0);
        userIds.forEach(id -> {
            //relations.add(new QsUserSupplier(id, supplierId));

            QsUserSupplier t = new QsUserSupplier();
            t.setUserId(id);
            t.setSupplierId(supplierId);
            relations.add(t);
        });
        return userSupplierMapper.insertBatch(relations);
    }
}
