package com.huimv.receive.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.utils.Print;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BaseDevice;
import com.huimv.receive.mapper.BaseDeviceMapper;
import com.huimv.receive.service.IBaseDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-04
 */
@Service
public class BaseDeviceServiceImpl extends ServiceImpl<BaseDeviceMapper, BaseDevice> implements IBaseDeviceService {

    @Autowired
    private BaseDeviceMapper deviceMapper;
    @Override
    public void printDevice(HttpServletResponse httpServletRequest, Map<String, String> paramsMap) throws Exception {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        List<BaseDevice> list = new ArrayList<>();
        for (String s : split) {
            BaseDevice admission = deviceMapper.selectById(s);
            list.add(admission);
        }
        Print.printDevice(list);
    }

    @Override
    public Result listCount(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BaseDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        queryWrapper.eq("device_status", 0);
        Integer count = deviceMapper.selectCount(queryWrapper);//在线
        QueryWrapper<BaseDevice> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId);
        queryWrapper1.eq("device_status", 1);
        Integer count1 = deviceMapper.selectCount(queryWrapper1);//离线
        Integer allCount = count + count1;
        DecimalFormat def = new DecimalFormat("0.00");
        String rank;
        if (allCount == 0 || allCount == null) {
          Double ranks  = Double.valueOf("0.00");
            rank = ranks.toString();
        } else {
           Double ranks = (Double.valueOf(count.toString()) / Double.valueOf(allCount.toString())) * 100;
           rank = def.format(ranks);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", allCount);
        jsonObject.put("count1", count);
        jsonObject.put("count2", count1);
        jsonObject.put("rank", rank);
        return new Result(ResultCode.SUCCESS, jsonObject);
    }

    @Override
    public Result listSelect(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String word = paramsMap.get("word");
        String locationId = paramsMap.get("locationId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        QueryWrapper<BaseDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        if (StringUtils.isNotBlank(word)) {
            queryWrapper.and(i -> i.like("device_code", word)
                    .or().like( "device_name", word));
        }
        queryWrapper.eq(StringUtils.isNotBlank(locationId),"work_location_type", locationId);
        Page<BaseDevice> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS, deviceMapper.selectPage(page, queryWrapper));
    }
    @Override
    public Result addDevice(BaseDevice baseDevice) {
        String deviceCode = baseDevice.getDeviceCode();
        int deviceCodeCount = this.count(new QueryWrapper<BaseDevice>().eq("device_code", deviceCode));
        if (deviceCodeCount>0){
            return new Result(10001,"设备编码已存在",false);
        }
        this.save(baseDevice);
        return new Result(10000,"添加成功",true);
    }

    @Override
    public Result updateDevice(BaseDevice baseDevice) {
        this.updateById(baseDevice);
        return new Result(10000,"修改成功",true);
    }
}
