package org.springblade.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.FormulaBean;
import org.springblade.manager.entity.Formula;
import org.springblade.manager.service.impl.FormulaServiceImpl;
import org.springblade.manager.wrapper.FormulaWrapper;
import org.springblade.system.user.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

/**
 * @author yangyj
 * @Date 2022/6/9 14:27
 * @description TODO
 */
@RestController
@AllArgsConstructor
@RequestMapping("/formula")
@Api(value = "公式脚本", tags = "公式脚本")
public class FormulaController {
    private final FormulaServiceImpl service;
    /**
     * 新增或修改
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增或修改", notes = "传入")
    public R save(@RequestBody Formula f) {
        return R.status(service.save(f));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入脚本")
    public R update( @RequestBody FormulaBean f) {
        if(f.getId()!=null){
            Formula old = this.service.getById(f.getId());
            if(old!=null){
                BeanUtils.copyProperties(f,old);
                return R.status(service.updateById(old));
            }
        }
        return R.status(false);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除", notes = "传入id集合")
    public R remove(@RequestParam String ids) {
        return R.status(this.service.deleteLogic(Func.toLongList(ids)));
    }
    /**
     * 查询单条
     */
    @ApiOperationSupport(order =7)
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    public R<FormulaBean> detail(Formula f) {
        Formula detail = service.getOne(Condition.getQueryWrapper(f));
        if(detail!=null){
           return R.data(BeanUtil.copy(detail,FormulaBean.class));
        }
        return R.fail("无数据");
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "公式翻页查询", notes = "公式翻页查询")
    @GetMapping("/page")
    public IPage<FormulaBean> userSearch(@ApiIgnore FormulaBean param, @ApiIgnore Query query) {
        LambdaQueryWrapper<Formula> queryWrapper = Wrappers.<Formula>query().lambda();
        if(Func.isNotEmpty(param.getWbsId())){
            queryWrapper.eq(Formula::getWbsId,param.getWbsId());
        }
        if(Func.isNotEmpty(param.getFormula())){
            queryWrapper.like(Formula::getFormula,param.getFormula());
        }
        if(Func.isNotEmpty(param.getRemark())){
            queryWrapper.like(Formula::getRemark,param.getRemark());
        }
        IPage<Formula> pages  = this.service.page(Condition.getPage(query),queryWrapper);
         return FormulaWrapper.build().pageVO(pages);
    }




}
