package com.ylx.web.controller.massage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.massage.domain.TLbt;
import com.ylx.massage.service.TLbtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图 前端控制器
 */
@RestController
@RequestMapping("/api/lbt/v1")
@Slf4j
@Api(tags = {"轮播图管理"})
public class TLbtController {

    @Resource
    private TLbtService lbtService;

    /**
     * 获取轮播图
     *
     * @return
     */
    @ApiOperation("获取轮播图")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public R<List<TLbt>> getAll() {
        QueryWrapper<TLbt> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(TLbt::getcSort);
        List<TLbt> list = lbtService.list(wrapper);
        return R.ok(list);
    }

    /**
     * 添加或者更新轮播图
     *
     * @param lbt
     * @return
     */
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ApiOperation("添加或者更新轮播图")
    public R addOrUpdate(@RequestBody TLbt lbt) {
        try {
            return R.ok(lbtService.addOrUpdate(lbt));
        } catch (ServiceException s) {
            log.error(s.toString());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error(e.toString());
            return R.fail("系统异常");
        }
    }


    /**
     * 分页查询数据
     */
    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public R<Page<TLbt>> selectSpfl(Page<TLbt> page, TLbt lbt) {

        LambdaQueryWrapper<TLbt> tLbtLambdaQueryWrapper = new LambdaQueryWrapper<>();

        tLbtLambdaQueryWrapper.like(StringUtils.isNotBlank(lbt.getcDescribe()), TLbt::getcDescribe, lbt.getcDescribe())
                .orderByAsc(TLbt::getcSort);
        // 获取查询返回结果
        Page<TLbt> pageSelect = lbtService.page(page, tLbtLambdaQueryWrapper);
        return R.ok(pageSelect);
    }

    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation("删除轮播图")
    public R del(@RequestBody TLbt tLbt) {
        try {
            return R.ok(lbtService.del(tLbt));
        } catch (ServiceException s) {
            log.error(s.toString());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error(e.toString());
            return R.fail("系统异常");
        }
    }

    @ApiOperation("根据id查询轮播图")
    @RequestMapping(value = "/wx/getByid", method = RequestMethod.POST)
    public R getById(@RequestBody TLbt tLbt) {
        return R.ok(lbtService.getById(tLbt.getcId()));
    }
}
