package com.ylx.banner.controller;

import com.alibaba.fastjson.JSON;
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.banner.domain.Banner;
import com.ylx.massage.domain.dto.TLbtStatusDTO;
import com.ylx.banner.service.BannerService;
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 BannerController {

    @Resource
    private BannerService lbtService;

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

    /**
     * 添加或者更新轮播图
     *
     * @param lbt
     * @return R
     */
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ApiOperation("添加或者更新轮播图")
    public R addOrUpdate(@RequestBody Banner lbt) {
        try {
            log.info("请求参数:{}", JSON.toJSONString(lbt));
            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("系统异常");
        }
    }


    /**
     * 分页查询轮播图数据
     * @param page
     * @param lbt
     * @return R<Page<TLbt>>
     */
    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public R<Page<Banner>> selectSpfl(Page<Banner> page, Banner lbt) {
        try {
            LambdaQueryWrapper<Banner> tLbtLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tLbtLambdaQueryWrapper.like(StringUtils.isNotBlank(lbt.getTitle()), Banner::getTitle, lbt.getTitle()).orderByAsc(Banner::getSort);
            //查询条件添加显示状态
            tLbtLambdaQueryWrapper.eq(lbt.getStatus() != null, Banner::getStatus, lbt.getStatus());
            // 获取查询返回结果
            Page<Banner> pageSelect = lbtService.page(page, tLbtLambdaQueryWrapper);
            return R.ok(pageSelect);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

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

    /**
     * 打开或者关闭轮播图显示。
     *
     * @param dto 显示状态修改参数
     * @return R
     */
    @Log(title = "轮播图显示状态", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/status", method = RequestMethod.PUT)
    @ApiOperation("打开或者关闭轮播图显示")
    public R updateStatus(@RequestBody TLbtStatusDTO dto) {
        try {
            return R.ok(lbtService.updateStatus(dto));
        } catch (ServiceException s) {
            log.error(s.toString());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error(e.toString());
            return R.fail("系统异常");
        }
    }

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