package com.fastbee.waterele.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.util.NumberUtils;
import com.fastbee.common.core.page.PageDomain;
import com.fastbee.common.core.page.TableDataInfo;
import com.fastbee.common.core.page.TableSupport;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.waterele.mapper.MaGuangaiRecordMapper;
import com.fastbee.waterele.domain.MaGuangaiRecord;
import com.fastbee.waterele.service.IMaGuangaiRecordService;

import static com.fastbee.common.utils.PageUtils.startPage;

/**
 * 灌溉记录Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-08-12
 */
@Service
public class MaGuangaiRecordServiceImpl implements IMaGuangaiRecordService 
{
    @Autowired
    private MaGuangaiRecordMapper maGuangaiRecordMapper;

    /**
     * 查询灌溉记录
     * 
     * @param id 灌溉记录主键
     * @return 灌溉记录
     */
    @Override
    public MaGuangaiRecord selectMaGuangaiRecordById(Long id)
    {
        return maGuangaiRecordMapper.selectMaGuangaiRecordById(id);
    }

    /**
     * 查询灌溉记录列表
     * 
     * @param maGuangaiRecord 灌溉记录
     * @return 灌溉记录
     */
    @Override
    public TableDataInfo selectMaGuangaiRecordList(MaGuangaiRecord maGuangaiRecord)
    {
        //
        Long endTime = 0L;
        Long startTime = 0L;
        if(null == maGuangaiRecord.getParams()){
            endTime = new Date().getTime()/1000;
            startTime = endTime - 86400*3;
        }else{
            if(!maGuangaiRecord.getParams().containsKey("endCreateTime")){
                endTime = new Date().getTime()/1000;
            }else{
                endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,
                        maGuangaiRecord.getParams().get("endCreateTime").toString()).getTime();
            }
            if(!maGuangaiRecord.getParams().containsKey("beginCreateTime")){
                startTime = endTime - 86400*3;
            }else{
                startTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,
                        maGuangaiRecord.getParams().get("beginCreateTime").toString()).getTime();
            }
        }
        Date startDate = new Date();
        startDate.setTime(startTime*1000);
        String startDateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,startDate);
        Date endDate = new Date();
        endDate.setTime(endTime*1000);
        String endDateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,endDate);
        String url = "https://zhwly.sdhzwl.cn/api/java/xinjiangup/getGuangaiRecord?devSn="
                +maGuangaiRecord.getDevSn()+"&startTime="+startDateStr+"&endTime="+endDateStr;
        String result =HttpUtil.get(url);

        JSONObject jsonObject = new JSONObject(result);
        List<MaGuangaiRecord> list = new ArrayList<>();
        if(jsonObject != null){
            if(jsonObject.containsKey("code") && Integer.parseInt(jsonObject.get("code").toString()) == 200){
                if(jsonObject.containsKey("data") && jsonObject.get("data") != null){
                    JSONArray dataList = (JSONArray)jsonObject.get("data");
                    for (Object object : dataList) {
                        JSONObject item = (JSONObject)object;
                        MaGuangaiRecord record = new MaGuangaiRecord();
                        record.setDevSn(item.get("dev_sn").toString());
                        if(StringUtils.isNotEmpty(item.get("start_time").toString())){
                            if(NumberUtil.isNumber(item.get("start_time").toString())) {
                                Date date = new Date(Long.parseLong(item.get("start_time").toString()) * 1000);
                                record.setStartTime(date);
                            }
                        }
                        if(StringUtils.isNotEmpty(item.get("end_time").toString())){
                            if(NumberUtil.isNumber(item.get("end_time").toString())){
                                Date date = new Date(Long.parseLong(item.get("end_time").toString())*1000);
                                record.setEndTime(date);
                            }
                        }
                        if(StringUtils.isNotEmpty(item.get("last_time").toString())){
                            if(NumberUtil.isNumber(item.get("last_time").toString())) {
                                Date date = new Date(Long.parseLong(item.get("last_time").toString()) * 1000);
                                record.setLastTime(date);
                            }
                        }
                        record.setCurEle(item.get("cur_ele").toString());
                        record.setCardId(item.get("card_id").toString());
                        record.setAreaCode(item.get("area_code").toString());
                        record.setUserBalance(com.fastbee.common.utils.NumberUtils.formatFloat(Float.parseFloat(item.get("user_balance").toString())/100)+"");
                        record.setCurFlow(item.get("cur_flow").toString().equals("null")?"--":
                                (com.fastbee.common.utils.NumberUtils.formatFloat(Float.parseFloat(item.get("cur_flow").toString())/100)+""));
                        record.setCurEle(item.get("cur_ele").toString().equals("null")?"--":
                                (com.fastbee.common.utils.NumberUtils.formatFloat(Float.parseFloat(item.get("cur_ele").toString())/100)+""));
                        record.setStatus(Integer.parseInt(item.get("status").toString()));
                        if(StringUtils.isNotEmpty(item.get("create_time").toString())){
                            if(NumberUtil.isNumber(item.get("create_time").toString())) {
                                Date date = new Date(Long.parseLong(item.get("create_time").toString()) * 1000);
                                record.setCreateTime(date);
                            }
                        }
                        list.add(record);
                    }
                }
            }
        }
        //
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<MaGuangaiRecord> subList = new ArrayList<>();
        if(list.size() > (pageNum-1)*pageSize){
            if(list.size() < pageNum*pageSize){
                subList = list.subList((pageNum-1)*pageSize, list.size());
            }else{
                subList = list.subList((pageNum-1)*pageSize, pageNum*pageSize);
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("获取成功");
        tableDataInfo.setTotal(list.size());
        tableDataInfo.setRows(subList);
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }




    /**
     * 新增灌溉记录
     * 
     * @param maGuangaiRecord 灌溉记录
     * @return 结果
     */
    @Override
    public int insertMaGuangaiRecord(MaGuangaiRecord maGuangaiRecord)
    {
        maGuangaiRecord.setCreateTime(DateUtils.getNowDate());
        return maGuangaiRecordMapper.insertMaGuangaiRecord(maGuangaiRecord);
    }

    /**
     * 修改灌溉记录
     * 
     * @param maGuangaiRecord 灌溉记录
     * @return 结果
     */
    @Override
    public int updateMaGuangaiRecord(MaGuangaiRecord maGuangaiRecord)
    {
        maGuangaiRecord.setUpdateTime(DateUtils.getNowDate());
        return maGuangaiRecordMapper.updateMaGuangaiRecord(maGuangaiRecord);
    }

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

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