package com.huimv.admin.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.EnvDeviceMaintain;
import com.huimv.admin.mapper.EnvDeviceMaintainMapper;
import com.huimv.admin.service.IEnvDeviceMaintainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 设备维修记录 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-13
 */
@Service
public class EnvDeviceMaintainServiceImpl extends ServiceImpl<EnvDeviceMaintainMapper, EnvDeviceMaintain> implements IEnvDeviceMaintainService {

    @Autowired
    private EnvDeviceMaintainMapper envDeviceMaintainMapper;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String deviceId = paramsMap.get("deviceId");
        QueryWrapper<EnvDeviceMaintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId).eq("farm_id", farmId).orderByDesc("maintain_time");
        return new Result(ResultCode.SUCCESS,envDeviceMaintainMapper.selectList(queryWrapper));
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String deviceId = paramsMap.get("deviceId");
        String maintainContent = paramsMap.get("maintainContent");
        String remark = paramsMap.get("remark");

        Date dateTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(dateTime);
        Date date = null;
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = simpleDateFormat1.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EnvDeviceMaintain envDeviceMaintain = new EnvDeviceMaintain();
        envDeviceMaintain.setMaintainContent(maintainContent);
        envDeviceMaintain.setDeviceId(deviceId);
        envDeviceMaintain.setFarmId(Integer.parseInt(farmId));
        envDeviceMaintain.setRemark(remark);
        envDeviceMaintain.setMaintainTime(date);

        envDeviceMaintainMapper.insert(envDeviceMaintain);
        return new Result(ResultCode.SUCCESS,"添加成功");
    }

}
