package com.xy.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yitter.idgen.YitIdHelper;
import com.xy.dto.DeviceChargingConfigDto;
import com.xy.dto.be.MercDto;
import com.xy.entity.DeviceChargingConfig;
import com.xy.mapper.DeviceChargingConfigMapper;
import com.xy.service.be.MercService;
import com.xy.utils.Beans;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.R;
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.List;
/**
*
* 设备计费配置表 服务实现类
*
*
* @author lijin
* @since 2023-07-28
*/
@Service
@AllArgsConstructor
@Api(tags = "设备计费配置表")
public class DeviceChargingConfigServiceImpl extends ServiceImpl implements DeviceChargingConfigService {
private MercService mercService;
@PostMapping("list")
@ApiOperation("集合查询")
public R> list(@RequestBody @Validated DeviceChargingConfigDto.SelectList selectList) {
MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(selectList.getMercId())).getData();
LambdaQueryWrapper lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceChargingConfig.class)
.eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
.build();
List list = list(lambdaQueryWrapper);
return R.ok(Beans.copy(DeviceChargingConfigDto.Vo.class, list));
}
@PostMapping("saveOrUpdate")
@ApiOperation("添加或修改")
public R saveOrUpdate(@RequestBody @Validated DeviceChargingConfigDto.SaveOrUpdate saveOrUpdate) {
LocalDateTime now = LocalDateTime.now();
MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(saveOrUpdate.getMercId())).getData();
List values = saveOrUpdate.getConfigValues();
for (DeviceChargingConfigDto.SaveOrUpdate.ConfigValue value : values) {
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper()
.eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
.eq(DeviceChargingConfig::getDeviceType, saveOrUpdate.getDeviceType())
.eq(DeviceChargingConfig::getType, value.getType());
DeviceChargingConfig deviceChargingConfig = getOne(lambdaQueryWrapper);
if (deviceChargingConfig == null) {
if (value.getConfigValue() == -1) {
continue;
}
deviceChargingConfig = new DeviceChargingConfig()
.setId(YitIdHelper.nextId())
.setMercCode(merc.getMercCode())
.setDeviceType(saveOrUpdate.getDeviceType())
.setConfigValue(value.getConfigValue())
.setNote(saveOrUpdate.getNote())
.setType(value.getType())
.setCreateTime(now)
.setUpdateTime(now);
save(deviceChargingConfig);
} else {
if (value.getConfigValue() == -1) {
removeById(deviceChargingConfig.getId());
continue;
}
deviceChargingConfig.setConfigValue(value.getConfigValue())
.setNote(saveOrUpdate.getNote())
.setUpdateTime(now);
updateById(deviceChargingConfig);
}
}
return R.ok();
}
}