package com.xy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.annotation.LogOperate;
import com.xy.dto.DeviceTypeAlgorithmBeforConfigDto;
import com.xy.entity.DeviceTypeAlgorithmBeforConfig;
import com.xy.mapper.DeviceTypeAlgorithmBeforConfigMapper;
import com.xy.utils.Emptys;
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;

import static com.xy.utils.Beans.copy;


/**
 * <p>
 * 设备类型算法预充配置 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-07-10
 */
@Service
@AllArgsConstructor
@Api(tags = "设备类型算法预充配置")
public class DeviceTypeAlgorithmBeforConfigServiceImpl extends ServiceImpl<DeviceTypeAlgorithmBeforConfigMapper, DeviceTypeAlgorithmBeforConfig> implements DeviceTypeAlgorithmBeforConfigService {

    @Override
    @ApiOperation("对象查询")
    public R<DeviceTypeAlgorithmBeforConfigDto.Vo> obj(@RequestBody DeviceTypeAlgorithmBeforConfigDto.Vo vo) {
        DeviceTypeAlgorithmBeforConfigDto.SelectList selectList = copy(DeviceTypeAlgorithmBeforConfigDto.SelectList.class, vo);
        List<DeviceTypeAlgorithmBeforConfigDto.Vo> list = list(selectList).getData();
        if (Emptys.check(list)) {
            return R.ok(list.get(0));
        }
        return R.ok();
    }

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<DeviceTypeAlgorithmBeforConfigDto.Vo>> list(@RequestBody DeviceTypeAlgorithmBeforConfigDto.SelectList selectList) {
        LambdaQueryWrapper<DeviceTypeAlgorithmBeforConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceTypeAlgorithmBeforConfig.class).build();
        List<DeviceTypeAlgorithmBeforConfig> list = list(lambdaQueryWrapper);
        return R.ok(copy(DeviceTypeAlgorithmBeforConfigDto.Vo.class, list));
    }

    @LogOperate
    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated DeviceTypeAlgorithmBeforConfigDto.Save save) {
        LocalDateTime now = LocalDateTime.now();
        DeviceTypeAlgorithmBeforConfig saveInfo = copy(DeviceTypeAlgorithmBeforConfig.class, save)
                .setCreateTime(now)
                .setUpdateTime(now);
        save(saveInfo);
        return R.ok().setLogMsg(String.format("商户id：%s,算法id：%s,预充金额：%s", save.getMercId(), save.getAlgorithmId(), save.getMoney()));
    }

    @LogOperate
    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated DeviceTypeAlgorithmBeforConfigDto.Update update) {
        DeviceTypeAlgorithmBeforConfig deviceTypeAlgorithmBeforConfig = getById(update.getId());
        DeviceTypeAlgorithmBeforConfig updateInfo = copy(DeviceTypeAlgorithmBeforConfig.class, update)
                .setUpdateTime(LocalDateTime.now());
        updateById(updateInfo);
        return R.ok().setLogMsg(String.format("商户id：%s,算法id：%s,原预充金额：%s,新预充金额：%s", deviceTypeAlgorithmBeforConfig.getMercId(), deviceTypeAlgorithmBeforConfig.getAlgorithmId(), deviceTypeAlgorithmBeforConfig.getMoney(), update.getMoney()));
    }

    @LogOperate
    @PostMapping("delete")
    @ApiOperation("删除")
    public R delete(@RequestBody @Validated DeviceTypeAlgorithmBeforConfigDto.Delete delete) {
        removeById(delete.getId());
        return R.ok();
    }
}