package com.prolog.cs.biz.inbound.service.impl;

import com.prolog.common.util.ToolKits;
import com.prolog.cs.basic.common.model.BaseBizFile;
import com.prolog.cs.basic.common.model.vo.BaseBizFileQueryVO;
import com.prolog.cs.biz.inbound.remote.CommonCaller;
import com.prolog.cs.biz.inbound.service.PictureManagementService;
import com.prolog.framework.utils.StringUtils;
import com.prolog.wms.base.util.RestMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
public class PictureManagementServiceImpl implements PictureManagementService {

    @Autowired
    private CommonCaller commonCaller;

    @Override
    @Transactional
    public Object saveOrUpdateFile(List<BaseBizFile> dataList) {
        // 参数校验
        this.checkFileParam(dataList);

        BaseBizFileQueryVO queryVO = new BaseBizFileQueryVO();
        queryVO.setBizId(dataList.get(0).getBizId());
        queryVO.setBizType(dataList.get(0).getBizType());
        RestMsgUtils.retrieveResult(commonCaller.del(queryVO), "删除关联文件接口：");
        RestMsgUtils.retrieveResult(commonCaller.add(dataList), "保存关联文件记录接口：");
        return true;
    }

    @Override
    public Object del(List<BaseBizFile> dataList) {
        if(dataList == null || dataList.size() <= 0){
            return null;
        }
        for (BaseBizFile baseBizFile : dataList) {
            if(!StringUtils.isBlank(baseBizFile.getBizId())){
                BaseBizFileQueryVO queryVO = new BaseBizFileQueryVO();
                queryVO.setBizId(baseBizFile.getBizId());
                commonCaller.del(queryVO);
            }
        }
        return null;
    }

    private void checkFileParam(List<BaseBizFile> dataList) {
        ToolKits.isParamNull(dataList, "请求入参不能为空！");
        for (BaseBizFile baseBizFile : dataList) {
            ToolKits.isParamNull(baseBizFile.getBizId(), "业务ID不能为空！");
            ToolKits.isParamNull(baseBizFile.getBizType(), "业务类型不能为空！");
            ToolKits.isParamNull(baseBizFile.getFileUrl(), "文件路径不能为空！");
            ToolKits.isParamNull(baseBizFile.getFileName(), "文件名称不能为空！");
        }
    }
}
