package org.dromara.erp.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
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.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpClient;
import org.dromara.erp.domain.query.ErpClientQuery;
import org.dromara.erp.domain.vo.ErpClientVo;
import org.dromara.erp.mapper.ErpClientMapper;
import org.dromara.erp.service.IErpClientService;
import org.dromara.yingpaipay.api.erp.domain.vo.CommonErpClientVo;
import org.dromara.yingpaipay.api.erp.CommonErpClientService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * HTSail_CS 客户档案 Service实现
 *
 * @Author: Antigravity
 */
@DS("htsail_cs")
@RequiredArgsConstructor
@Service("erpClientService")
public class ErpClientServiceImpl implements IErpClientService, CommonErpClientService {

    private final ErpClientMapper baseMapper;

    @Override
    public Map<String, CommonErpClientVo> selectInfoByFRowIDs(List<String> authClientRowIds) {
        if (authClientRowIds == null || authClientRowIds.isEmpty()) {
            return Map.of();
        }
        List<ErpClient> clients = baseMapper.selectList(Wrappers.lambdaQuery(ErpClient.class)
            .select(ErpClient::getRowId, ErpClient::getName, ErpClient::getClientClass, ErpClient::getEnterDate)
            .in(ErpClient::getRowId, authClientRowIds));
        return clients.stream().collect(Collectors.toMap(ErpClient::getRowId, client -> {
            CommonErpClientVo vo = new CommonErpClientVo();
            vo.setRowId(client.getRowId());
            vo.setName(client.getName());
            vo.setClientClass(client.getClientClass());
            vo.setEnterDate(client.getEnterDate());
            return vo;
        }, (v1, v2) -> v1));
    }

    @Override
    public TableDataInfo<ErpClientVo> queryPageList(ErpClientQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpClient> lqw = buildQueryWrapper(query);
        Page<ErpClientVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public ErpClientVo queryById(String rowId) {
        return baseMapper.selectVoById(rowId);
    }

    @Override
    public List<ErpClientVo> searchByName(String name) {
        LambdaQueryWrapper<ErpClient> lqw = Wrappers.lambdaQuery();
        lqw.like(ErpClient::getName, name);
        lqw.orderByAsc(ErpClient::getNum);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<ErpClientVo> queryByIds(List<String> rowIds) {
        if (rowIds == null || rowIds.isEmpty()) {
            return List.of();
        }
        return baseMapper.selectVoByIds(rowIds);
    }

    private LambdaQueryWrapper<ErpClient> buildQueryWrapper(ErpClientQuery query) {
        LambdaQueryWrapper<ErpClient> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getName()), ErpClient::getName, query.getName());
        lqw.like(StringUtils.isNotBlank(query.getNum()), ErpClient::getNum, query.getNum());
        lqw.like(StringUtils.isNotBlank(query.getShortName()), ErpClient::getShortName, query.getShortName());
        lqw.like(StringUtils.isNotBlank(query.getPyCode()), ErpClient::getPyCode, query.getPyCode());
        lqw.eq(StringUtils.isNotBlank(query.getClientClass()), ErpClient::getClientClass, query.getClientClass());
        lqw.eq(query.getStopUse() != null, ErpClient::getStopUse, query.getStopUse());
        lqw.orderByAsc(ErpClient::getNum);
        return lqw;
    }

}
