package com.xjrsoft.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.system.dto.AddXjrTipsMessageDto;
import com.xjrsoft.module.system.dto.UpdateXjrTipsMessageDto;
import com.xjrsoft.module.system.dto.XjrTipsMessagePageDto;
import com.xjrsoft.module.system.entity.XjrTipsMessage;
import com.xjrsoft.module.system.service.IXjrTipsMessageService;
import com.xjrsoft.module.system.vo.XjrTipsMessagePageVo;
import com.xjrsoft.module.system.vo.XjrTipsMessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 系统提示信息设置
* @Author dzx
* @Date: 2024-08-12
* @Version 1.0
*/
@RestController
@RequestMapping("/system" + "/xjrTipsMessage")
@Api(value = "/system"  + "/xjrTipsMessage",tags = "系统提示信息设置代码")
@AllArgsConstructor
public class XjrTipsMessageController {


    private final IXjrTipsMessageService xjrTipsMessageService;

    @GetMapping(value = "/page")
    @ApiOperation(value="系统提示信息设置列表(分页)")
    @SaCheckPermission("xjrtipsmessage:detail")
    public RT<PageOutput<XjrTipsMessagePageVo>> page(@Valid XjrTipsMessagePageDto dto){

        LambdaQueryWrapper<XjrTipsMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(XjrTipsMessage::getId)
                .eq(ObjectUtil.isNotNull(dto), XjrTipsMessage::getGroupName, dto.getGroupName())
                .select(XjrTipsMessage.class,x -> VoToColumnUtil.fieldsToColumns(XjrTipsMessagePageVo.class).contains(x.getProperty()));
        IPage<XjrTipsMessage> page = xjrTipsMessageService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<XjrTipsMessagePageVo> pageOutput = ConventPage.getPageOutput(page, XjrTipsMessagePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/list")
    @ApiOperation(value="系统提示信息设置列表")
    @SaCheckPermission("xjrtipsmessage:detail")
    public RT<List<XjrTipsMessagePageVo>> list(@Valid XjrTipsMessagePageDto dto){

        LambdaQueryWrapper<XjrTipsMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ObjectUtil.isNotNull(dto), XjrTipsMessage::getGroupName, dto.getGroupName())
                .orderByDesc(XjrTipsMessage::getId)
                .select(XjrTipsMessage.class,x -> VoToColumnUtil.fieldsToColumns(XjrTipsMessagePageVo.class).contains(x.getProperty()));
        List<XjrTipsMessage> list = xjrTipsMessageService.list(queryWrapper);
        return RT.ok(BeanUtil.copyToList(list, XjrTipsMessagePageVo.class));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询系统提示信息设置信息")
    @SaCheckPermission("xjrtipsmessage:detail")
    public RT<XjrTipsMessageVo> info(@RequestParam Long id){
        XjrTipsMessage xjrTipsMessage = xjrTipsMessageService.getById(id);
        if (xjrTipsMessage == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(xjrTipsMessage, XjrTipsMessageVo.class));
    }

    @GetMapping(value = "/infoByCode")
    @ApiOperation(value="根据code查询系统提示信息设置信息")
    @SaCheckPermission("xjrtipsmessage:detail")
    public RT<XjrTipsMessageVo> infoByCode(@RequestParam String code){
        XjrTipsMessage xjrTipsMessage = xjrTipsMessageService.getOne(
                new QueryWrapper<XjrTipsMessage>().lambda().eq(XjrTipsMessage::getCode, code)
        );
        if (xjrTipsMessage == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(xjrTipsMessage, XjrTipsMessageVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增系统提示信息设置")
    @SaCheckPermission("xjrtipsmessage:add")
    public RT<Boolean> add(@Valid @RequestBody AddXjrTipsMessageDto dto){
        long count = xjrTipsMessageService.count(
                new QueryWrapper<XjrTipsMessage>().lambda()
                        .eq(XjrTipsMessage::getCode, dto.getCode())
        );
        if(count > 0){
            return RT.error("code已存在！请保证code唯一！");
        }
        XjrTipsMessage xjrTipsMessage = BeanUtil.toBean(dto, XjrTipsMessage.class);
        boolean isSuccess = xjrTipsMessageService.save(xjrTipsMessage);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改系统提示信息设置")
    @SaCheckPermission("xjrtipsmessage:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateXjrTipsMessageDto dto){
        long count = xjrTipsMessageService.count(
                new QueryWrapper<XjrTipsMessage>().lambda()
                        .eq(XjrTipsMessage::getCode, dto.getCode())
                        .ne(XjrTipsMessage::getId, dto.getId())
        );
        if(count > 0){
            return RT.error("code已存在！请保证code唯一！");
        }
        XjrTipsMessage xjrTipsMessage = BeanUtil.toBean(dto, XjrTipsMessage.class);
        return RT.ok(xjrTipsMessageService.updateById(xjrTipsMessage));

    }

    @DeleteMapping
    @ApiOperation(value = "删除系统提示信息设置")
    @SaCheckPermission("xjrtipsmessage:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(xjrTipsMessageService.removeBatchByIds(ids));

    }

}