package com.huimv.admin.service.impl;

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,"添加成功");
    }

}

