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

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.form.enums.design.FormStatusEnum;
import ink.wgink.module.form.pojo.dtos.design.FormDTO;
import ink.wgink.module.form.pojo.vos.design.FormVO;
import ink.wgink.module.form.service.design.IFormService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
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: FormController
 * @Description: 表单
 * @Author: wanggeng
 * @Date: 2022/3/10 7:38 AM
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "表单")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/form")
public class FormController extends DefaultBaseController {

    @Autowired
    private IFormService formService;

    @ApiOperation(value = "表单删除", notes = "通过id列表批量删除表单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "表单ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        formService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "表单删除（物理删除）", notes = "通过id删除（物理删除）表单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "表单ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("delete/{id}")
    public SuccessResult delete(@PathVariable("id") String id) {
        formService.delete(Arrays.asList(id));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改表单状态", notes = "修改表单状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path"),
            @ApiImplicitParam(name = "formStatus", value = "表单状态", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update-status/{formId}/{formStatus}")
    public SuccessResult updateFormStatus(@PathVariable("formId") String formId, @PathVariable("formStatus") String formStatus) {
        if (!StringUtils.equals(FormStatusEnum.ACTIVE.getValue(), formStatus)
                && !StringUtils.equals(FormStatusEnum.INACTIVE.getValue(), formStatus)) {
            throw new ParamsException("表单状态错误");
        }
        formService.updateStatus(formId, formStatus);
        return new SuccessResult();
    }

    @ApiOperation(value = "修改新增页面代码", notes = "修改新增页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-save-page-code/{formId}")
    public SuccessResult updateSavePageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getSavePageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateSavePageCode(formId, formVO.getSavePageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改修改页面代码", notes = "修改修改页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-update-page-code/{formId}")
    public SuccessResult updateUpdatePageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getUpdatePageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateUpdatePageCode(formId, formVO.getUpdatePageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改详情页面代码", notes = "修改详情页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-show-page-code/{formId}")
    public SuccessResult updateShowPageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getShowPageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateShowPageCode(formId, formVO.getShowPageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改打印页面代码", notes = "修改打印页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-print-page-code/{formId}")
    public SuccessResult updatePrintPageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getPrintPageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updatePrintPageCode(formId, formVO.getPrintPageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改App新增页面代码", notes = "修改App新增页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-app-save-page-code/{formId}")
    public SuccessResult updateAppSavePageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getAppSavePageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateAppSavePageCode(formId, formVO.getAppSavePageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改App修改页面代码", notes = "修改App修改页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-app-update-page-code/{formId}")
    public SuccessResult updateAppUpdatePageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getAppUpdatePageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateAppUpdatePageCode(formId, formVO.getAppUpdatePageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "修改App详情页面代码", notes = "修改App详情页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @PutMapping("update-app-show-page-code/{formId}")
    public SuccessResult updateAppShowPageCode(@PathVariable("formId") String formId, @RequestBody FormVO formVO) {
        if (StringUtils.isBlank(formVO.getAppShowPageCode())) {
            throw new ParamsException("代码不能为空");
        }
        formService.updateAppShowPageCode(formId, formVO.getAppShowPageCode());
        return new SuccessResult();
    }

    @ApiOperation(value = "更新到菜单", notes = "更新到菜单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path"),
            @ApiImplicitParam(name = "menuParentId", value = "上级菜单ID", paramType = "path")
    })
    @PutMapping("update-to-menu/form-id/{formId}/menu-parent-id/{menuParentId}")
    public SuccessResult updateToMenu(@PathVariable("formId") String formId, @PathVariable("menuParentId") String menuParentId) {
        formService.updateToMenu(formId, menuParentId);
        return new SuccessResult();
    }

    @ApiOperation(value = "表单列表", notes = "表单列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<FormDTO> list() {
        Map<String, Object> params = requestParams();
        return formService.list(params);
    }

    @ApiOperation(value = "详情", notes = "详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{formId}")
    public FormDTO get(@PathVariable("formId") String formId) {
        return formService.get(formId);
    }

    @ApiOperation(value = "新增页面代码", notes = "新增页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-save-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getSavePageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setSavePageCode(formService.getSavePageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "修改页面代码", notes = "修改页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-update-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getUpdatePageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setUpdatePageCode(formService.getUpdatePageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "详情页面代码", notes = "详情页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-show-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getShowPageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setShowPageCode(formService.getShowPageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "打印页面代码", notes = "打印页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-print-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getPrintPageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setPrintPageCode(formService.getPrintPageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "APP新增页面代码", notes = "APP新增页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-app-save-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getAppSavePageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setAppSavePageCode(formService.getAppSavePageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "修改页面代码", notes = "修改页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-app-update-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getAppUpdatePageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setAppUpdatePageCode(formService.getAppUpdatePageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "详情页面代码", notes = "详情页面代码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-app-show-page-code/code/{formCode}/version/{formVersion}")
    public FormDTO getAppShowPageCode(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        FormDTO formDTO = new FormDTO();
        formDTO.setAppShowPageCode(formService.getAppShowPageCodeByCodeAndVersion(formCode, formVersion));
        return formDTO;
    }

    @ApiOperation(value = "查看data", notes = "查看data接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-form-source-data/{formId}")
    public FormDTO getFormSourceData(@PathVariable("formId") String formId) {
        return formService.getFormSourceData(formId);
    }

    @ApiOperation(value = "表单分页列表", notes = "表单分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<FormDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formService.listPage(page);
    }

    @ApiOperation(value = "最新表单分页列表", notes = "最新表单分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-latest")
    public SuccessResultList<List<FormDTO>> listPageLatest(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formService.listPageLatest(page);
    }

    @ApiOperation(value = "历史表单分页列表", notes = "历史表单分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单ID", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-history/form-id/{formId}")
    public SuccessResultList<List<FormDTO>> listPageHistoryByFormId(@PathVariable("formId") String formId,
                                                           ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return formService.listPageHistoryByFormId(formId, page);
    }



}
