package com.huimv.produce.produce.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.ProdWriteStock;
import com.huimv.produce.produce.mapper.ProdWriteStockMapper;
import com.huimv.produce.produce.service.ProdWriteStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zn
 * @since 2022-11-08
 */
@Service
public class ProdWriteStockServiceImpl extends ServiceImpl<ProdWriteStockMapper, ProdWriteStock> implements ProdWriteStockService {

    @Autowired
    private static final String globalFarmCode = "331023001";
    @Autowired
    private ProdWriteStockMapper prodWriteStockMapper;

    @Override
    public Result list(Map<String,String> paramsMap) {
        String farmId = paramsMap.get("farm_id");
        if (farmId==null) {
            farmId = globalFarmCode;
        }
        QueryWrapper<ProdWriteStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        ProdWriteStock prodWriteStock1 = prodWriteStockMapper.selectOne(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        if (prodWriteStock1 == null) {
            jsonObject.put("addTime", 0);
            jsonObject.put("allStock", 0);
            jsonObject.put("pigletStock", 0);
            jsonObject.put("sowStock", 0);
            jsonObject.put("fatStock", 0);
            jsonObject.put("boarStock", 0);
            jsonObject.put("outStock", 0);
        } else {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonObject.put("addTime", format.format(prodWriteStock1.getAddTime()));
            jsonObject.put("allStock", prodWriteStock1.getAllStock());
            jsonObject.put("pigletStock", prodWriteStock1.getPigletStock());
            jsonObject.put("sowStock", prodWriteStock1.getSowStock());
            jsonObject.put("fatStock", prodWriteStock1.getFatStock());
            jsonObject.put("boarStock", prodWriteStock1.getBoarStock());
            jsonObject.put("outStock", prodWriteStock1.getOutStock());
        }
        return new Result(ResultCode.SUCCESS,jsonObject);
    }

    @Override
    public Result add(ProdWriteStock prodWriteStock) {
        //保持只有一条最新的填报数据
        QueryWrapper<ProdWriteStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", globalFarmCode);
        prodWriteStockMapper.delete(queryWrapper);

        ProdWriteStock prodWriteStock1 = new ProdWriteStock();
        prodWriteStock1.setFarmId(Integer.parseInt(globalFarmCode));
        long timeMillis = System.currentTimeMillis();
        prodWriteStock1.setAddTime(new Date(timeMillis));
        prodWriteStock1.setAllStock(prodWriteStock.getAllStock());
        prodWriteStock1.setPigletStock(prodWriteStock.getPigletStock());
        prodWriteStock1.setSowStock(prodWriteStock.getSowStock());
        prodWriteStock1.setFatStock(prodWriteStock.getFatStock());
        prodWriteStock1.setBoarStock(prodWriteStock.getBoarStock());
        prodWriteStock1.setOutStock(prodWriteStock.getOutStock());

        prodWriteStockMapper.insert(prodWriteStock1);
        return new Result(ResultCode.SUCCESS,"填报成功");
    }
}
