package com.keyware.shandan.mark.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.mark.pojo.FormMark;
import com.keyware.shandan.mark.pojo.MarkType;
import com.keyware.shandan.mark.services.MarkFormService;
import com.keyware.shandan.mark.services.MarkTypeService;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author GuoXin
 * @date 2022-05-17
 */
@RestController
@RequestMapping("/mark/api/form")
public class MarkFormController {

    private final MarkFormService markFormService;
    private final MarkTypeService markTypeService;

    public MarkFormController(MarkFormService markFormService, MarkTypeService markTypeService) {
        this.markFormService = markFormService;
        this.markTypeService = markTypeService;
    }

    /**
     * 根据表单类型查询配置数据列表
     *
     * @param page     分页对象
     * @param formType 表单类型
     * @return 返回分页数据
     */
    @GetMapping("/config/list/{formType}")
    public Result<Page<FormMark>> list(Page<FormMark> page, @PathVariable String formType) {
        page.setSize(999);
        QueryWrapper<FormMark> query = new QueryWrapper<>();
        query.eq(!"all".equalsIgnoreCase(formType),"FORM_TYPE", formType);
        Map<String, FormMark> temp = new HashMap<>();
        markFormService.list(query).forEach(formMark -> temp.put(formMark.getMarkType(), formMark));

        page.setRecords(new ArrayList<>(temp.values()));
        return Result.of(page);
    }

    /**
     * 新增表单配置
     *
     * @param formMark 表单配置对象
     * @return 返回新增结果
     */
    @PostMapping("/config/add")
    public Result<Object> add(FormMark formMark) {
        List<FormMark> list = new ArrayList<>();
        formMark.getMarkTypeList().forEach(markType -> {
            FormMark form = new FormMark();
            form.setFormType(formMark.getFormType());
            form.setMarkTypeId(markType.getId());
            form.setMarkType(markType.getMarkType());
            form.setMarkDefault(markType.getMarkDefault());
            form.setId(form.md5());
            list.add(form);
        });
        return Result.of(markFormService.saveOrUpdateBatch(list));
    }

    /**
     * 保存表单类型
     *
     * @param formMark 表单类型对象
     * @return 返回保存结果
     */
    @PostMapping("/config/save")
    public Result<Object> save(FormMark formMark) {
        return Result.of(markFormService.saveOrUpdate(formMark));
    }

    /**
     * 删除表单配置
     *
     * @param id 表单配置id
     * @return 返回删除结果
     */
    @DeleteMapping("/config/{id}")
    public Result<Object> delete(@PathVariable String id) {
        String[] ids = id.split(",");
        return Result.of(markFormService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("/type/list/{formType}")
    public Result<Page<HashMap<String, Object>>> typeList(Page<HashMap<String, Object>> page, @PathVariable String formType) {
        page.setSize(999);
        QueryWrapper<MarkType> query = new QueryWrapper<>();
        query.eq("FORM_TYPE", formType);

        return Result.of(page);
    }

}
