package com.mcwl.web.controller.resource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.mcwl.common.core.controller.BaseController;
import com.mcwl.common.core.domain.R;
import com.mcwl.resource.domain.WorkFlow;
import com.mcwl.resource.domain.dto.AddRequestWorkFlow;
import com.mcwl.resource.domain.request.RequestWorkFlow;
import com.mcwl.resource.domain.response.ResponseWorkFlow;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.WorkFlowVersionMapper;
import com.mcwl.resource.service.WorkFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * 工作流
 *
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.web.controller.resource
 * @Filename：MallProductController
 * @Description 商品
 * @Date：2024/12/31 10:48
 */
@Api(tags = "工作流")
@RestController
@RequestMapping("/WorkFlow")
public class WorkFlowController extends BaseController {


    @Autowired
    private WorkFlowService workFlowService;

    @Autowired
    private WorkFlowVersionMapper workFlowVersionMapper;


    /**
     * 设置工作流的置顶状态
     *
     * @param id
     * @param isTop
     * @return
     */
    @ApiOperation(value = "设置工作流的置顶状态")
    @GetMapping("/{id}/top")
    public R<Object> setWorkFlowTop(@PathVariable Long id, @RequestParam boolean isTop) {

        workFlowService.setworkFlowTop(id, isTop);
        return R.ok();

    }


    /**
     * 获取所有工作流，按照置顶状态排序
     *
     * @return
     */
    @ApiOperation(value = "获取置顶的模型列表")
    @GetMapping
    public R<List<WorkFlow>> fetchWorkFlowSortedByTopStatus() {
        List<WorkFlow> models = workFlowService.fetchWorkFlowSortedByTopStatus();
        return R.ok(models);
    }


    /**
     * 添加工作流
     *
     * @param addRequestWorkFlow
     * @return
     */
    @ApiOperation(value = "添加工作流")
    @PostMapping("/addWorkFlow")
    public R<Object> addWorkFlow(@Validated @RequestBody AddRequestWorkFlow addRequestWorkFlow) {

        return workFlowService.addWorkFlow(addRequestWorkFlow);
    }

    /**
     * 修改工作流
     *
     * @param requestWorkFlow
     * @return
     */
    @ApiOperation(value = "修改工作流")
    @PostMapping("/updateWorkFlow")
    public R<Object> updateWorkFlow(@RequestBody RequestWorkFlow requestWorkFlow) {


//        for (WorkFlowVersion workFlowVersion : requestWorkFlow.getWorkFlowVersionList()) {
//
//            //校验名字
//            WorkFlowVersion workFlowVersion1 = workFlowVersionMapper.selectByFileName(workFlowVersion.getFileName());
//            if (workFlowVersion1 != null) {
//
//                return R.fail(HttpStatus.SHOW_ERROR_MSG,"文件名字重复");
//            }
//            //校验hash
//            WorkFlowVersion workFlowVersion2 = workFlowVersionMapper.selectByHash(workFlowVersion.getFileHash());
//            if (workFlowVersion2 != null){
//
//                return R.fail(HttpStatus.SHOW_ERROR_MSG,"文件内容重复");
//            }
//        }

        workFlowService.updateWorkFlow(requestWorkFlow);

        return R.ok();
    }

    /**
     * 删除工作流
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除工作流")
    @GetMapping("/deleteWorkFlow")
    public R<Object> deleteWorkFlow(@RequestParam @Valid @NotNull(message = "工作流id不能为空") Long id) {

        workFlowService.deleteWorkFlow(id);
        return R.ok();
    }

    /**
     * 查询工作流列表
     *
     * @param pageVo
     * @return
     */
    @ApiOperation(value = "查询工作流列表")
    @PostMapping("/selectWorkFlow")
    public R<Page<WorkFlow>> selectWorkFlow(@RequestBody PageVo pageVo) {

        return workFlowService.selectWorkFlow(pageVo);
    }


    /**
     * 查询工作流详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询工作流详情")
    @GetMapping("/selectWorkFlowById")
    public R<WorkFlow> selectWorkFlowById(@RequestParam @Valid @NotNull(message = "工作流id不能为空") Long id) {

        return workFlowService.selectWorkFlowById(id);
    }

    /**
     * 校验名字是否重复
     *
     * @param name
     * @return
     */
    @ApiOperation(value = "校验名字是否重复")
    @GetMapping("/selectWorkFlowByName")
    public R<Long> selectWorkFlowByName(@RequestParam String name) {

        Long sign = workFlowService.selectWorkFlowByName(name);
        return R.ok(sign);
    }


    /**
     * 回显接口
     *
     * @return
     */
    @ApiOperation(value = "工作流回显接口")
    @GetMapping("/selectWorkFlowVersionById")
    public R<RequestWorkFlow> selectWorkFlowVersionById(@RequestParam @Valid @NotNull(message = "图片id不能为空") Long id) {

        return workFlowService.selectWorkFlowVersionById(id);
    }

    /**
     * 工作流列表
     *
     * @return
     */
    @ApiOperation(value = "工作流列表")
    @PostMapping("/workFlowList")
    public R<PageInfo<ResponseWorkFlow>> workFlowList(@RequestBody PageVo pageVo) {

        PageInfo<ResponseWorkFlow> responseWorkFlowPageInfo = workFlowService.workFlowList(pageVo);
        return R.ok(responseWorkFlowPageInfo);
    }




}
