package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.SysSystemValueDto;
import com.xy.entity.SysSystemValue;
import com.xy.mapper.SysSystemValueMapper;
import com.xy.redis.RedisCache;
import com.xy.service.SysSystemValueService;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;


/**
 * <p>
 * 系统属性表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2022-12-09
 */
@Api(tags = "系统属性接口")
@Service
public class SysSystemValueServiceImpl extends ServiceImpl<SysSystemValueMapper, SysSystemValue> implements SysSystemValueService {

    @PostMapping("save")
    @ApiOperation("添加")
    public R<Boolean> save(@RequestBody @Validated SysSystemValueDto.Save save) {
        long count = count(new LambdaQueryWrapper<SysSystemValue>()
                .eq(SysSystemValue::getCode, save.getCode())
                .eq(SysSystemValue::getSysId, save.getSysId())
        );
        if (count > 0) {
            return R.fail("标识已存在");
        }
        SysSystemValue sysSystemValue = copy(SysSystemValue.class, save)
                .create(RedisCache.getLoginId(Long.class));
        save(sysSystemValue);
        return R.ok();
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R<PageBean<SysSystemValueDto.Vo>> page(@RequestBody @Validated SysSystemValueDto.Select select) {
        LambdaQueryWrapper<SysSystemValue> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(select, SysSystemValue.class)
                .ge(SysSystemValue::getCreateTime, select.getBeginCreateTime())
                .le(SysSystemValue::getCreateTime, select.getEndCreateTime())
                .build();
        IPage<SysSystemValue> page = page(toIPage(select.getPage()), lambdaQueryWrapper);
        return R.ok(toPageBean(SysSystemValueDto.Vo.class, page));
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated SysSystemValueDto.Update update) {
        SysSystemValue sysSystemValue = copy(SysSystemValue.class, update).update(RedisCache.getLoginId(Long.class));
        updateById(sysSystemValue);
        return R.ok();
    }

    @PostMapping("del")
    @ApiOperation("删除")
    public R del(@RequestBody @Validated SysSystemValueDto.Del del) {
        removeByIds(del.getId());
        return R.ok();
    }
}
