package com.fjhx.controller.common;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.entity.common.CommonFile;
import com.fjhx.entity.example.ExampleInfo;
import com.fjhx.params.ExampleResult;
import com.fjhx.service.common.CommonFileService;
import com.fjhx.utils.ExampleAbstract;
import com.fjhx.utils.FlowConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通用 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/commonFile")
public class CommonFileController {

    @Autowired
    private CommonFileService commonFileService;

    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, String> result = commonFileService.uploadFile(file);
        return R.success(result);
    }

    @PostMapping("list")
    public R list(@RequestBody CommonFile commonFile) {
        List<CommonFile> list = commonFileService.list(Wrappers.<CommonFile>lambdaQuery()
                .eq(CommonFile::getBusinessId, commonFile.getBusinessId()));
        return R.success(list);
    }


    private FlowConstructor flowConstructor() {
        return FlowConstructor.init(
                new ExampleAbstract() {
                    @Override
                    public String getCode() {
                        return "test";
                    }

                    @Override
                    public void end() {
                        ExampleInfo cacheData = getCacheData(ExampleInfo.class);
                        System.err.println("缓存     " + cacheData);
                        System.err.println("流程结束了");
                    }

                    @Override
                    public void reject() {
                        System.err.println("审批驳回方法(直接结束流程)");
                    }

                    @Override
                    public String circulation() {
                        return super.circulation();
                    }

                    @Override
                    public void fail() {
                        System.err.println("审批不通过方法(返回上一节点)");
                    }
                }
        );
    }

    @GetMapping("create")
    public R test() {
        HashMap<String, String> map = new HashMap<>();
        map.put("processInfoId", "1111111111111111111111");
        map.put("processTenantId", "222222222222222222222");
        ExampleResult result = flowConstructor().create(123456L, "测试流程", "测试开始流程", map);
        return R.success(result);
    }

    @GetMapping("jump")
    public R jump(@RequestParam("buttonId") Long buttonId) {
        flowConstructor().jump(123456L, buttonId, "测试流程运行");
        return R.success();
    }

    @GetMapping("/getPassword")
    public String getPassword(@RequestParam("password") String password) {
        return DigestUtil.hex(MD5.create().digestHex(password));
    }

}

