package com.huimv.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.ProdProduction;
import com.huimv.admin.mapper.ProdProductionMapper;
import com.huimv.admin.service.IProdProductionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
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 ProdProductionServiceImpl extends ServiceImpl<ProdProductionMapper, ProdProduction> implements IProdProductionService {

    @Autowired
    private ProdProductionMapper productionMapper;
    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");
        QueryWrapper<ProdProduction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        if (StringUtils.isNullOrEmpty(startTime) || StringUtils.isNullOrEmpty(endTime)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间
            //获取前12个月的第一天
            Calendar cal_1 = Calendar.getInstance();//获取当前日期
            cal_1.add(Calendar.MONTH, -12);
            cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
            cal_1.set(Calendar.HOUR_OF_DAY, 0);
            cal_1.set(Calendar.MINUTE, 0);
            cal_1.set(Calendar.SECOND, 0);
            String firstDay = format.format(cal_1.getTime());
            queryWrapper.ge("creat_time", firstDay);
        } else {
            startTime = startTime +"-01-" + " 00:00:00";
            String[] split = endTime.split("-");
            if (split[1].equals("01") || split[1].equals("03") || split[1].equals("05")
                    || split[1].equals("07") || split[1].equals("08") || split[1].equals("10") || split[1].equals("12")) {
                endTime = endTime + "-31-"+" 23:59:59";
            } else {
                endTime = endTime + "-30-"+" 23:59:59";
            }
            queryWrapper.between("creat_time", startTime, endTime);
        }

        List<ProdProduction> prodProductions = productionMapper.listDay(queryWrapper);
        return new Result(ResultCode.SUCCESS,prodProductions);
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, ProdProduction prodProduction) {
        productionMapper.insert(prodProduction);
        return new Result(ResultCode.SUCCESS,"添加成功");
    }

    @Override
    public Result listProd(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<ProdProduction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.orderByDesc("creat_time").last(" limit 1");
        ProdProduction prodProduction = productionMapper.selectOne(queryWrapper);
        return new Result(ResultCode.SUCCESS,prodProduction);
    }

    @Override
    public Result addProd(HttpServletRequest httpServletRequest, ProdProduction prodProduction) {
        prodProduction.setCreatTime(new Date());
        productionMapper.insert(prodProduction);
        return new Result(ResultCode.SUCCESS,"添加成功");
    }

    @Override
    public Result listScreenProd(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<ProdProduction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.orderByDesc("creat_time").last(" limit 1");
        ProdProduction prodProduction = productionMapper.selectOne(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        if ("".equals(prodProduction.getOther1()) || null == prodProduction.getOther1()) {
            for (int i = 0; i < 4; i++) {
                JSONObject jsonObject = new JSONObject();
                if (i == 0) {
                    jsonObject.put("name", "产房成活率");
                    jsonObject.put("value", prodProduction.getCfchl());
                    jsonObject.put("unit", "%");
                }
                if (i == 1) {
                    jsonObject.put("name", "保育成活率");
                    jsonObject.put("value",prodProduction.getBychl());
                    jsonObject.put("unit", "%");
                }
                if (i == 2) {
                    jsonObject.put("name", "育肥成活率");
                    jsonObject.put("value",prodProduction.getYfchl());
                    jsonObject.put("unit", "%");
                }
                if (i == 3) {
                    jsonObject.put("name", "肥猪成活率");
                    jsonObject.put("value",prodProduction.getFzchl());
                    jsonObject.put("unit", "%");
                }
                jsonArray.add(jsonObject);
            }
        } else {
            String[] split = prodProduction.getOther1().split(",");
            for (String s : split) {
                JSONObject jsonObject = new JSONObject();
                if ("1".equals(s)) {
                    jsonObject.put("name", "产房成活率");
                    jsonObject.put("value", prodProduction.getCfchl());
                    jsonObject.put("unit", "%");
                }
                if ("2".equals(s)) {
                    jsonObject.put("name", "保育成活率");
                    jsonObject.put("value",prodProduction.getBychl());
                    jsonObject.put("unit", "%");
                }
                if ("3".equals(s)) {
                    jsonObject.put("name", "育肥成活率");
                    jsonObject.put("value",prodProduction.getYfchl());
                    jsonObject.put("unit", "%");
                }
                if ("4".equals(s)) {
                    jsonObject.put("name", "肥猪成活率");
                    jsonObject.put("value",prodProduction.getFzchl());
                    jsonObject.put("unit", "%");
                }
                if ("5".equals(s)) {
                    jsonObject.put("name", "每日喂料量");
                    jsonObject.put("value",prodProduction.getMrwll());
                    jsonObject.put("unit", "kg");
                }
                if ("6".equals(s)) {
                    jsonObject.put("name", "日增重");
                    jsonObject.put("value",prodProduction.getRzz());
                    jsonObject.put("unit", "kg");
                }
                if ("7".equals(s)) {
                    jsonObject.put("name", "7日断配率");
                    jsonObject.put("value",prodProduction.getQrdpl());
                    jsonObject.put("unit", "%");
                }
                if ("8".equals(s)) {
                    jsonObject.put("name", "配种分娩率");
                    jsonObject.put("value",prodProduction.getPzfml());
                    jsonObject.put("unit", "%");
                }
                if ("9".equals(s)) {
                    jsonObject.put("name", "平均窝产仔数(只)");
                    jsonObject.put("value",prodProduction.getPjwczs());
                }
                if ("10".equals(s)) {
                    jsonObject.put("name", "平均窝产活仔数(只)");
                    jsonObject.put("value",prodProduction.getPjwchzs());
                }
                if ("11".equals(s)) {
                    jsonObject.put("name", "平均窝产正常仔数(只)");
                    jsonObject.put("value",prodProduction.getPjwczczs());
                }
                if ("12".equals(s)) {
                    jsonObject.put("name", "窝断奶仔猪数(只)");
                    jsonObject.put("value",prodProduction.getWdnzzs());
                }
                if ("13".equals(s)) {
                    jsonObject.put("name", "平均窝断奶窝重");
                    jsonObject.put("value",prodProduction.getPjwdnz());
                    jsonObject.put("unit", "kg");
                }
                if ("14".equals(s)) {
                    jsonObject.put("name", "非生产天数");
                    jsonObject.put("value",prodProduction.getFccts());
                    jsonObject.put("unit", "天");
                }
                if ("15".equals(s)) {
                    jsonObject.put("name", "年产窝数(只)");
                    jsonObject.put("value",prodProduction.getScws());
                }
                if ("16".equals(s)) {
                    jsonObject.put("name", "PSY");
                    jsonObject.put("value",prodProduction.getPsy());
                }
                jsonArray.add(jsonObject);
            }
        }
        return new Result(ResultCode.SUCCESS,jsonArray);
    }

    @Override
    public Result addScreenProd(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String other1 = paramsMap.get("other1");
        QueryWrapper<ProdProduction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.orderByDesc("creat_time").last(" limit 1");
        ProdProduction prodProduction = productionMapper.selectOne(queryWrapper);
        prodProduction.setOther1(other1);
        productionMapper.updateById(prodProduction);
        return new Result(10000, "修改成功！", true);
    }

}

