package com.deer.wms.produce.manage.service.impl;

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.dao.MaterialsInfoMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MaterialsInfoService;

import com.deer.wms.produce.manage.service.MaterialsOutgoingLogService;
import com.deer.wms.produce.manage.service.MaterialsStockInfoService;
import com.deer.wms.project.seed.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by  on 2019/07/18.
 */
@Service
@Transactional
public class MaterialsInfoServiceImpl extends AbstractService<MaterialsInfo, Integer> implements MaterialsInfoService {

    @Autowired
    private MaterialsInfoMapper materialsInfoMapper;

    @Autowired
    private MaterialsStockInfoService materialsStockInfoService;

    @Autowired
    private MaterialsOutgoingLogService materialsOutgoingLogService;

    @Override
    public List<MaterialsInfoDto> findList(MaterialsInfoParams params) {
        return materialsInfoMapper.findList(params);
    }

    @Override
    public MaterialsInfoDto findDetailById(MaterialsInfoParams params) {
        return materialsInfoMapper.findDetailById(params);
    }

    @Override
    public List<MaterialsInfoVO> findMaterialByNameAndNum(MaterialsInfoParams params) {
        return materialsInfoMapper.findMaterialByNameOrNum(params);
    }

    @Override
    public List<MaterialsInfoVO> findMaterialsByKeyWords(MaterialsInfoParams params) {
        return materialsInfoMapper.findMaterialsByKeyWords(params);
    }

    @Override
    public void deleteRelevantById(Integer materialsId, CurrentUser currentUser) {
        materialsInfoMapper.deleteByPrimaryKey(materialsId);
        MaterialsStockInfo stock = materialsStockInfoService.findBy("materialsId", materialsId);
        if(null!=stock)
            materialsStockInfoService.deleteById(stock.getId());
        MaterialsOutgoingLogParams params = new MaterialsOutgoingLogParams();
        params.setMaterialsId(materialsId);
        params.setCompanyId(currentUser.getCompanyId());
        List<MaterialsOutgoingLogVo> materialsOutgoingLogList = materialsOutgoingLogService.findListByOneMaterial(params);

        if(null!=materialsOutgoingLogList && materialsOutgoingLogList.size()!=0){
            List<Integer> ids = new ArrayList<Integer>();
            for (MaterialsOutgoingLogVo materialsOutgoingLogVo : materialsOutgoingLogList){
                ids.add(materialsOutgoingLogVo.getId());
            }

            if(null!=ids && ids.size()!=0) {
                HashMap outgoingParam = new HashMap();
                outgoingParam.put("companyId", currentUser.getCompanyId());
                outgoingParam.put("ids", ids);
                materialsOutgoingLogService.deleteByOutgoingIds(outgoingParam);
            }
        }

    }


}
