package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.WarehouseConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.WarehouseConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.VolunteerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.WarehouseConfigDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.VolunteerMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.WarehouseConfigMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.VOLUNTEER_NOT_EXISTS;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.CONFIG_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/10  9:05
 * @Description:
 * @Version: 4.0
 */

@Service
@Validated
public class WarehouseConfigServiceImpl implements WarehouseConfigService {

    @Resource
    private WarehouseConfigMapper warehouseConfigMapper;

    @Resource
    private TenantMapper tenantMapper;

    @Override
    public Long createConfig(WarehouseConfigSaveReqVO createReqVO) {
        // 插入
        WarehouseConfigDO config = BeanUtils.toBean(createReqVO, WarehouseConfigDO.class);
        warehouseConfigMapper.insert(config);
        // 返回
        return config.getId();
    }

    @Override
    public void updateConfig(WarehouseConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigExists(updateReqVO.getId());
        // 更新
        WarehouseConfigDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseConfigDO.class);
        warehouseConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfig(Long id) {
        // 校验存在
        validateConfigExists(id);
        // 删除
        warehouseConfigMapper.deleteById(id);
    }

    private void validateConfigExists(Long id) {
        if (warehouseConfigMapper.selectById(id) == null) {
            throw exception(CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseConfigDO getConfig(Long id) {
        return warehouseConfigMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseConfigDO> getConfigPage(WarehouseConfigPageReqVO pageReqVO) {
        List<TenantDO> list = tenantMapper.selectList(new LambdaQueryWrapperX<TenantDO>().inIfPresent(TenantDO::getId, pageReqVO.getTenantIds()));
        Map<Long, String> tenantMap = new HashMap<>();
        list.forEach(tenantDO -> {
            tenantMap.put(tenantDO.getId(), tenantDO.getName());
        });
        PageResult<WarehouseConfigDO> warehouseConfigDOPageResult = warehouseConfigMapper.selectPage(pageReqVO);
        List<WarehouseConfigDO> resultList = warehouseConfigDOPageResult.getList();
        resultList.forEach(warehouseConfigDO -> {
            if (tenantMap.containsKey(warehouseConfigDO.getTenantId())) {
                warehouseConfigDO.setTenantName(tenantMap.get(warehouseConfigDO.getTenantId()));
            } else {
                warehouseConfigDO.setTenantName(tenantMapper.selectById(warehouseConfigDO.getTenantId()).getName());
            }
        });
        return warehouseConfigDOPageResult;
    }


}
