package com.yc.ship.module.resource.service.supplier;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.util.MyBatisUtils;
import com.yc.ship.module.resource.controller.admin.supplier.vo.*;
import com.yc.ship.module.resource.dal.dataobject.supplier.ProductSupplierDO;
import com.yc.ship.module.resource.dal.dataobject.suppliercategory.SupplierCategoryDO;
import com.yc.ship.module.resource.dal.mysql.supplier.ProductSupplierMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import com.yc.ship.module.resource.service.suppliercategory.SupplierCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;


/**
 * 资源供应商 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductSupplierServiceImpl implements ProductSupplierService {

    @Resource
    private ProductSupplierMapper productSupplierMapper;
    @Autowired
    private SupplierCategoryService supplierCategoryService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createSupplier(SupplierSaveReqVO createReqVO) {
        // 插入
        ProductSupplierDO supplier = BeanUtils.toBean(createReqVO, ProductSupplierDO.class);
        Long id = IdWorker.getId(supplier);
        supplier.setId(id);
        supplier.setDeleted(false);
        productSupplierMapper.insert(supplier);
        List<Long> resourceList = createReqVO.getResourceType();
        if (CollUtil.isNotEmpty(resourceList)) {
            supplierCategoryService.saveBatch(resourceList, id);
        }
        // 返回
        return supplier.getId();
    }

    @Override
    @Transactional
    public void updateSupplier(SupplierSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierExists(updateReqVO.getId());
        // 更新
        ProductSupplierDO updateObj = BeanUtils.toBean(updateReqVO, ProductSupplierDO.class);
        productSupplierMapper.updateById(updateObj);
        supplierCategoryService.deleteSupplierCategory(updateObj.getId());
        List<Long> resourceList = updateReqVO.getResourceType();
        if (CollUtil.isNotEmpty(resourceList)) {
            supplierCategoryService.saveBatch(resourceList, updateObj.getId());
        }
    }

    @Override
    @Transactional
    public void deleteSupplier(Long id) {
        // 校验存在
        validateSupplierExists(id);
        // 删除
        productSupplierMapper.deleteById(id);
        supplierCategoryService.deleteSupplierCategory(id);
    }

    private void validateSupplierExists(Long id) {
        if (productSupplierMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    public ProductSupplierDO getSupplier(Long id) {
        return  productSupplierMapper.selectById(id);
    }

    @Override
    public SupplierRespVO getSupplierById(Long id) {
        ProductSupplierDO supplierDO = productSupplierMapper.selectById(id);
        List<SupplierCategoryDO> list = supplierCategoryService.getSupplierCategoryList(id);
        List<Long> categoryList = list.stream().map(SupplierCategoryDO::getCategoryId).collect(Collectors.toList());
        SupplierRespVO respVO = BeanUtils.toBean(supplierDO, SupplierRespVO.class);
        respVO.setResourceType(categoryList);
        return respVO;
    }

    @Override
    public PageResult<SupplierRespVO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        IPage<SupplierRespVO> iPage = MyBatisUtils.buildPage(pageReqVO);
        IPage<SupplierRespVO> page = productSupplierMapper.queryPage(iPage,pageReqVO);
        return new PageResult<>(page.getRecords(),page.getTotal());
    }

    @Override
    public void updateUserStatus(Long id, Integer status) {
        // 校验用户存在
        validateSupplierExists(id);
        // 更新状态
        ProductSupplierDO supplier = new ProductSupplierDO();
        supplier.setId(id);
        supplier.setUseStatus(status);
        productSupplierMapper.updateById(supplier);
    }

    @Override
    public SupplierImportRespVO importSupplierList(List<SupplierImportExcelVO> list, Boolean updateSupport) {
        if (CollUtil.isEmpty(list)) {
            throw exception(USER_IMPORT_LIST_IS_EMPTY);
        }
        SupplierImportRespVO respVO = SupplierImportRespVO.builder().createSuppliers(new ArrayList<>())
                .updateSuppliers(new ArrayList<>()).failureSuppliers(new LinkedHashMap<>()).build();
        list.forEach(importUser -> {
            // 校验，判断是否有不符合的原因

        });
        return respVO;

    }

    @Override
    public List<ProductSupplierDO> getSimpleListByCategory(SupplierListReqVO reqVO) {

        return productSupplierMapper.getSimpleListByCategory(reqVO);
    }

    @Override
    public SupplierRespVO getSupplierByDirectAccount(String directAccount) {
        List<ProductSupplierDO> lidst = productSupplierMapper.selectList(ProductSupplierDO::getDirectAccount, directAccount);
        if(lidst!=null && lidst.size()>0){
            ProductSupplierDO supplierDO = lidst.get(0);
            SupplierRespVO respVO = BeanUtils.toBean(supplierDO, SupplierRespVO.class);
            return respVO;
        }
        return null;
    }

    @Override
    public List<ProductSupplierDO> getSimpleListByType(SupplierListReqVO reqVO) {
        return productSupplierMapper.selectListByType(reqVO);
    }

    @Override
    public List<ProductSupplierDO> getList(List<Long> ids) {
        if(ids.isEmpty()) {
            return Collections.emptyList();
        }
        return productSupplierMapper.selectByIds(ids);
    }
}