package com.xjrsoft.module.liteflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.liteflow.dto.AddXjrLiteflowChainDto;
import com.xjrsoft.module.liteflow.dto.UpdateXjrLiteflowChainDto;
import com.xjrsoft.module.liteflow.dto.XjrLiteflowChainListDto;
import com.xjrsoft.module.liteflow.dto.XjrLiteflowChainPageDto;
import com.xjrsoft.module.liteflow.entity.XjrLiteflowChain;
import com.xjrsoft.module.liteflow.service.IXjrLiteflowChainService;
import com.xjrsoft.module.liteflow.vo.XjrLiteflowChainListVo;
import com.xjrsoft.module.liteflow.vo.XjrLiteflowChainOptionsVo;
import com.xjrsoft.module.liteflow.vo.XjrLiteflowChainPageVo;
import com.xjrsoft.module.liteflow.vo.XjrLiteflowChainVo;
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.time.LocalDateTime;
import java.util.List;

/**
* @title: 规则表
* @Author fanxp
* @Date: 2023-11-02
* @Version 1.0
*/
@RestController
@RequestMapping("/liteflow" + "/chain")
@Api(value = "/liteflow"  + "/chain",tags = "规则表代码")
@AllArgsConstructor
public class XjrLiteflowChainController {


    private final IXjrLiteflowChainService xjrLiteflowChainService;

    @GetMapping(value = "/list")
    @ApiOperation(value="规则表列表(不分页)")
    @SaCheckPermission("xjrliteflowchain:detail")
    public RT<List<XjrLiteflowChainListVo>> list(@Valid XjrLiteflowChainListDto dto){

        LambdaQueryWrapper<XjrLiteflowChain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(XjrLiteflowChain::getId)
                .select(XjrLiteflowChain.class,x -> VoToColumnUtil.fieldsToColumns(XjrLiteflowChainListVo.class).contains(x.getProperty()));

        List<XjrLiteflowChain> list = xjrLiteflowChainService.list(queryWrapper);
        List<XjrLiteflowChainListVo> listVos = BeanUtil.copyToList(list, XjrLiteflowChainListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/options")
    @ApiOperation(value="规则下拉选项")
    @SaCheckPermission("xjrliteflowchain:detail")
    public RT<List<XjrLiteflowChainOptionsVo>> options(){

        LambdaQueryWrapper<XjrLiteflowChain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(XjrLiteflowChain::getId)
                .select(XjrLiteflowChain.class,x -> VoToColumnUtil.fieldsToColumns(XjrLiteflowChainOptionsVo.class).contains(x.getProperty()));

        List<XjrLiteflowChain> list = xjrLiteflowChainService.list(queryWrapper);
        List<XjrLiteflowChainOptionsVo> listVos = BeanUtil.copyToList(list, XjrLiteflowChainOptionsVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="规则表列表(分页)")
    @SaCheckPermission("xjrliteflowchain:detail")
    public RT<PageOutput<XjrLiteflowChainPageVo>> page(@Valid XjrLiteflowChainPageDto dto){

        LambdaQueryWrapper<XjrLiteflowChain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(XjrLiteflowChain::getId)
                .select(XjrLiteflowChain.class,x -> VoToColumnUtil.fieldsToColumns(XjrLiteflowChainPageVo.class).contains(x.getProperty()));
        IPage<XjrLiteflowChain> page = xjrLiteflowChainService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<XjrLiteflowChainPageVo> pageOutput = ConventPage.getPageOutput(page, XjrLiteflowChainPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询规则表信息")
    @SaCheckPermission("xjrliteflowchain:detail")
    public RT<XjrLiteflowChainVo> info(@RequestParam Long id){
        XjrLiteflowChain xjrLiteflowChain = xjrLiteflowChainService.getById(id);
        if (xjrLiteflowChain == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(xjrLiteflowChain, XjrLiteflowChainVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增规则表")
    @SaCheckPermission("xjrliteflowchain:add")
    public RT<Boolean> add(@Valid @RequestBody AddXjrLiteflowChainDto dto){
        XjrLiteflowChain xjrLiteflowChain = BeanUtil.toBean(dto, XjrLiteflowChain.class);
        xjrLiteflowChain.setCreateTime(LocalDateTime.now());
        boolean isSuccess = xjrLiteflowChainService.save(xjrLiteflowChain);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改规则表")
    @SaCheckPermission("xjrliteflowchain:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateXjrLiteflowChainDto dto){

        XjrLiteflowChain xjrLiteflowChain = BeanUtil.toBean(dto, XjrLiteflowChain.class);
        return RT.ok(xjrLiteflowChainService.updateById(xjrLiteflowChain));

    }

    @DeleteMapping
    @ApiOperation(value = "删除规则表")
    @SaCheckPermission("xjrliteflowchain:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(xjrLiteflowChainService.removeBatchByIds(ids));

    }

}