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.ArrayList;
import java.util.List;


/**
 * <p>
 * 设备计费配置表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-07-28
 */
@Service
@AllArgsConstructor
@Api(tags = "设备计费配置表")
public class DeviceChargingConfigServiceImpl extends ServiceImpl<DeviceChargingConfigMapper, DeviceChargingConfig> implements DeviceChargingConfigService {

    private MercService mercService;

    private DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<DeviceChargingConfigDto.Vo>> list(@RequestBody @Validated DeviceChargingConfigDto.SelectList selectList) {
        MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(selectList.getMercId())).getData();
        LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceChargingConfig.class)
                .eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
                .build();
        List<DeviceChargingConfig> 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<DeviceChargingConfigDto.SaveOrUpdate.ConfigValue> values = saveOrUpdate.getConfigValues();
        for (DeviceChargingConfigDto.SaveOrUpdate.ConfigValue value : values) {
            LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceChargingConfig>()
                    .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();
    }

    @Override
    @ApiOperation("集合查询2")
    public R<List<DeviceChargingConfigDto.Vo>> list2(DeviceChargingConfigDto.SelectList selectList) {
        MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(selectList.getMercId())).getData();
        List<String> mercCodes = new ArrayList<>();
        deviceAlgorithmChargingService.getMercCodes(merc.getMercCode(), mercCodes);
        LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceChargingConfig.class)
                .in(DeviceChargingConfig::getMercCode, mercCodes)
                .build()
                .orderByDesc(DeviceChargingConfig::getMercCode);
        List<DeviceChargingConfig> list = list(lambdaQueryWrapper);
        return R.ok(Beans.copy(DeviceChargingConfigDto.Vo.class, list));
    }
}