package com.huimv.receiver.farm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.receiver.farm.dao.entity.ProdBatchWeightEntity;
import com.huimv.receiver.farm.dao.entity.ProdDayWeightEntity;
import com.huimv.receiver.farm.dao.entity.ProdWeightEntity;
import com.huimv.receiver.farm.dao.repo.ProdBatchWeightRepo;
import com.huimv.receiver.farm.dao.repo.ProdDayWeightRepo;
import com.huimv.receiver.farm.dao.repo.ProdWeightRepo;
import com.huimv.receiver.farm.service.IWeight;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Optional;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
@Slf4j
public class WeightImpl implements IWeight {
    @Autowired
    private ProdDayWeightRepo dayWeightRepo;
    @Autowired
    private ProdBatchWeightRepo batchWeightRepo;
    @Autowired
    private ProdWeightRepo prodWeightRepo;

    /**
     * @Method      : saveDayWeight
     * @Description : 保存日总重数据
     * @Params      : [data]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/12/7       
     * @Time        : 19:48
     */
    @Override
    public void saveDayWeight(String data) {
        log.info("saveDayWeight>>"+data);
        JSONObject dayWeightJo = JSONObject.parseObject(data);
        Long date = dayWeightJo.getLong("addTime");
        Integer farmId = dayWeightJo.getInteger("farmId");
        //
        Optional<ProdDayWeightEntity> optional = dayWeightRepo.findByDateAndFarmId(new Date(date),farmId);
        log.info("optional.isPresent>>"+optional.isPresent());
        if(optional.isPresent()){
            ProdDayWeightEntity existDayWeightEntity = optional.get();
            existDayWeightEntity.setAddTime(new Timestamp(new Date().getTime()));
            existDayWeightEntity.setGrossWeight(dayWeightJo.getFloat("grossWeight"));
            existDayWeightEntity.setTareWeight(dayWeightJo.getFloat("tareWeight"));
            existDayWeightEntity.setNetWeight(dayWeightJo.getFloat("netWeight"));
            dayWeightRepo.saveAndFlush(existDayWeightEntity);
            log.info("编辑每天总重>>"+existDayWeightEntity);
        }else{
            ProdDayWeightEntity newDayWeightEntity = new ProdDayWeightEntity();
            newDayWeightEntity.setAddTime(new Timestamp(new Date().getTime()));
            newDayWeightEntity.setFarmId(dayWeightJo.getInteger("farmId"));
            newDayWeightEntity.setGrossWeight(dayWeightJo.getFloat("grossWeight"));
            newDayWeightEntity.setTareWeight(dayWeightJo.getFloat("tareWeight"));
            newDayWeightEntity.setNetWeight(dayWeightJo.getFloat("netWeight"));
            dayWeightRepo.saveAndFlush(newDayWeightEntity);
            log.info("新增每天总重>>"+newDayWeightEntity);
        }

    }

    /**
     * @Method      : saveBatchWeight
     * @Description : 保存批次重量数据
     * @Params      : [data]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/12/7       
     * @Time        : 19:49
     */
    @Override
    public void saveBatchWeight(String data) {
        JSONObject batchWeightJo = JSONObject.parseObject(data);
        String batchCode = batchWeightJo.getString("batchCode");
        System.out.println("batchCode>>"+batchCode);
        //查找批次称重数据
        Optional<ProdBatchWeightEntity> optional = batchWeightRepo.findByBatchCode(batchCode);
        log.info("optional.isPresent>>"+optional.isPresent());
        if(!optional.isPresent()){
            ProdBatchWeightEntity newBatchWeightEntity = new ProdBatchWeightEntity();
            newBatchWeightEntity.setBatchCode(batchWeightJo.getString("batchCode"));
            newBatchWeightEntity.setBatchSort(batchWeightJo.getInteger("batchSort"));
            newBatchWeightEntity.setFarmId(batchWeightJo.getInteger("farmId"));
            newBatchWeightEntity.setGrossWeight(batchWeightJo.getFloat("grossWeight"));
            newBatchWeightEntity.setTareWeight(batchWeightJo.getFloat("tareWeight"));
            newBatchWeightEntity.setNetWeight(batchWeightJo.getFloat("netWeight"));
            newBatchWeightEntity.setAddTime(new Timestamp(new Date().getTime()));
            batchWeightRepo.saveAndFlush(newBatchWeightEntity);
            log.info("新增批次称重数据>>"+newBatchWeightEntity);
        }else{
            ProdBatchWeightEntity existBatchWeightEntity = optional.get();
            existBatchWeightEntity.setGrossWeight(batchWeightJo.getFloat("grossWeight"));
            existBatchWeightEntity.setTareWeight(batchWeightJo.getFloat("tareWeight"));
            existBatchWeightEntity.setNetWeight(batchWeightJo.getFloat("netWeight"));
            batchWeightRepo.saveAndFlush(existBatchWeightEntity);
            log.info("编辑批次称重数据>>"+existBatchWeightEntity);
        }
    }

    /**
     * @Method      : saveEveryTimeWeight
     * @Description : 保存每次称重数据
     * @Params      : [data]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/12/22       
     * @Time        : 18:28
     */
    @Override
    public void saveEveryTimeWeight(String data) {
        JSONObject everyTimeWeightJo = JSONObject.parseObject(data);
//        ProdWeightEntity newWeightEntity = JSON.toJavaObject(everyTimeWeightJo,ProdWeightEntity.class);
//        ProdWeightEntity addWeightEntity = prodWeightRepo.saveAndFlush(newWeightEntity);
        ProdWeightEntity newWeightEntity = new ProdWeightEntity();
        newWeightEntity.setFarmId(everyTimeWeightJo.getInteger("farmId"));
        newWeightEntity.setGrossWeight(everyTimeWeightJo.getFloat("grossWeight"));
        newWeightEntity.setTareWeight(everyTimeWeightJo.getFloat("tareWeight"));
        newWeightEntity.setNetWeight(everyTimeWeightJo.getFloat("netWeight"));
        newWeightEntity.setAddTime(everyTimeWeightJo.getTimestamp("addTime"));
        ProdWeightEntity addWeightEntity = prodWeightRepo.saveAndFlush(newWeightEntity);
        log.info("保存每次称重>>"+addWeightEntity);
    }
}
