package com.yida.data.form.customForm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.customform.*;
import com.yida.data.customForm.dto.RemindAgainDTO;
import com.yida.data.form.customForm.entity.FormDataReturn;
import com.yida.data.form.customForm.service.CoreCustomFormDataService;
import com.yida.data.form.customForm.service.CoreCustomFormItemChildrenService;
import com.yida.data.form.customForm.service.CoreCustomFormItemsService;
import com.yida.data.form.customForm.service.CoreCustomFormService;
import com.yida.data.customForm.vo.CommitUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName CoreCustomFormDataController
 * @Author jianMingWang
 * @Date 2021/8/12 16:54
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/coreCustomFormData")
@RequiredArgsConstructor
@Api(tags = "后台--表单数据相关")
public class CoreCustomFormDataController {

    @Autowired
    private CoreCustomFormDataService coreCustomFormDataService;
    @Autowired
    private CoreCustomFormService coreCustomFormService;
    @Autowired
    private CoreCustomFormItemsService coreCustomFormItemsService;
    @Autowired
    private CoreCustomFormItemChildrenService coreCustomFormItemChildrenService;


    /***
     * @Author jianMingWang
     * @Description //列表分页查看当前表单数据
     * @Date 17:33 2021/9/16
     * @Param []
     * @return com.yida.data.common.core.common.ResultBean<java.util.HashMap>
     **/
    @ApiOperation("列表分页查询当前表单数据")
    @GetMapping("/findSubmitFromDataListByFormId")
    public ResultBean<FormDataReturn> findSubmitFromDataListByFormId(@ApiParam(required = true, value = "表单主键ID") @RequestParam Integer formId,
                                                                     @ApiParam(required = true, value = "每页条数") @RequestParam(required = true, defaultValue = "10") Integer pageSize,
                                                                     @ApiParam(required = true, value = "页码") @RequestParam(required = true, defaultValue = "1") Integer pageNo) {

        return ResultBean.buildSuccess(coreCustomFormDataService.findList(formId, pageSize, pageNo));

    }

    /***
     * @Author jianMingWang
     * @Description //导出excel数据
     * @Date 16:40 2021/9/17
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.customForm.entity.FormDataReturn>
     **/
    @GetMapping("/exportData")
    @ApiOperation("导出表单数据")
    public void exportData(@ApiParam(value = "表单主键ID", required = true) @RequestParam Integer formId, HttpServletResponse response) {
        try {
            coreCustomFormDataService.exportData(formId, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * @Author jianMingWang
     * @Description //获取当前表单
     * @Date 11:22 2021/8/10
     * @Param [schoolId, formTypeId]
     * @return com.liuliu.data.custom.entity.ResultBean<com.liuliu.data.custom.entity.CoreCustomForm>
     **/
    @ApiOperation("表单详情")
    @GetMapping("/findById")
    public ResultBean<CoreCustomForm> findById(@ApiParam(value = "主键ID", required = true, type = "Long") Long id) {

        CoreCustomForm coreCustomForm = coreCustomFormService.getById(id);
        List<CoreCustomFormItems> coreCustomFormItemList = coreCustomFormItemsService.list(Wrappers.<CoreCustomFormItems>query().lambda().eq(CoreCustomFormItems::getFormId, id));
        coreCustomFormItemList.forEach(coreCustomFormItems -> {
            List<CoreCustomFormItemChildren> coreCustomFormItemChildrenlist = coreCustomFormItemChildrenService.list(Wrappers.<CoreCustomFormItemChildren>query().lambda().eq(CoreCustomFormItemChildren::getFormItemId, coreCustomFormItems.getId()));
            coreCustomFormItems.setCoreCustomFormItemChildrenList(coreCustomFormItemChildrenlist);
        });
        coreCustomForm.setCoreCustomFormItemsList(coreCustomFormItemList);
        return ResultBean.buildSuccess(coreCustomForm);


    }

    @GetMapping("commitUser")
    @ApiOperation("表单人员列表查询")
    public ResultBean<CommitUserVO> commitUser(@ApiParam("表单id") @RequestParam Long formId,
                                                  @ApiParam("未提交为0，已提交为1") @RequestParam Integer type,
                                                  @ApiParam("名字") @RequestParam(required = false) String name,
                                                  @RequestParam(defaultValue = "1") Long pageNum,
                                                  @RequestParam(defaultValue = "10") Long pageSize) {

        Page<CoreCustomFormRelationUser> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(coreCustomFormService.commitUser(formId,type,name,page));
    }

    @GetMapping("findCommitUser")
    @ApiOperation("已提交人员列表查询")
    public ResultBean<Page> findCommitUser(@ApiParam("表单id") @RequestParam Long formId,
                                           @ApiParam("0为未提交，1为已提交") @RequestParam(required = false) Integer fillType,
                                           @RequestParam(defaultValue = "1") Long pageNum,
                                           @RequestParam(defaultValue = "10") Long pageSize) {

        Page page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(coreCustomFormService.findCommitUser(formId, fillType, page));
    }

    @GetMapping("findStatistic")
    @ApiOperation("查询统计数据")
    public ResultBean<CoreCustomFormStatistic> findStatistic(@ApiParam("表单id") @RequestParam Long formId) {

        return ResultBean.buildSuccess(coreCustomFormService.findStatistic(formId));
    }

    @PostMapping("remindAgain")
    @ApiOperation("未提交人员再次提醒")
    public ResultBean remindAgain(@RequestBody RemindAgainDTO dto) {

        coreCustomFormService.remindAgain(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("导出已提交、未提交人员")
    @GetMapping("/exportUserListInOut")
    public void exportUserListInOut(@ApiParam("表单id") @RequestParam Long formId, @ApiParam("1为已提交，0为未提交") @RequestParam Integer status, HttpServletResponse response) {
        coreCustomFormService.exportUserListInOut(formId, status, response);
    }

}
