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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.domain.EnterpriseDriverFile;
import com.bonus.sgzb.base.mapper.EnterpriseDriverFileMapper;
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 com.bonus.sgzb.material.config.FieldGenerator;
import lombok.extern.slf4j.Slf4j;
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
 */
@Slf4j
@Service
public class MaReceiveServiceImpl implements MaReceiveService {

    @Resource
    private MaReceiveMapper maReceiveMapper;

    @Resource
    private MaMachineMapper maMachineMapper;

    @Resource
    private MaTypeMapper maTypeMapper;

    @Resource
    private EnterpriseDriverFileMapper enterpriseDriverFileMapper;

    @Override
    @Transactional
    public int saveDataReceiveInfo(String maMachineRequest) {
        log.info("推送数据{}", maMachineRequest);
        JSONObject object = JSONObject.parseObject(maMachineRequest);
        maMachineRequest = object.getString("body");
        JSONArray dataArray = JSONArray.parseArray(maMachineRequest);
        List<DataReceiveDetail> dataReceiveDetails = dataArray.toJavaList(DataReceiveDetail.class);
        DataReceiveInfo dataReceiveInfo = new DataReceiveInfo();
        dataReceiveInfo.setPushNum(dataReceiveDetails.size());
        maReceiveMapper.saveDataReceiveInfo(dataReceiveInfo);
        if (dataReceiveInfo.getId() != null) {
            for (DataReceiveDetail dataReceiveDetail : dataReceiveDetails) {
                dataReceiveDetail.setReceiveId(dataReceiveInfo.getId());
                saveDataReceiveDetails(dataReceiveDetail);
            }
            return dataReceiveInfo.getId();
        } else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int saveDataReceiveDetails(DataReceiveDetail dataReceiveDetail) {
        log.info("保存推送详情{}", dataReceiveDetail);
        int i = 0;
        //保存推送详情
        i = maReceiveMapper.saveDataReceiveDetails(dataReceiveDetail);
        if (i == 0) {
            throw new RuntimeException("保存失败");
        }
        //保存机手附件信息
        List<EnterpriseDriverFile> enterpriseDriverFileList = dataReceiveDetail.getEnterpriseDriverFileList();
        if (enterpriseDriverFileList != null && !enterpriseDriverFileList.isEmpty()){
            for (EnterpriseDriverFile enterpriseDriverFile : enterpriseDriverFileList) {
                enterpriseDriverFile.setReceiveDetailId(dataReceiveDetail.getId());
                enterpriseDriverFileMapper.insertEnterpriseDriverFile(enterpriseDriverFile);
            }
        }
        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) {
        List<DataReceiveDetail> dataReceiveDetails = maReceiveMapper.getDataReceiveDetails(dataReceiveDetail);
        dataReceiveDetails.forEach(dataReceiveDetail1 -> {
            dataReceiveDetail1.setEnterpriseDriverFileList(enterpriseDriverFileMapper.selectEnterpriseDriverFileByReceiveDetailId(dataReceiveDetail1.getId()));
        });
        return dataReceiveDetails;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveMachine(DataReceiveInfo dataReceiveInfo) {
        for (DataReceiveDetail dataReceiveDetail : dataReceiveInfo.getDataReceiveDetailList()) {
            MaMachine maMachine = new MaMachine();
            maMachine.setMaCode(FieldGenerator.generateField());
//            maMachine.setTypeId(dataReceiveDetail.getTypeId());
            maMachine.setTypeId(1763);
            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;
    }

    @Override
    public Long getItemStatus(String itemId) {
        log.info("获取状态{}", itemId);
        JSONObject object = JSONObject.parseObject(itemId);
        itemId = object.getString("body");
        DataReceiveDetail dataReceiveDetail = JSONObject.parseObject(itemId, DataReceiveDetail.class);
        return maMachineMapper.selectMaMachineByItemId(dataReceiveDetail.getItemId());
    }



    public Integer updateItemStatus(String dataReceiveDetailReq) {
        log.info("修改状态{}", dataReceiveDetailReq);
        JSONObject object = JSONObject.parseObject(dataReceiveDetailReq);
        dataReceiveDetailReq = object.getString("body");
        DataReceiveDetail dataReceiveDetail = JSONObject.parseObject(dataReceiveDetailReq, DataReceiveDetail.class);
        if (dataReceiveDetail!=null && dataReceiveDetail.getOrderOverTime()!= null){
            return   maReceiveMapper.updateOrderOverTime(dataReceiveDetail);
        }else {
            return maMachineMapper.updateItemStatus(dataReceiveDetail);
        }
    }

}
