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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.CoreCustomForm;
import com.yida.data.common.core.entity.customform.CoreCustomFormRelationUser;
import com.yida.data.common.core.entity.customform.CoreCustomFormRelationUserParent;
import com.yida.data.common.core.entity.customform.CoreCustomFormStatistic;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.customForm.dto.FormDataInput;
import com.yida.data.customForm.service.CoreCustomFormDataService;
import com.yida.data.customForm.service.CoreCustomFormRelationUserParentService;
import com.yida.data.customForm.service.CoreCustomFormRelationUserService;
import com.yida.data.customForm.service.CoreCustomFormService;
import com.yida.data.customForm.vo.FormStudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ccl
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/appCustomForm")
@RequiredArgsConstructor
@Api(tags = "app表单--表单管理")
public class AppCustomFormController {

    @Resource
    private final CoreCustomFormService formService;

    @Resource
    private final CoreCustomFormDataService coreCustomFormDataService;

    @Resource
    private final CoreCustomFormRelationUserService userService;

    @Resource
    private final CoreCustomFormRelationUserParentService parentService;

    @GetMapping("appFindFormList")
    @ApiOperation("app查询填报列表")
    public ResultBean<IPage<CoreCustomForm>> appFindFormList(@ApiParam("用户id") @RequestParam Long userId,
                                                             @ApiParam("0为职工，1为学生，2为家长") @RequestParam Integer type,
                                                             @ApiParam("学校id") @RequestParam Long schoolId,
                                                             @ApiParam("表单名称") @RequestParam(required = false) String tableName,
                                                             @ApiParam("表单状态，不传为全查") @RequestParam(required = false) Integer status,
                                                             @RequestParam(defaultValue = "10") Long pageSize,
                                                             @RequestParam(defaultValue = "1") Long pageNum){

        return ResultBean.buildSuccess(formService.appFindFormList(new Page<CoreCustomForm>(pageNum,pageSize),userId,tableName,status,schoolId,type));
    }

    @GetMapping("appFindAdminFormList")
    @ApiOperation("app查询管理列表列表")
    public ResultBean<IPage<CoreCustomForm>> appFindAdminFormList(@ApiParam("用户id") @RequestParam Long userId,
                                                                  @ApiParam("学校id") @RequestParam Long schoolId,
                                                                  @ApiParam("表单名称") @RequestParam(required = false) String tableName,
                                                                  @ApiParam("表单状态，不传为全查") @RequestParam(required = false) Integer status,
                                                                  @RequestParam(defaultValue = "10") Long pageSize,
                                                                  @RequestParam(defaultValue = "1") Long pageNum){
        return ResultBean.buildSuccess(formService.appFindAdminFormList(new Page<CoreCustomForm>(pageNum,pageSize),userId,tableName,status,schoolId));
    }

    @GetMapping("appHandOnCustomForm")
    @ApiOperation("app用户查询用户是否是管理员")
     public ResultBean appHandOnCustomForm(@ApiParam("用户id")@RequestParam Long userId,
                                          @ApiParam("学校id")@RequestParam Long schoolId){

        return ResultBean.buildSuccess(formService.appHandOnCustomForm(userId,schoolId));
    }

    /***
     * @Author jianMingWang
     * @Description 提交表单数据
     * @Date 11:28 2021/8/11
     * @Param [formDataInput]
     * @return com.liuliu.data.custom.entity.ResultBean
     **/
    @ApiOperation("app提交保存表单数据")
    @PostMapping("/saveFormData")
    public ResultBean saveFormData(@RequestBody FormDataInput formDataInput) {

        CoreCustomForm form = formService.getOne(Wrappers.lambdaQuery(new CoreCustomForm())
                .eq(CoreCustomForm::getTableName, formDataInput.getTableName()));

        //验证用否是否能填报
        List<Long> userList = new ArrayList<>();
        List<CoreCustomFormRelationUser> list = userService.list(Wrappers.lambdaQuery(new CoreCustomFormRelationUser())
                .eq(CoreCustomFormRelationUser::getCustomFormId, form.getId()));
        if (form.getRecever()==2){
            List<Long> list1 = new ArrayList<>();
            List<CoreCustomFormRelationUserParent> parentList = parentService.list(Wrappers.lambdaQuery(new CoreCustomFormRelationUserParent())
                    .eq(CoreCustomFormRelationUserParent::getCustomFormId, form.getId()));
            for (CoreCustomFormRelationUserParent userParent : parentList) {
                list1.add(userParent.getParentId());
            }
            if (!list1.contains(formDataInput.getUserId())){
                return ResultBean.buildError("您不能填报该表");
            }
        }else {
            for (CoreCustomFormRelationUser relationUser : list) {
                userList.add(relationUser.getUserId());
            }
            if (!userList.contains(formDataInput.getUserId())) {
                return ResultBean.buildError("您不能填报该表");
            }
        }
        Map data = coreCustomFormDataService.findData(form.getId(), formDataInput.getUserId());
        if (ObjectUtil.isNotEmpty(data)&&form.getUpdateType()==0){
            return ResultBean.buildError("您已填报，请勿重复填写");
        }
        coreCustomFormDataService.saveFormData(formDataInput,data);
        return ResultBean.buildSuccess();

    }

    @GetMapping("appFindFormAndData")
    @ApiOperation("app查询表单填报详情")
    public ResultBean appFindFormAndData(@ApiParam("用户id")@RequestParam Long userId,
                                         @ApiParam("表单id")@RequestParam Long formId){

        return ResultBean.buildSuccess(formService.appFindFormAndData(userId,formId));
    }

    @GetMapping("appFindStudentByForm")
    @ApiOperation("app查询表单关联的学生列表")
    public ResultBean<List<FormStudentVO>> appFindStudentByForm(@ApiParam("家长id")@RequestParam Long userId,
                                                                @ApiParam("表单id")@RequestParam Long formId){

        return ResultBean.buildSuccess(formService.appFindStudentByForm(userId,formId));
    }
}
