package ink.wgink.module.form.controller.api.report;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.form.service.report.IFormReportService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: FormReportController
 * @Description: 表单上报
 * @Author: wanggeng
 * @Date: 2022/3/10 7:29 PM
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "表单")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/form-report")
public class FormReportController extends DefaultBaseController {

    @Autowired
    private IFormReportService formReportService;

    @PostMapping("save/code/{formCode}/version/{formVersion}")
    public SuccessResult save(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @RequestBody Map<String, Object> params) {
        checkParams(params);
        formReportService.save(formCode, formVersion, params);
        return new SuccessResult();
    }

    @DeleteMapping("remove/code/{formCode}/version/{formVersion}/uids/{uids}")
    public SuccessResult remove(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @PathVariable("uids") String uids) {
        formReportService.remove(formCode, formVersion, Arrays.asList(uids.split("\\_")));
        return new SuccessResult();
    }

    @PutMapping("update/code/{formCode}/version/{formVersion}/uid/{uid}")
    public SuccessResult update(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @PathVariable("uid") String uid, @RequestBody Map<String, Object> params) {
        checkParams(params);
        formReportService.update(formCode, formVersion, uid, params);
        return new SuccessResult();
    }

    @GetMapping("get/code/{formCode}/version/{formVersion}/uid/{uid}")
    public Map<String, Object> get(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @PathVariable("uid") String uid) {
        return formReportService.get(formCode, formVersion, uid);
    }

    @GetMapping("list/code/{formCode}/version/{formVersion}")
    public List<Map<String, Object>> list(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        Map<String, Object> params = requestParams();
        return formReportService.list(formCode, formVersion, params);
    }

    @GetMapping("listpage/code/{formCode}/version/{formVersion}")
    public SuccessResultList<List<Map<String, Object>>> listPage(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formReportService.listPage(formCode, formVersion, page);
    }

    @GetMapping("listpage-all-fields/code/{formCode}/version/{formVersion}")
    public SuccessResultList<List<Map<String, Object>>> listPageAllField(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formReportService.listPageAllField(formCode, formVersion, page);
    }

    @GetMapping("list-show-field/code/{formCode}/version/{formVersion}")
    public List<String> listShowField(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        return formReportService.listShowField(formCode, formVersion);
    }

    private void checkParams(Map<String, Object> body) {
        if (body.isEmpty()) {
            throw new ParamsException("提交内容不能为空");
        }
        for (Map.Entry<String, Object> kv : body.entrySet()) {
            String key = kv.getKey();
        }
    }

}
