package com.zzsmart.qomo.kn.cost.manage.controller;

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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzsmart.qomo.dao.entity.FlowDefinition;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostModel;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostModelService;
import com.zzsmart.qomo.service.IDataFlowDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: app_scene_cost_model
 * @Author: jeecg-boot
 * @Date: 2024-07-29
 * @Version: V1.0
 */
@Api(tags = "cost-标准成本-模型管理")
@RestController
@RequestMapping("//cost-model")
@Slf4j
public class AppSceneCostModelController  {
    @Autowired
    private IAppSceneCostModelService appSceneCostModelService;
    @Autowired
    private IDataFlowDefinitionService flowDefinitionService;

    /**
     * 标准成本模型分页列表查询
     *
     * @param flowDefinition
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_scene_cost_model-分页列表查询")
    @ApiOperation(value = "标准成本模型分页列表查询", notes = "标准成本模型分页列表查询")
    @GetMapping(value = "/pageList")
    public Result<IPage<FlowDefinition>> queryPageList(FlowDefinition flowDefinition, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<FlowDefinition> queryWrapper = QueryGenerator.initQueryWrapper(flowDefinition, req.getParameterMap());
        Page<FlowDefinition> page = new Page((long) pageNo, (long) pageSize);
        LambdaQueryWrapper<FlowDefinition> wrapper = queryWrapper.lambda();
        wrapper.orderByDesc(FlowDefinition::getCreateTime);
        IPage<FlowDefinition> pageList = flowDefinitionService.queryPageList(page, wrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "标准成本模型列表", notes = "标准成本模型列表")
    @GetMapping(value = "/list")
    public Result<List<FlowDefinition>> queryList(FlowDefinition flowDefinition, HttpServletRequest req) {
        QueryWrapper<FlowDefinition> queryWrapper = QueryGenerator.initQueryWrapper(flowDefinition, req.getParameterMap());
        LambdaQueryWrapper<FlowDefinition> wrapper = queryWrapper.lambda();
        wrapper.orderByDesc(FlowDefinition::getCreateTime);
        List<FlowDefinition> list = appSceneCostModelService.queryListResult(wrapper);
        return Result.OK(list);
    }

    /**
     * 添加标准成本模型
     *
     * @param flowDefinition
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-添加")
    @ApiOperation(value = "app_scene_cost_model-添加", notes = "app_scene_cost_model-添加")
    @PostMapping(value = "/add")
    public Result<FlowDefinition> add(@RequestBody FlowDefinition flowDefinition) {
        FlowDefinition definition = flowDefinitionService.add(flowDefinition);
        return Result.OK(definition);
    }

    /**
     * 编辑
     *
     * @param flowDefinition
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-编辑")
    @ApiOperation(value = "app_scene_cost_model-编辑", notes = "app_scene_cost_model-编辑")
    @RequiresPermissions("com.zzsmart.qomo.kn.cost.manage:app_scene_cost_model:edit")
    @PostMapping(value = "/edit")
    public Result<FlowDefinition> edit(@RequestBody FlowDefinition flowDefinition) {
        return Result.OK(flowDefinitionService.edit(flowDefinition));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-通过id删除")
    @ApiOperation(value = "app_scene_cost_model-通过id删除", notes = "app_scene_cost_model-通过id删除")
    @RequiresPermissions("com.zzsmart.qomo.kn.cost.manage:app_scene_cost_model:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) Integer id) {
        flowDefinitionService.delete(id);
        return Result.OK("删除成功!");
    }

}
