package com.fastbee.rechargecard.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.mapper.DeviceMapper;
import com.fastbee.rechargecard.domain.NgIrrigationControllers;
import com.fastbee.rechargecard.domain.UserConsumptionDetails;
import com.fastbee.rechargecard.domain.dto.UserIrrigationRecordDto;
import com.fastbee.rechargecard.mapper.NgIrrigationControllersMapper;
import com.fastbee.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.rechargecard.mapper.UserIrrigationRecordMapper;
import com.fastbee.rechargecard.domain.UserIrrigationRecord;
import com.fastbee.rechargecard.service.IUserIrrigationRecordService;

/**
 * 灌溉记录Service业务层处理
 *
 * @author kerwincui
 * @date 2024-12-18
 */
@Service
public class UserIrrigationRecordServiceImpl implements IUserIrrigationRecordService
{
    @Autowired
    private UserIrrigationRecordMapper userIrrigationRecordMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NgIrrigationControllersMapper ngIrrigationControllersMapper;


    @Override
    public List<UserIrrigationRecordDto> selectUserIrrigationRecordShowListByCardNumber(UserIrrigationRecord userIrrigationRecord) {
        List<UserIrrigationRecordDto> result=new ArrayList<>();
        List<UserIrrigationRecord> list=userIrrigationRecordMapper.selectUserIrrigationRecordShowListBycardNumber(userIrrigationRecord);
        for(int i=0;i<list.size();i++)
        {
            UserIrrigationRecordDto temp=new UserIrrigationRecordDto();
            temp.id=list.get(i).getId();
            if(list.get(i).getUserId()!=null)
            {
                temp.userName= sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName()==null ? "" :sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName();

            }
            if(list.get(i).getDeviceNumber()!=null)
            {
                temp.deviceName=ngIrrigationControllersMapper.selectNgIrrigationControllersBySerialNumber(list.get(i).getDeviceNumber()).getControllerName() == null ? "":ngIrrigationControllersMapper.selectNgIrrigationControllersBySerialNumber(list.get(i).getDeviceNumber()).getControllerName();

            }
           temp.cardNumber=list.get(i).getCardNumber() == null ? "" : list.get(i).getCardNumber();
            temp.flow=list.get(i).getCurFlow()==null ? BigDecimal.valueOf(0) :list.get(i).getCurFlow();
            temp.startTime=list.get(i).getStartTime()==null ? null : list.get(i).getStartTime();
            temp.endTime=list.get(i).getEndTime()==null ? null :list.get(0).getEndTime();
            result.add(temp);
        }

        return result;
    }

    @Override
    public List<UserIrrigationRecord> selectUserIrrigationRecordShowListByDeviceNumber(String deviceNumber) {
        List<UserIrrigationRecord> list=userIrrigationRecordMapper.selectUserIrrigationRecordListByDeviceNumber(deviceNumber);
        return list;
    }

    @Override
    public List<UserIrrigationRecordDto> selectUserIrrigationRecordShowList(UserIrrigationRecord userIrrigationRecord) {
        List<UserIrrigationRecordDto> result=new ArrayList<>();
        List<UserIrrigationRecord> list=userIrrigationRecordMapper.selectUserIrrigationRecordList(userIrrigationRecord);
        for(int i=0;i<list.size();i++)
        {
            UserIrrigationRecordDto temp=new UserIrrigationRecordDto();
            temp.id=list.get(i).getId();
            if(list.get(i).getUserId()!=null)
            {
                temp.userName= sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName()==null ? "" :sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName();

            }
            if(list.get(i).getDeviceNumber()!=null)
            {
                temp.deviceName=ngIrrigationControllersMapper.selectNgIrrigationControllersBySerialNumber(list.get(i).getDeviceNumber()).getControllerName() == null ? "":ngIrrigationControllersMapper.selectNgIrrigationControllersBySerialNumber(list.get(i).getDeviceNumber()).getControllerName();

            }
            temp.cardNumber=list.get(i).getCardNumber() == null ? "" : list.get(i).getCardNumber();
            temp.flow=list.get(i).getCurFlow()==null ? BigDecimal.valueOf(0) :list.get(i).getCurFlow();
            temp.startTime=list.get(i).getStartTime()==null ? null : list.get(i).getStartTime();
            temp.endTime=list.get(i).getEndTime()==null ? null :list.get(0).getEndTime();
            result.add(temp);
        }

        return result;
    }

    /**
     * 获取用户灌溉记录列表
     * @param cardNumber 卡号
     * @return
     */
    @Override
    public List<UserIrrigationRecordDto> selectUserIrrigationRecordListBycardNumber(String cardNumber)
    {
        //return userIrrigationRecordMapper.selectUserIrrigationRecordListBycardNumber(cardNumber);
        List<UserIrrigationRecordDto> result=new ArrayList<>();
        List<UserIrrigationRecord> list=userIrrigationRecordMapper.selectUserIrrigationRecordListBycardNumber(cardNumber);
        for(int i=0;i<list.size();i++)
        {
            UserIrrigationRecordDto temp=new UserIrrigationRecordDto();
            temp.id=list.get(i).getId();
            if(list.get(i).getUserId()!=null)
            {
                temp.userName= sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName();
            }
            if(list.get(i).getDeviceNumber()!=null)
            {
                temp.deviceName=ngIrrigationControllersMapper.selectNgIrrigationControllersBySerialNumber(list.get(i).getDeviceNumber()).getControllerName();

            }
            temp.cardNumber=list.get(i).getCardNumber();
            temp.flow=list.get(i).getCurFlow();
            temp.startTime=list.get(i).getStartTime();
            temp.endTime=list.get(i).getEndTime();
            result.add(temp);
        }

        return result;
    }

    /**
     * 获取用户灌溉记录展示数据列表
     * @param cardNumber 卡号
     * @return
     */
    /*@Override
    public List<UserIrrigationRecordDto> selectUserIrrigationRecordListShowBycardNumber(String cardNumber)
    {
        List<UserIrrigationRecordDto> result=new ArrayList<>();
        List<UserIrrigationRecord> list=userIrrigationRecordMapper.selectUserIrrigationRecordListBycardNumber(cardNumber);
        for(int i=0;i<list.size();i++)
        {
            UserIrrigationRecordDto temp=new UserIrrigationRecordDto();
            temp.id=list.get(i).getId();
            temp.userName= sysUserMapper.selectUserById(list.get(i).getUserId()).getUserName();
            temp.deviceName=deviceMapper.selectDeviceBySerialNumber(list.get(i).getDeviceNumber()).getDeviceName();
            temp.cardNumber=list.get(i).getCardNumber();
            temp.flow=list.get(i).getCurFlow();
            temp.startTime=list.get(i).getStartTime();
            temp.endTime=list.get(i).getEndTime();
            result.add(temp);
        }

        return result;
    }*/

    /**
     * 查询灌溉记录
     *
     * @param id 灌溉记录主键
     * @return 灌溉记录
     */
    @Override
    public UserIrrigationRecord selectUserIrrigationRecordById(Long id)
    {
        return userIrrigationRecordMapper.selectUserIrrigationRecordById(id);
    }

    /**
     * 查询灌溉记录列表
     *
     * @param userIrrigationRecord 灌溉记录
     * @return 灌溉记录
     */
    @Override
    public List<UserIrrigationRecord> selectUserIrrigationRecordList(UserIrrigationRecord userIrrigationRecord)
    {
        return userIrrigationRecordMapper.selectUserIrrigationRecordList(userIrrigationRecord);
    }

    /**
     * 新增灌溉记录
     *
     * @param userIrrigationRecord 灌溉记录
     * @return 结果
     */
    @Override
    public int insertUserIrrigationRecord(UserIrrigationRecord userIrrigationRecord)
    {
        userIrrigationRecord.setCreateTime(DateUtils.getNowDate());
        return userIrrigationRecordMapper.insertUserIrrigationRecord(userIrrigationRecord);
    }

    /**
     * 修改灌溉记录
     *
     * @param userIrrigationRecord 灌溉记录
     * @return 结果
     */
    @Override
    public int updateUserIrrigationRecord(UserIrrigationRecord userIrrigationRecord)
    {
        userIrrigationRecord.setUpdateTime(DateUtils.getNowDate());
        return userIrrigationRecordMapper.updateUserIrrigationRecord(userIrrigationRecord);
    }

    /**
     * 批量删除灌溉记录
     *
     * @param ids 需要删除的灌溉记录主键
     * @return 结果
     */
    @Override
    public int deleteUserIrrigationRecordByIds(Long[] ids)
    {
        return userIrrigationRecordMapper.deleteUserIrrigationRecordByIds(ids);
    }

    /**
     * 删除灌溉记录信息
     *
     * @param id 灌溉记录主键
     * @return 结果
     */
    @Override
    public int deleteUserIrrigationRecordById(Long id)
    {
        return userIrrigationRecordMapper.deleteUserIrrigationRecordById(id);
    }
}
