package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.base.domain.DataReceiveDetail;
import com.bonus.sgzb.base.domain.DataReceiveInfo;
import com.bonus.sgzb.base.mapper.MaMachineMapper;
import com.bonus.sgzb.base.mapper.MaReceiveMapper;
import com.bonus.sgzb.base.mapper.MaTypeMapper;
import com.bonus.sgzb.base.service.MaReceiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author：liang.chao
 * @Date：2024/7/24 - 13:20
 */
@Service
public class MaReceiveServiceImpl implements MaReceiveService {

    @Resource
    private MaReceiveMapper maReceiveMapper;

    @Resource
    private MaMachineMapper maMachineMapper;

    @Resource
    private MaTypeMapper maTypeMapper;

    @Override
    @Transactional
    public int saveDataReceiveInfo(DataReceiveInfo dataReceiveInfo) {
        maReceiveMapper.saveDataReceiveInfo(dataReceiveInfo);
        if (dataReceiveInfo.getId() != null) {
            return dataReceiveInfo.getId();
        } else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int saveDataReceiveDetails(DataReceiveDetail dataReceiveDetail) {
        int i = 0;
        i = maReceiveMapper.saveDataReceiveDetails(dataReceiveDetail);
        if (i == 0) {
            throw new RuntimeException("保存失败");
        }
        return i;
    }

    @Override
    public List<DataReceiveInfo> getDataReceive(DataReceiveInfo dataReceiveInfo) {
        List<DataReceiveInfo> dataReceive = maReceiveMapper.getDataReceive(dataReceiveInfo);
        for (DataReceiveInfo receiveInfo : dataReceive) {
            List<DataReceiveDetail> dataReceiveDetails = maReceiveMapper.getDataReceiveDetailsById(receiveInfo.getId());
            long num = dataReceiveDetails.stream()
                    .filter(t -> t.getStatus() == 1)
                    .count();
            receiveInfo.setReceiveNum((int) num);
        }
        return dataReceive;
    }

    @Override
    public List<DataReceiveDetail> getDataReceiveDetails(DataReceiveDetail dataReceiveDetail) {
        return maReceiveMapper.getDataReceiveDetails(dataReceiveDetail);
    }

    @Override
    public int saveMachine(DataReceiveInfo dataReceiveInfo) {
        for (DataReceiveDetail dataReceiveDetail : dataReceiveInfo.getDataReceiveDetailList()) {
            MaMachine maMachine = new MaMachine();
            maMachine.setMaCode(dataReceiveDetail.getMaCode());
            maMachine.setTypeId(dataReceiveDetail.getTypeId());
            maMachine.setMaCode(dataReceiveDetail.getMaCode());
            maMachine.setMaStatus("15");
            maMachine.setCreateTime(new Date());
            maMachine.setSouceBy(2);
            maMachine.setDataReceiveId(dataReceiveInfo.getId());
            // 添加机具
            int i = maMachineMapper.maMachineAdd(maMachine);
            if (i == 0) {
                return i;
            }
            // 增加库存
            int j = maTypeMapper.updateTypeNum(maMachine);
            if (j == 0) {
                return j;
            }
            // 修改状态为已接收
            dataReceiveDetail.setMaId((int) maMachine.getMaId());
            int k = maReceiveMapper.updateStatus(dataReceiveDetail);
            if (k == 0) {
                return k;
            }
        }
        List<DataReceiveDetail> dataReceiveDetails = maReceiveMapper.getDataReceiveDetailsById(dataReceiveInfo.getId());
        if (dataReceiveDetails.stream().allMatch(t -> t.getStatus() == 1)) {
            int i = maReceiveMapper.updateInfoStatus(dataReceiveInfo.getId());
            if (i == 0) {
                return i;
            }
        }
        return 1;

    }

    @Override
    public List<DataReceiveDetail> getDateReceiveMachine(DataReceiveDetail dataReceiveDetail) {
        List<DataReceiveDetail> dateReceiveMachine = maReceiveMapper.getDateReceiveMachine(dataReceiveDetail);
        return dateReceiveMachine;
    }
}
