package com.xy.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.device.EnumDeviceTempConfig;
import com.xy.dto.DeviceConfigDto;
import com.xy.entity.DeviceConfig;
import com.xy.entity.SysDictRedis;
import com.xy.mapper.DeviceConfigMapper;
import com.xy.utils.Beans;
import com.xy.utils.R;
import com.xy.utils.SysDictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 设备配置表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-06-16
 */
@Service
@AllArgsConstructor
@Api(tags = "设备配置表")
public class DeviceConfigServiceImpl extends ServiceImpl<DeviceConfigMapper, DeviceConfig> implements DeviceConfigService {

    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated DeviceConfigDto.Update update) {
        DeviceConfig deviceConfigInfo = getById(update.getDeviceId());
        LocalDateTime now = LocalDateTime.now();
        DeviceConfig deviceConfig = Beans.copy(DeviceConfig.class, update);
        if (deviceConfigInfo == null) {
            deviceConfig.setCreateTime(now);
        }
        deviceConfig.setUpdateTime(LocalDateTime.now());
        saveOrUpdate(deviceConfig);
        return R.ok();
    }

    @PostMapping("batchUpdate")
    @ApiOperation("批量更新")
    public R batchUpdate(@RequestBody @Validated DeviceConfigDto.BatchUpdate batchUpdate) {
        List<Long> deviceIds = batchUpdate.getDeviceIds();
        List<DeviceConfig> deviceConfigs = new ArrayList<>();
        if (CollUtil.isNotEmpty(deviceIds)) {
            deviceIds.forEach(d -> {
                DeviceConfig deviceConfig = new DeviceConfig();
                deviceConfig.setDeviceId(d);
                deviceConfig.setTempMin(batchUpdate.getTempMin());
                deviceConfig.setTempMax(batchUpdate.getTempMax());
                deviceConfig.setStockOutGoodsNum(batchUpdate.getStockOutGoodsNum());
                deviceConfig.setStockOutRate(batchUpdate.getStockOutRate());
                deviceConfig.setUpdateTime(LocalDateTime.now());
                deviceConfigs.add(deviceConfig);
            });
            this.saveOrUpdateBatch(deviceConfigs);
        }
        return R.ok();
    }

    @Override
    @ApiOperation("对象查询")
    public R<DeviceConfigDto.Vo> obj(@RequestBody @Validated DeviceConfigDto.Obj obj) {
        DeviceConfig deviceConfig = getById(obj.getDeviceId());
        if (deviceConfig == null) {
            Map<String, SysDictRedis> stringSysDictRedisMap = SysDictUtils.get(EnumDeviceTempConfig.Code.CODE.getCode());
            deviceConfig = new DeviceConfig()
                    .setDeviceId(obj.getDeviceId())
                    .setTempMax(Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MAX.getCode()).getValue()))
                    .setTempMin(Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MIN.getCode()).getValue()));
        }
        return R.ok(Beans.copy(DeviceConfigDto.Vo.class, deviceConfig));
    }
}
