package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.mapper.SysResMapper;
import com.yunfeiyun.agmp.common.web.system.service.ISysResService;
import com.yunfeiyun.agmp.fms.domain.WmsSupplier;
import com.yunfeiyun.agmp.fms.mapper.WmsSupplierMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsSupplierAddReqVo;
import com.yunfeiyun.agmp.fms.service.IWmsSupplierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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


/**
 * 供应商Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-25
 */
@Service
public class WmsSupplierServiceImpl implements IWmsSupplierService
{
    private static final Logger log = LoggerFactory.getLogger(WmsSupplierServiceImpl.class);
    @Autowired
    private WmsSupplierMapper wmsSupplierMapper;

    @Autowired
    private SysResMapper sysResMapper;

    @Resource
    private ResManager resManager;

    @Resource
    private ISysResService sysResService;

    /**
     * 查询供应商
     * 
     * @param supplierId 供应商主键
     * @return 供应商
     */
    @Override
    public WmsSupplier selectWmsSupplierBySupplierId(String supplierId)
    {
        return wmsSupplierMapper.selectWmsSupplierBySupplierId(supplierId);
    }

    /**
     * 查询供应商列表
     * 
     * @param wmsSupplier 供应商
     * @return 供应商
     */
    @Override
    public List<WmsSupplier> selectWmsSupplierList(WmsSupplier wmsSupplier)
    {
        return wmsSupplierMapper.selectWmsSupplierList(wmsSupplier);
    }

    /**
     * 新增供应商
     * 
     * @param wmsSupplierAddReqVo 供应商
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsSupplier(WmsSupplierAddReqVo wmsSupplierAddReqVo)
    {
        if(StringUtils.isEmpty(wmsSupplierAddReqVo.getSupplierType())){
            throw new BizException(ErrorCode.WMS_SUPPLIER_TYPE_IS_EMPTY);
        }
        wmsSupplierAddReqVo.setSupplierId(wmsSupplierAddReqVo.getId());
        resManager.updateRes(wmsSupplierAddReqVo.getSupplierQualificationResId(),wmsSupplierAddReqVo.getSupplierId());
        return wmsSupplierMapper.insertWmsSupplier(wmsSupplierAddReqVo);
    }

    /**
     * 修改供应商
     * 
     * @param wmsSupplierAddReqVo 供应商
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsSupplier(WmsSupplierAddReqVo  wmsSupplierAddReqVo)
    {
        List<SysRes> oldRes = resManager.queryRes(wmsSupplierAddReqVo.getSupplierId(),ResConstants.BizType.SUPPLY,ResConstants.mediaType.IMG);
        if(oldRes.size() == 0){
            if (!StringUtils.isEmpty(wmsSupplierAddReqVo.getSupplierQualificationResId())) {
                resManager.updateRes(wmsSupplierAddReqVo.getSupplierQualificationResId(),wmsSupplierAddReqVo.getSupplierId());
            }
            return wmsSupplierMapper.updateWmsSupplier(wmsSupplierAddReqVo);
        }
        SysRes sysRes = oldRes.get(0);
        if (!sysRes.getResId().equals(wmsSupplierAddReqVo.getSupplierQualificationResId())) {
            sysResMapper.deleteSysResByResId(sysRes.getResId());
            if (!StringUtils.isEmpty(wmsSupplierAddReqVo.getSupplierQualificationResId())) {
                resManager.updateRes(wmsSupplierAddReqVo.getSupplierQualificationResId(),wmsSupplierAddReqVo.getSupplierId());
            }
        }
        return wmsSupplierMapper.updateWmsSupplier(wmsSupplierAddReqVo);
    }

    /**
     * 批量删除供应商
     * 
     * @param supplierIds 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteWmsSupplierBySupplierIds(String[] supplierIds)
    {
        return wmsSupplierMapper.deleteWmsSupplierBySupplierIds(supplierIds);
    }

    /**
     * 删除供应商信息
     * 
     * @param supplierId 供应商主键
     * @return 结果
     */
    @Override
    public int deleteWmsSupplierBySupplierId(String supplierId)
    {
        return wmsSupplierMapper.deleteWmsSupplierBySupplierId(supplierId);
    }

    @Override
    public AjaxResult upload(MultipartFile file) {
        if (file.isEmpty())
        {
            return AjaxResult.error(ErrorCode.UPLOAD_FAIL.getCode());
        }
        try {
            SysRes sysRes = resManager.uploadRes(ResConstants.BizType.SUPPLY, ResConstants.mediaType.IMG,null,file,SecurityUtils.getLoginUser());
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", sysRes.getResUrl());
            ajax.put("resId", sysRes.getResId());
            return ajax;
        }catch (Exception e){
            log.error("{}",e);
            return AjaxResult.error(ErrorCode.UPLOAD_FAIL.getCode());
        }
    }
}
