package cn.iocoder.yudao.module.conf.service.impl;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.conf.entity.BaseWarehouse;
import cn.iocoder.yudao.module.conf.mapper.BaseWarehouseMapper;
import cn.iocoder.yudao.module.conf.service.BaseWarehouseService;
import cn.iocoder.yudao.module.conf.vo.BaseWarehouseListReqVO;
import cn.iocoder.yudao.module.conf.vo.BaseWarehouseRespVO;
import cn.iocoder.yudao.module.conf.vo.BaseWarehouseSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.common.ErrorCodeConstants.BASE_WAREHOUSE_EXITS_CHILDREN;
import static cn.iocoder.yudao.common.ErrorCodeConstants.BASE_WAREHOUSE_NAME_DUPLICATE;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 配置类型 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class BaseWarehouseServiceImpl extends ServiceImpl<BaseWarehouseMapper, BaseWarehouse> implements BaseWarehouseService {
    @Resource
    private BaseWarehouseMapper baseWarehouseMapper;

    /**
     * 新增仓库
     *
     * @param saveReqVO BaseWarehouseSaveReqVO实体
     * @return void
     * @author PQZ
     * @date 18:28 2024/9/10
     **/
    @Override
    public void saveBaseWarehouse(BaseWarehouseSaveReqVO saveReqVO) {
        /*1、数据校验*/
        //同级别下校验名称是否重复
        validateWarehouseUnique(saveReqVO.getId(), saveReqVO.getParentId(), saveReqVO.getName(), saveReqVO.getCode());
        /*2、数据保存*/
        //类型转换
        BaseWarehouse baseWarehouse = BeanUtils.toBean(saveReqVO, BaseWarehouse.class);
        //数据保存
        saveOrUpdate(baseWarehouse);
    }

    /**
     * 删除仓库
     *
     * @param id 仓库id
     * @author PQZ
     * @date 18:29 2024/9/10
     **/
    @Override
    public void deleteBaseWarehouse(String id) {
        // 校验是否有子配置类型
        LambdaQueryWrapper<BaseWarehouse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseWarehouse::getParentId, id);
        List<BaseWarehouse> list = list(lambdaQueryWrapper);
        if (list.size() > 0) {
            throw exception(BASE_WAREHOUSE_EXITS_CHILDREN);
        }
        // 删除
        baseWarehouseMapper.deleteById(id);
    }

    /**
     * 根据仓库id查询仓库
     *
     * @param id 仓库id
     * @return cn.iocoder.yudao.module.conf.vo.BaseTypeRespVO
     * @author PQZ
     * @date 18:30 2024/9/10
     **/
    @Override
    public BaseWarehouseRespVO getBaseWarehouse(String id) {
        BaseWarehouse baseWarehouse = getById(id);
        BaseWarehouseRespVO result = BeanUtils.toBean(baseWarehouse, BaseWarehouseRespVO.class);
        //关联子公司转换
        if (StringUtils.isNotBlank(result.getCorpId())) {
            result.setCorpIds(Arrays.asList(result.getCorpId().split(",")));
        }
        return result;
    }

    /**
     * 获取仓库列表
     *
     * @param listReqVO BaseWarehouseListReqVO实体
     * @return java.util.List<cn.iocoder.yudao.module.conf.vo.BaseWarehouseRespVO>
     * @author PQZ
     * @date 18:31 2024/9/10
     **/
    @Override
    public List<BaseWarehouseRespVO> getBaseWarehouseList(BaseWarehouseListReqVO listReqVO) {
        return baseWarehouseMapper.queryList(listReqVO);
    }

    /**
     * 同类型，同级别下校验名称及编码唯一性
     *
     * @param id       id
     * @param parentId 父类id
     * @param name     名称
     * @param code     编码
     * @author PQZ
     * @date 11:34 2024/9/10
     **/
    private void validateWarehouseUnique(String id, String parentId, String name, String code) {
        LambdaQueryWrapper<BaseWarehouse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted, 0)
                .eq(BaseWarehouse::getName, name)
                .eq(BaseWarehouse::getCode, code)
                .eq(BaseWarehouse::getParentId, parentId);
        //id存在为编辑情况,排除自身
        if (null != id) {
            lambdaQueryWrapper.ne(BaseWarehouse::getId, id);
        }
        //查询list，校验唯一性
        List<BaseWarehouse> list = list(lambdaQueryWrapper);
        if (list.size() > 0) {
            throw exception(BASE_WAREHOUSE_NAME_DUPLICATE);
        }
    }
}