package com.huimv.admin.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.DataUill;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.ProdProduction;
import com.huimv.admin.entity.ProdSale;
import com.huimv.admin.entity.ProdStock;
import com.huimv.admin.entity.vo.ProdStockVo;
import com.huimv.admin.mapper.ProdProductionMapper;
import com.huimv.admin.mapper.ProdSaleMapper;
import com.huimv.admin.mapper.ProdStockMapper;
import com.huimv.admin.service.IProdStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import netscape.javascript.JSObject;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存栏结构 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@Service
public class ProdStockServiceImpl extends ServiceImpl<ProdStockMapper, ProdStock> implements IProdStockService {

    @Autowired
    private ProdStockMapper prodStockMapper;
    @Autowired
    private ProdProductionMapper productionMapper;
    @Autowired
    private ProdSaleMapper saleMapper;


    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        QueryWrapper<ProdStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间
        //获取前12个月的第一天
        queryWrapper.ge("creat_time", DateUtil.offsetMonth(new Date(),-12));
        queryWrapper.orderByAsc("creat_time");
        List<ProdStock> prodStocks = prodStockMapper.listDay(queryWrapper);

        QueryWrapper<ProdStock> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId).orderByDesc("creat_time").last(" limit 1");
        queryWrapper1.eq("display_type",Integer.parseInt(displayType));
        ProdStock prodStock = prodStockMapper.selectOne(queryWrapper1);
        if (ObjectUtil.isEmpty(prodStock)){
            prodStock = new ProdStock();
            prodStock.setFarmId(Integer.valueOf(farmId));
            prodStock.setDisplayType(Integer.valueOf(displayType));
            prodStock.setZcl(0);
            prodStock.setYfzcl(0);
            prodStock.setByzcl(0);
            prodStock.setZzcl(0);
            prodStock.setBrmzcl(0);
            prodStock.setBrzcl(0);
            prodStock.setCreatTime(new Date());
            prodStock.setGzcl(0);
            prodStock.setHbzcl(0);
            prodStock.setKhzcl(0);
            prodStock.setMzcl(0);
            prodStock.setRszcl(0);
            prodStock.setSpzcl(0);
            prodStockMapper.insert(prodStock);
        }
        ProdStockVo prodStockVo = new ProdStockVo();
        prodStockVo.setCount(0);
        prodStockVo.setProdStock(prodStock);
        prodStockVo.setProdStocks(prodStocks);
        prodStockVo.setMzclTRate("0");
        prodStockVo.setGzclTRate("0");
        prodStockVo.setBrzclTRate("0");
        prodStockVo.setByzclTRate("0");
        prodStockVo.setYfzclRate("0");
        prodStockVo.setHbzclRate("0");
        prodStockVo.setRczclTRate("0");
        prodStockVo.setBrmzclRate("0");
        prodStockVo.setKhzclRate("0");
        prodStockVo.setSpzclRate("0");
        prodStockVo.setZclRate("0");
        prodStockVo.setZzclRate("0");
        Integer count = 0;
        if (ObjectUtil.isNotEmpty(prodStock)){
            count =prodStock.getMzcl() + prodStock.getGzcl() + prodStock.getBrzcl() + prodStock.getByzcl() + prodStock.getYfzcl() +
                    prodStock.getHbzcl() + prodStock.getRszcl() + prodStock.getBrmzcl() + prodStock.getKhzcl() + prodStock.getSpzcl()+prodStock.getZzcl();
            NumberFormat nt = NumberFormat.getPercentInstance();
            nt.setMinimumFractionDigits(2);
            prodStockVo.setCount(count);
            prodStockVo.setProdStock(prodStock);
            prodStockVo.setProdStocks(prodStocks);
            prodStockVo.setMzclTRate(nt.format((double)prodStock.getMzcl()/count));
            prodStockVo.setGzclTRate(nt.format((double) prodStock.getGzcl() / count));
            prodStockVo.setBrzclTRate(nt.format((double) prodStock.getBrzcl() / count));
            prodStockVo.setByzclTRate(nt.format((double) prodStock.getByzcl() / count));
            prodStockVo.setYfzclRate(nt.format((double) prodStock.getYfzcl() / count));
            prodStockVo.setHbzclRate(nt.format((double) prodStock.getHbzcl() / count));
            prodStockVo.setRczclTRate(nt.format((double) prodStock.getRszcl() / count));
            prodStockVo.setBrmzclRate(nt.format((double) prodStock.getBrmzcl() / count));
            prodStockVo.setKhzclRate(nt.format((double) prodStock.getKhzcl() / count));
            prodStockVo.setSpzclRate(nt.format((double) prodStock.getSpzcl() / count));

            prodStockVo.setZclRate(nt.format((double) prodStock.getZcl() / count));
            prodStockVo.setZzclRate(nt.format((double) prodStock.getZzcl() / count));
        }

        return new Result(ResultCode.SUCCESS,prodStockVo);
    }

    @Override
    public Result listStockScreen(HttpServletRequest httpServletRequest, Map<String, String> paramaMap) {
        String farmId = paramaMap.get("farmId");
        String displayType = paramaMap.get("displayType");
        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        QueryWrapper<ProdStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.orderByDesc("creat_time").last(" limit 1");
        ProdStock prodStock = prodStockMapper.selectOne(queryWrapper);
        JSONObject jsonObject = new JSONObject();

        QueryWrapper<ProdProduction> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId).orderByDesc("creat_time").last(" limit 1");
        ProdProduction prodProduction = productionMapper.selectOne(queryWrapper1);

        if (ObjectUtil.isEmpty(prodStock)) {
            jsonObject.put("yfcl", 0);
            jsonObject.put("bycl", 0);
        } else {
            jsonObject.put("yfcl", prodStock.getYfzcl());
            jsonObject.put("bycl", prodStock.getByzcl());
        }
        if (ObjectUtil.isEmpty(prodProduction)) {
            jsonObject.put("chl", 0);
        } else {
            jsonObject.put("chl", prodProduction.getBychl());
        }
        Date timesMonthmorning = DataUill.getTimesMonthmorning();
        QueryWrapper<ProdSale> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("display_type",Integer.parseInt(displayType));
        queryWrapper2.eq("farm_id", farmId).ge("creat_time", timesMonthmorning);
        Integer count = 0;
        List<ProdSale> prodSales = saleMapper.selectList(queryWrapper2);
        if (prodSales.size() == 0) {
            count = 0;
        } else {
            for (ProdSale prodSale : prodSales) {
                count = count + prodSale.getSaleQuantity();
            }
        }

        DateTime dateTime = DateUtil.beginOfYear(new Date());
        QueryWrapper<ProdSale> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("farm_id", farmId).ge("creat_time",dateTime);
        queryWrapper3.eq("display_type",Integer.parseInt(displayType));
        Integer yearCount = 0;
        List<ProdSale> yearProdSales = saleMapper.selectList(queryWrapper3);
        if (yearProdSales.size() == 0) {
            yearCount = 0;
        } else {
            for (ProdSale prodSale : yearProdSales) {
                yearCount = yearCount + prodSale.getSaleQuantity();
            }
        }

        jsonObject.put("monthCount", count);
        jsonObject.put("yearCount", yearCount);
        return new Result(ResultCode.SUCCESS,jsonObject);
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, ProdStock prodStock) {
        prodStockMapper.insert(prodStock);
        return Result.SUCCESS();
    }
}
