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

import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.form.pojo.pos.design.FormPO;
import ink.wgink.module.form.service.design.IFormFieldService;
import ink.wgink.module.form.service.design.IFormService;
import ink.wgink.module.form.service.report.IFormReportRouteService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Autowired
    private IFormService formService;
    @Autowired
    private IFormFieldService formFieldService;
    @Autowired
    private IFormReportRouteService formReportService;

    @GetMapping("save/code/{formCode}/version/{formVersion}")
    public void get(@PathVariable("formCode") String formCode,
                    @PathVariable("formVersion") Integer formVersion,
                    HttpSession httpSession,
                    HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse) {
        formReportService.save(formCode, formVersion, httpSession, httpServletRequest, httpServletResponse);
    }

    @GetMapping("update/code/{formCode}/version/{formVersion}")
    public void update(@PathVariable("formCode") String formCode,
                       @PathVariable("formVersion") Integer formVersion,
                       HttpSession httpSession,
                       HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse) {
        formReportService.update(formCode, formVersion, httpSession, httpServletRequest, httpServletResponse);
    }

    @GetMapping("show/code/{formCode}/version/{formVersion}")
    public void show(@PathVariable("formCode") String formCode,
                     @PathVariable("formVersion") Integer formVersion,
                     HttpSession httpSession,
                     HttpServletRequest httpServletRequest,
                     HttpServletResponse httpServletResponse) {
        formReportService.show(formCode, formVersion, httpSession, httpServletRequest, httpServletResponse);
    }

    @GetMapping("list/code/{formCode}/version/{formVersion}")
    public ModelAndView list(@PathVariable("formCode") String formCode,
                             @PathVariable("formVersion") Integer formVersion) {
        ModelAndView mv = new ModelAndView("form-report/list");
        mv.addObject("isFromMenu", false);
        setObject(formCode, formVersion, mv);
        return mv;
    }

    @GetMapping("list/code/{formCode}/version/{formVersion}/{isFromMenu}")
    public ModelAndView list(@PathVariable("formCode") String formCode,
                             @PathVariable("formVersion") Integer formVersion,
                             @PathVariable("isFromMenu") Boolean isFromMenu) {
        ModelAndView mv = new ModelAndView("form-report/list");
        mv.addObject("isFromMenu", isFromMenu);
        setObject(formCode, formVersion, mv);
        return mv;
    }

    private void setObject(String formCode, Integer formVersion, ModelAndView mv) {
        FormPO formPO = formService.getPOByCodeAndVersion(formCode, formVersion);
        if (formPO == null) {
            throw new SearchException("表单不存在");
        }
        List<Map<String, String>> showFields = formFieldService.listByFormId(formPO.getFormId()).stream().map(
                formFieldDTO -> {
                    Map<String, String> showField = new HashMap<>();
                    showField.put("fieldName", formFieldDTO.getFieldName());
                    showField.put("fieldExplain", formFieldDTO.getFieldExplain());
                    showField.put("fieldTag", formFieldDTO.getFieldTag());
                    return showField;
                }
        ).collect(Collectors.toList());

        mv.addObject("formCode", formCode);
        mv.addObject("formVersion", formVersion);
        mv.addObject("formType", formPO.getFormType());
        mv.addObject("showFields", showFields);
    }

}
