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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.supplier.dto.SupplierRpcDTO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.system.dal.dataobject.supplier.SupplierProductDO;
import cn.iocoder.yudao.module.system.dal.mysql.supplier.SupplierMapper;
import cn.iocoder.yudao.module.system.dal.mysql.supplier.SupplierProductMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应商列表(OA已审核通过后插入) Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private SupplierProductMapper supplierProductMapper;

    @Override
    public Long createSupplier(SupplierSaveReqVO createReqVO) {
        // 插入
        SupplierDO supplier = BeanUtils.toBean(createReqVO, SupplierDO.class);
        supplierMapper.insert(supplier);
        // 返回
        return supplier.getId();
    }

    @Override
    public void updateSupplier(SupplierSaveReqVO updateReqVO) {
        // 更新
        SupplierDO updateObj = BeanUtils.toBean(updateReqVO, SupplierDO.class);
        supplierMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplier(Long id) {
        // 删除
        supplierMapper.deleteById(id);
    }

    @Override
    public SupplierDO getSupplier(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public PageResult<SupplierDO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        return supplierMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveOrEdit(SupplierRpcDTO supplier) {
        // 新增操作
        SupplierDO supplierDO = BeanUtil.copyProperties(supplier, SupplierDO.class);
        List<SupplierProductDO> products = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(supplier.getItems())) {
            products = BeanUtil.copyToList(supplier.getItems(), SupplierProductDO.class);
        }
        if (supplier.getId() == null) {
            supplierMapper.insert(supplierDO);
            if (CollectionUtil.isNotEmpty(products)) {
                for (SupplierProductDO product : products) {
                    product.setSupplierId(supplierDO.getId());
                }
            }
            supplierProductMapper.insertBatch(products);
        } else {
            supplierMapper.updateById(supplierDO);
            // -- 操作子表
            if (CollectionUtil.isNotEmpty(products)) {
                List<SupplierProductDO> saveList = products.stream().filter(a -> a.getId() == null).collect(Collectors.toList());
                List<SupplierProductDO> editList = products.stream().filter(a -> a.getId() != null).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(saveList)) {
                    for (SupplierProductDO supplierProductDO : saveList) {
                        supplierProductDO.setSupplierId(supplierDO.getId());
                    }
                    supplierProductMapper.insertBatch(saveList);
                }
                if (CollectionUtil.isNotEmpty(editList)) {
                    supplierProductMapper.updateBatch(editList);
                }
            }
        }
    }

    @Override
    public void editStatus(SupplierSaveReqVO vo) {
        SupplierDO supplier = BeanUtils.toBean(vo, SupplierDO.class);
        supplierMapper.updateById(supplier);
    }

    @Override
    public List<SupplierDO> allList(Integer status) {
        return supplierMapper.selectList(new LambdaQueryWrapper<SupplierDO>()
                .eq(status != null, SupplierDO::getStatus, status));
    }
}
