package cn.iocoder.yudao.module.supplier.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.supplier.entity.BaseSupplier;
import cn.iocoder.yudao.module.supplier.entity.BaseSupplierAccount;
import cn.iocoder.yudao.module.supplier.mapper.BaseSupplierMapper;
import cn.iocoder.yudao.module.supplier.service.BaseSupplierAccountService;
import cn.iocoder.yudao.module.supplier.service.BaseSupplierService;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierPageReqVO;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO;
import cn.iocoder.yudao.module.supplier.vo.BaseSupplierSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;

/**
 * 供应商 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class BaseSupplierServiceImpl extends ServiceImpl<BaseSupplierMapper, BaseSupplier> implements BaseSupplierService {

    @Resource
    private BaseSupplierMapper baseSupplierMapper;
    @Resource
    private BaseSupplierAccountService baseSupplierAccountService;

    /**
     * 保存供应商
     *
     * @param saveReqVO BaseSupplierSaveReqVO
     * @author PQZ
     * @date 11:00 2024/9/11
     **/
    @Override
    public void saveBaseSupplier(BaseSupplierSaveReqVO saveReqVO) {
        //保存主表信息
        BaseSupplier baseSupplier = BeanUtils.toBean(saveReqVO, BaseSupplier.class);
        saveOrUpdate(baseSupplier);
        //保存子表信息
        baseSupplierAccountService.saveBaseSupplierAccount(baseSupplier.getId(),saveReqVO.getAccountList());
    }

    /**
     * 生成采购单选择供应商
     *
     * @param supplierName 供应商名称
     * @return java.lang.String
     * @author PQZ
     * @date 17:43 2024/10/25
     **/
    @Override
    public String appSaveSupplier(String supplierName) {
        LambdaQueryWrapper<BaseSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted,0).eq(BaseSupplier::getName,supplierName);
        List<BaseSupplier> list = list(lambdaQueryWrapper);
        if (!list.isEmpty()){
            //存在供应商
            return list.get(0).getId();
        } else {
            BaseSupplier supplier = new BaseSupplier();
            supplier.setName(supplierName);
            save(supplier);
            return supplier.getId();
        }
    }


    /**
     * 删除供应商信息
     * @author PQZ
     * @date 9:56 2024/9/12
     * @param id 供应商id
     **/
    @Override
    public void deleteBaseSupplier(String id) {
        //删除主表信息
        removeById(id);
        //删除子表信息
        baseSupplierAccountService.removeBySupplierId(id);
    }

    /**
     * 通过供应商id查询供应商
     * @author PQZ
     * @date 10:02 2024/9/12
     * @param id 供应商id
     * @return cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO
     **/
    @Override
    public BaseSupplierRespVO getBaseSupplier(String id) {
        //主表内容
        BaseSupplier baseSupplier = getById(id);
        BaseSupplierRespVO respVO = BeanUtils.toBean(baseSupplier, BaseSupplierRespVO.class);
        //银行账户内容
        List<BaseSupplierAccount> list = baseSupplierAccountService.listBySupplierId(id);
        respVO.setAccountList(list);
        //关联子公司转换
        if (StringUtils.isNotBlank(respVO.getCorpId())) {
            respVO.setCorpIds(Arrays.asList(respVO.getCorpId().split(",")));
        }
        return respVO;
    }

    /**
     * 分页查询供应商信息
     *
     * @param pageReqVO BaseSupplierPageReqVO实体
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.supplier.vo.BaseSupplierRespVO>
     * @author PQZ
     * @date 10:09 2024/9/12
     **/
    @Override
    public IPage<BaseSupplierRespVO> queryListPage(BaseSupplierPageReqVO pageReqVO, Page<BaseSupplierRespVO> page) {
        return baseSupplierMapper.selectListPage(pageReqVO,page);
    }

}