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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.dept.DeptSapOrgApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptSapOrgRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgSimpleReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.saporg.SapOrgDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import cn.iocoder.yudao.module.system.dal.mysql.saporg.SapOrgMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SAP_ORG_NOT_EXISTS;

/**
 * SAP中的组织主数据(工厂 成本中心) Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class SapOrgServiceImpl implements SapOrgService {

    @Resource
    private SapOrgMapper sapOrgMapper;

    @Resource
    private DeptMapper deptMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptSapOrgApi deptSapOrgApi;

    @Override
    public Long createSapOrg(SapOrgSaveReqVO createReqVO) {
        // 插入
        SapOrgDO sapOrg = BeanUtils.toBean(createReqVO, SapOrgDO.class);
        sapOrgMapper.insert(sapOrg);
        // 返回
        return sapOrg.getId();
    }

    @Override
    public void updateSapOrg(SapOrgSaveReqVO updateReqVO) {
        // 校验存在
        validateSapOrgExists(updateReqVO.getId());
        // 更新
        SapOrgDO updateObj = BeanUtils.toBean(updateReqVO, SapOrgDO.class);
        sapOrgMapper.updateById(updateObj);
    }

    @Override
    public void deleteSapOrg(Long id) {
        // 校验存在
        validateSapOrgExists(id);
        // 删除
        sapOrgMapper.deleteById(id);
    }

    private void validateSapOrgExists(Long id) {
        if (sapOrgMapper.selectById(id) == null) {
            throw exception(SAP_ORG_NOT_EXISTS);
        }
    }

    @Override
    public SapOrgDO getSapOrg(Long id) {
        return sapOrgMapper.selectById(id);
    }

    @Override
    public PageResult<SapOrgDO> getSapOrgPage(SapOrgPageReqVO pageReqVO) {
        return sapOrgMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SapOrgDO> getSapOrgList(Collection<Long> ids, Integer type, Collection<Integer> statuses) {
        return sapOrgMapper.selectList(ids, type, statuses);
    }

    @Override
    public List<SapOrgDO> getSelectedList(SapOrgSimpleReqVO reqVO) {
        // 根据已经选定的 SAP工厂 筛选 成本中心列表 or 库存地点列表
        return sapOrgMapper.selectedList(reqVO.getType(), reqVO.getFactoryCodes());
    }

    @Override
    public List<SapOrgDO> getSapOrgList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return sapOrgMapper.selectBatchIds(ids);
    }

    @Override
    public List<SapOrgDO> filteredSapOrgList(Long deptId, Integer type, Set<Integer> statuses) {
        // 筛选部门id及子部门id关联的所有 SAP组织
        if (ObjUtil.isEmpty(deptId)) {
            return CollectionUtil.newArrayList();
        }
        Set<Long> ids = new HashSet<>();
        ids = deptService.getChildDeptIdListFromCache(deptId);
        ids.add(deptId);
        // 查询当前部门及所有子部门关联的SAP工厂 成本中心 列表
        List<DeptSapOrgRespDTO> sapOrgS = deptSapOrgApi.getSapOrgListByDeptIds(ids);
        if (CollUtil.isEmpty(sapOrgS)) {
            return CollectionUtil.newArrayList();
        }
        Set<Long> sapOrgIds = new HashSet<>();
        sapOrgS.forEach(sapOrg -> {
            if (ObjUtil.isNotEmpty(sapOrg.getFactoryId())) {
                sapOrgIds.add(sapOrg.getFactoryId());
            }
            if (ObjUtil.isNotEmpty(sapOrg.getCostCenterId())) {
                sapOrgIds.add(sapOrg.getCostCenterId());
            }
        });
        List<SapOrgDO> sapOrgList = getSapOrgList(sapOrgIds, type, statuses);
        return sapOrgList;
    }

}