package ink.wgink.module.form.controller.app.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: FormReportAppController
 * @Description: 表单上报
 * @Author: wanggeng
 * @Date: 2022/3/10 7:29 PM
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "表单")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/form-report")
public class FormReportAppController extends DefaultBaseController {

    @Autowired
    private IFormReportService formReportService;

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

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

    @DeleteMapping("remove/code/{formCode}/version/{formVersion}/uids/{uids}")
    public SuccessResult remove(@RequestHeader("token") String token, @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(@RequestHeader("token") String token, @PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @PathVariable("uid") String uid, @RequestBody Map<String, Object> params) {
        checkParams(params);
        formReportService.updateByToken(token, formCode, formVersion, uid, params);
        return new SuccessResult();
    }

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

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

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

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

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

    @GetMapping("listpage/code/{formCode}/version/{formVersion}")
    public SuccessResultList<List<Map<String, Object>>> listPage(@RequestHeader("token") String token, @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-release/code/{formCode}/version/{formVersion}")
    public SuccessResultList<List<Map<String, Object>>> listPageRelease(@RequestHeader("userId") String userId, @PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formReportService.listPageByUserId(userId, formCode, formVersion, page);
    }

    @GetMapping("list-show-field/code/{formCode}/version/{formVersion}")
    public List<String> listShowField(@RequestHeader("token") String token, @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();
        }
    }

}
